Last active
August 29, 2015 14:11
-
-
Save Bachmann1234/2c262287cc9a2935be70 to your computer and use it in GitHub Desktop.
Summarize Python3
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
#!/usr/bin/env python3 | |
import os | |
import urllib | |
import urllib.request | |
import sys | |
import re | |
import json | |
TITLE = "title" | |
if __name__ == '__main__': | |
if len(sys.argv) < 1: | |
print("This script summarizes urls. Please provide one") | |
exit(1) | |
AYLIEN_ID = 'AYLIENID' | |
AYLIEN_KEY = 'AYLIENKEY' | |
aylein_url = 'https://api.aylien.com/api/v1/summarize?url={}' | |
page = urllib.request.urlopen(sys.argv[1].strip()).read().decode('utf-8') | |
try: | |
title = re.search( | |
r'(<{title}.*>)(.*)(</{title}>)'.format(title=TITLE), | |
page, | |
re.IGNORECASE | |
).group(2) | |
except AttributeError: | |
title = "Failed to find title" | |
headers = { | |
'Accept': 'application/json', | |
'Content-type': 'application/x-www-form-urlencoded', | |
'X-AYLIEN-TextAPI-Application-ID': os.environ.get(AYLIEN_ID), | |
'X-AYLIEN-TextAPI-Application-Key': os.environ.get(AYLIEN_KEY) | |
} | |
request = urllib.request.Request( | |
aylein_url.format(urllib.parse.quote(sys.argv[1])) | |
) | |
for key, value in headers.items(): | |
request.add_header(key, value) | |
summary = json.loads(urllib.request.urlopen(request).read().decode('utf-8')) | |
print("{}{}{}".format('\033[1m', title, '\033[0m')) | |
print("\n") | |
for sentence in summary['sentences']: | |
print("- {}\n".format(sentence)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment