Compare commits
5 Commits
c8664a1327
...
a24b36a822
Author | SHA1 | Date |
---|---|---|
Chuck Sneed | a24b36a822 | |
Chuck Sneed | 9613bfe054 | |
Chuck Sneed | fae35e819f | |
HeyMoon | 39e6c1963e | |
HeyMoon | dc25798767 |
|
@ -1,7 +1,12 @@
|
||||||
|
import time
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import traceback
|
import traceback
|
||||||
import backoff
|
import backoff
|
||||||
|
|
||||||
|
class TimeOutException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Wrapper around the RDRama API
|
Wrapper around the RDRama API
|
||||||
'''
|
'''
|
||||||
|
@ -25,43 +30,62 @@ class RDramaAPIInterface:
|
||||||
'''
|
'''
|
||||||
Replies to the comment with the given id.
|
Replies to the comment with the given id.
|
||||||
'''
|
'''
|
||||||
def reply_to_comment(self,parent_fullname, parent_submission, message):
|
def reply_to_comment(self,parent_fullname, parent_submission, message, file=None):
|
||||||
url=f"{self.protocol}://{self.site}/comment"
|
url=f"{self.protocol}://{self.site}/comment"
|
||||||
return self.post(url, data={
|
if file == None:
|
||||||
'parent_fullname':parent_fullname,
|
return self.post(url, data={
|
||||||
'submission': parent_submission,
|
'parent_fullname':parent_fullname,
|
||||||
"body": message
|
'submission': parent_submission,
|
||||||
})
|
"body": message
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
return self.post(url, data={
|
||||||
|
'parent_fullname':parent_fullname,
|
||||||
|
'submission': parent_submission,
|
||||||
|
"body": message
|
||||||
|
}, files=file)
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Replies to the comment with the given id.
|
Replies to the comment with the given id.
|
||||||
'''
|
'''
|
||||||
def reply_to_comment_easy(self,comment_id, parent_submission, message):
|
def reply_to_comment_easy(self,comment_id, parent_submission, message, file=None):
|
||||||
return self.reply_to_comment(f"t3_{comment_id}", parent_submission, message)
|
return self.reply_to_comment(f"c_{comment_id}", parent_submission, message, file=file)
|
||||||
|
|
||||||
def reply_to_post(self, post_id, message):
|
def reply_to_post(self, post_id, message):
|
||||||
return self.reply_to_comment(f"t2_{post_id}", post_id, message)
|
return self.reply_to_comment(f"p_{post_id}", post_id, message)
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Gets "all" comments.
|
Gets "all" comments.
|
||||||
'''
|
'''
|
||||||
def get_comments(self, number_of_pages=1, user=None):
|
def get_comments(self, number_of_pages=1, user=None, sort="new", upper_bound = 0, lower_bound = 0, t="all"):
|
||||||
|
return self.search("comments", number_of_pages, user, sort, upper_bound, lower_bound, t)
|
||||||
|
|
||||||
|
'''
|
||||||
|
Gets "all" posts.
|
||||||
|
'''
|
||||||
|
def get_posts(self, number_of_pages=1, user=None, sort="new", upper_bound = 0, lower_bound = 0, t="all"):
|
||||||
|
return self.search("", number_of_pages, user, sort, upper_bound, lower_bound, t)
|
||||||
|
|
||||||
|
def search(self, root, number_of_pages, user, sort, upper_bound, lower_bound, t):
|
||||||
if (user == None):
|
if (user == None):
|
||||||
url=f"{self.protocol}://{self.site}/comments"
|
url=f"{self.protocol}://{self.site}/{root}"
|
||||||
else:
|
else:
|
||||||
url=f"{self.protocol}://{self.site}/@{user}/comments"
|
url=f"{self.protocol}://{self.site}/@{user}/{root}"
|
||||||
|
|
||||||
|
params = f"?sort={sort}&t={t}&before={upper_bound}&after={lower_bound}"
|
||||||
|
url+=params
|
||||||
|
|
||||||
if number_of_pages == 1:
|
if number_of_pages == 1:
|
||||||
return self.get(url)
|
return self.get(url)
|
||||||
else:
|
else:
|
||||||
results = []
|
results = []
|
||||||
for i_ in range(number_of_pages):
|
for i_ in range(number_of_pages):
|
||||||
i = i_ + 1
|
i = i_ + 1
|
||||||
full_url=f"{url}?page={i}&sort=new&t=all"
|
full_url=f"{url}&page={i}"
|
||||||
results += self.get(full_url)['data']
|
results += self.get(full_url)['data']
|
||||||
return {
|
return {
|
||||||
'data': results
|
'data': results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Calls the notifications endpoint
|
Calls the notifications endpoint
|
||||||
|
@ -85,6 +109,10 @@ class RDramaAPIInterface:
|
||||||
url=f"{self.protocol}://{self.site}"
|
url=f"{self.protocol}://{self.site}"
|
||||||
return self.get(url)
|
return self.get(url)
|
||||||
|
|
||||||
|
def get_hole(self, hole: str):
|
||||||
|
url = f"{self.protocol}://{self.site}/h/{hole}"
|
||||||
|
return self.get(url)
|
||||||
|
|
||||||
def has_url_been_posted(self, the_url):
|
def has_url_been_posted(self, the_url):
|
||||||
url=f"{self.protocol}://{self.site}/is_repost"
|
url=f"{self.protocol}://{self.site}/is_repost"
|
||||||
return self.post(url, {'url': the_url})['permalink'] != ''
|
return self.post(url, {'url': the_url})['permalink'] != ''
|
||||||
|
@ -265,6 +293,7 @@ class RDramaAPIInterface:
|
||||||
"user_id": notification['author']['id'],
|
"user_id": notification['author']['id'],
|
||||||
"id": notification["id"],
|
"id": notification["id"],
|
||||||
"message": notification['body'],
|
"message": notification['body'],
|
||||||
|
"parent_comment_id": notification['parent_comment_id'] if notification['level'] != 1 else None,
|
||||||
"post_id": notification['post_id']
|
"post_id": notification['post_id']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,13 +350,16 @@ class RDramaAPIInterface:
|
||||||
else:
|
else:
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
@backoff.on_exception(backoff.expo, requests.exceptions.RequestException)
|
@backoff.on_exception(backoff.expo, TimeOutException)
|
||||||
def post(self, url, data):
|
def post(self, url, data, files = None):
|
||||||
response = requests.post(url, headers=self.headers, data=data)
|
if files == None:
|
||||||
|
response = requests.post(url, headers=self.headers, data=data)
|
||||||
|
else:
|
||||||
|
response = requests.post(url, headers=self.headers, data=data, files=files)
|
||||||
print(f"POST {url} ({response.status_code}) {data}")
|
print(f"POST {url} ({response.status_code}) {data}")
|
||||||
if (response.status_code == 429):
|
if (response.status_code == 429):
|
||||||
raise requests.exceptions.RequestException()
|
raise TimeOutException
|
||||||
if (response.status_code != 200):
|
if (response.status_code != 200):
|
||||||
raise BaseException(f"POST {url} ({response.status_code}) {data} => {response.json()}")
|
raise BaseException(f"POST {url} ({response.status_code}) {data} => {response.json()}")
|
||||||
else:
|
else:
|
||||||
return response.json()
|
return response.json()
|
|
@ -51,15 +51,15 @@ Yep. Just replace "rdrama.net" with whatever site it is.
|
||||||
|
|
||||||
probably because you are using https. try this:
|
probably because you are using https. try this:
|
||||||
|
|
||||||
'''python
|
```python
|
||||||
|
|
||||||
rdrama = RDramaAPIInterface(TEST_AUTH_TOKEN, "rdrama.net", https=False)
|
rdrama = RDramaAPIInterface(TEST_AUTH_TOKEN, "rdrama.net", https=False)
|
||||||
|
|
||||||
'''
|
```
|
||||||
|
|
||||||
### Q. WTF dude, my code was working yesterday and it suddenly started breaking, I literally cannot cope with this
|
### Q. WTF dude, my code was working yesterday and it suddenly started breaking, I literally cannot cope with this
|
||||||
|
|
||||||
aevann probably fucked around with the code again lmao. ask him what's going on. also, for real, don't be a douche like "ah ha idiot I found an issue in the code". you are an api, aevann is going to prioritize actual users of the site over bots.
|
aevann probably fucked around with the code again lmao. ask him what's going on.
|
||||||
|
|
||||||
### Q. Uhm, HeyMoon, your code is broken
|
### Q. Uhm, HeyMoon, your code is broken
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue