Compare commits

...

5 Commits

Author SHA1 Message Date
Chuck Sneed a24b36a822 updates 2022-11-27 14:14:34 -06:00
Chuck Sneed 9613bfe054 Merge branch 'master' of https://github.com/UnironicHeyMoon/RDramaAPIInterface 2022-07-22 19:52:14 -05:00
Chuck Sneed fae35e819f Dr. Transmisia's changes 2022-07-22 19:50:12 -05:00
HeyMoon 39e6c1963e
Update README.md 2022-07-22 18:55:45 -05:00
HeyMoon dc25798767
Update README.md 2022-07-22 18:55:20 -05:00
2 changed files with 54 additions and 22 deletions

View File

@ -1,7 +1,12 @@
import time
import requests
from bs4 import BeautifulSoup
import traceback
import backoff
class TimeOutException(Exception):
pass
'''
Wrapper around the RDRama API
'''
@ -25,43 +30,62 @@ class RDramaAPIInterface:
'''
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"
return self.post(url, data={
'parent_fullname':parent_fullname,
'submission': parent_submission,
"body": message
})
if file == None:
return self.post(url, data={
'parent_fullname':parent_fullname,
'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.
'''
def reply_to_comment_easy(self,comment_id, parent_submission, message):
return self.reply_to_comment(f"t3_{comment_id}", parent_submission, message)
def reply_to_comment_easy(self,comment_id, parent_submission, message, file=None):
return self.reply_to_comment(f"c_{comment_id}", parent_submission, message, file=file)
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.
'''
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):
url=f"{self.protocol}://{self.site}/comments"
url=f"{self.protocol}://{self.site}/{root}"
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:
return self.get(url)
else:
results = []
for i_ in range(number_of_pages):
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']
return {
'data': results
}
'''
Calls the notifications endpoint
@ -85,6 +109,10 @@ class RDramaAPIInterface:
url=f"{self.protocol}://{self.site}"
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):
url=f"{self.protocol}://{self.site}/is_repost"
return self.post(url, {'url': the_url})['permalink'] != ''
@ -265,6 +293,7 @@ class RDramaAPIInterface:
"user_id": notification['author']['id'],
"id": notification["id"],
"message": notification['body'],
"parent_comment_id": notification['parent_comment_id'] if notification['level'] != 1 else None,
"post_id": notification['post_id']
}
@ -321,13 +350,16 @@ class RDramaAPIInterface:
else:
return response.json()
@backoff.on_exception(backoff.expo, requests.exceptions.RequestException)
def post(self, url, data):
response = requests.post(url, headers=self.headers, data=data)
@backoff.on_exception(backoff.expo, TimeOutException)
def post(self, url, data, files = None):
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}")
if (response.status_code == 429):
raise requests.exceptions.RequestException()
raise TimeOutException
if (response.status_code != 200):
raise BaseException(f"POST {url} ({response.status_code}) {data} => {response.json()}")
else:
return response.json()
return response.json()

View File

@ -51,15 +51,15 @@ Yep. Just replace "rdrama.net" with whatever site it is.
probably because you are using https. try this:
'''python
```python
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
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