70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
import requests
|
|
import json
|
|
|
|
from models.xkcdComic import Comic
|
|
|
|
|
|
class ProgrammerExcuses:
|
|
def __init__(self) -> None:
|
|
self.__url = "http://programmingexcuses.com"
|
|
|
|
def getExcuse(self):
|
|
page = requests.get(url=self.__url)
|
|
content = page.content.decode().split("\n")
|
|
for html in content:
|
|
if 'href="/"' in html:
|
|
start_index = html.find('3;">')
|
|
end_index = html.find("</a></center>")
|
|
return html[start_index+4:end_index]
|
|
|
|
|
|
class XKCD:
|
|
def __init__(self) -> None:
|
|
self.url = "https://xkcd.com"
|
|
self.__api = "info.0.json"
|
|
|
|
def getRandomComic(self):
|
|
response = requests.get(url=f"https://c.xkcd.com/random/comic")
|
|
|
|
for line in response.text.split("\n"):
|
|
if '<meta property="og:url"' in line:
|
|
line = line.split('"')
|
|
for cell in line:
|
|
if "https" in cell:
|
|
return self.getSpecificComic(url=cell)
|
|
|
|
def getLastComic(self):
|
|
response = requests.api.get(f"{self.url}/{self.__api}")
|
|
return Comic(json.loads(response.text))
|
|
|
|
def getSpecificComic(self, num=None, url=None):
|
|
if num != None:
|
|
response = requests.api.get(url=f"{self.url}/{num}/{self.__api}")
|
|
return Comic(json.loads(response.text))
|
|
|
|
if url != None:
|
|
response = requests.api.get(url=f"{url}/{self.__api}")
|
|
return Comic(json.loads(response.text))
|
|
|
|
class Reddit:
|
|
def __init__(self) -> None:
|
|
self.url = "https://www.reddit.com"
|
|
self.random = "random.json"
|
|
|
|
def _getRandomPost(self, community):
|
|
response = requests.get(f"{self.url}/{community}/{self.random}")
|
|
return json.loads(response.text)
|
|
|
|
class RedditProgrammerHumor(Reddit):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
self.community = "r/ProgrammerHumor"
|
|
|
|
def getRandomPost(self):
|
|
post = self._getRandomPost(self.community)
|
|
with open(file="data/log/test.json", mode="w") as file:
|
|
file.write(json.dumps(obj=post, indent=2))
|
|
file.close()
|
|
return post
|
|
|
|
|