Last active
July 2, 2024 17:14
-
-
Save lightstrike/0f5606e615106c44cbb8fb524595cb12 to your computer and use it in GitHub Desktop.
Readable.io API Python Wrapper
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 hashlib | |
import time | |
import requests | |
class ReadableAPIClient: | |
""" | |
API Client for Readable.io, written for Python 3 | |
See API documentation: https://readable.io/api/docs/ | |
NOTE: Very limited exception handling, expand and adapt as needed | |
Example usage: | |
readable_client = ReadableAPIClient(**{"api_key": "your_api_string"}) | |
text_scoring = readable_client.check_text_readability("You can see your usage of the API in your account administration area.") | |
url_scoring = readable_client.check_url_readability("https://readable.io/api/docs/") | |
highlights_scoring = readable_client.retrieve_highlighted_issues("a2345c78f") | |
text_data = readable_client.get_readability_with_highlights( | |
analysis_type="text", | |
source="You can see your usage of the API in your account administration area", | |
) | |
url_data = readable_client.get_readability_with_highlights( | |
analysis_type="url", | |
source="https://readable.io/api/docs/", | |
extract=True, | |
) | |
""" | |
BASE_API_URL = 'https://api.readable.io/api/' | |
def __init__(self, *args, **kwargs): | |
self._api_key = kwargs.get('api_key') | |
def _prepare_headers(self): | |
timestamp = str(int(time.time())) | |
combined = '{}{}'.format(self._api_key, timestamp).encode('utf8') | |
signing = hashlib.md5() | |
signing.update(combined) | |
headers = {'API_SIGNATURE': signing.hexdigest().encode('utf8'), | |
'API_REQUEST_TIME': timestamp} | |
return headers | |
def _post(self, endpoint, payload): | |
request_url = ''.join([self.BASE_API_URL, endpoint]) | |
headers = self._prepare_headers() | |
response = requests.post(request_url, data=payload, headers=headers) | |
return response | |
def check_text_readability(self, text): | |
ENDPOINT = 'text/' | |
payload = {'text': text} | |
response = self._post(ENDPOINT, payload) | |
return response | |
def check_url_readability(self, url, extract=False): | |
ENDPOINT = 'url/' | |
payload = {'url': url, 'extract': extract} | |
response = self._post(ENDPOINT, payload) | |
return response | |
def retrieve_highlighted_issues(self, score_id): | |
ENDPOINT = 'highlight/' | |
payload = {'score_id': score_id} | |
response = self._post(ENDPOINT, payload) | |
return response | |
def get_readability_with_highlights(self, analysis_type, source, extract=False): | |
if analysis_type == 'url': | |
scoring_response = self.check_url_readability(source, extract) | |
elif analysis_type == 'text': | |
scoring_response = self.check_text_readability(source) | |
else: | |
raise Exception("Set analysis_type parameter to 'url' or 'text'") | |
scoring_data = scoring_response.json() | |
highlights_response = self.retrieve_highlighted_issues(scoring_data['score_id']) | |
return { | |
'scoring': scoring_data, | |
'highlights': highlights_response.json(), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment