-
-
Save kevinhowbrook/6cf535d2507f279946511c8aa1cd9520 to your computer and use it in GitHub Desktop.
KanyeRest and KanyeParse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import requests | |
from django.conf import settings | |
logger = logging.getLogger(__name__) | |
class KanyeParse: | |
def __init__(self, data): | |
self.data = data | |
# A very simple parser | |
def get_parsed_data(self): | |
parsed_data = json.loads(self.data) | |
return parsed_data["quote"] | |
class KanyeRest: | |
def __init__(self): | |
self.data = None | |
self.url = "https://api.kanye.rest/" | |
def get_url(self): | |
return self.url | |
def fetch_data(self): | |
try: | |
response = requests.get(url=self.url) | |
self.data = response.content | |
except requests.exceptions.Timeout: | |
logger.exception(f"Timeout occurred") | |
except Exception: | |
logger.exception(f"Error occurred") | |
return self.data | |
def get_data(self): | |
""" If there is data, parse it, otherwise return an empty list """ | |
self.data = self.fetch_data() | |
if self.data: | |
self.data = KanyeParse(self.data).get_parsed_data() | |
return self.data | |
else: | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment