Last active
August 29, 2015 14:10
-
-
Save Bachmann1234/ca61dfd411d0d97db1b8 to your computer and use it in GitHub Desktop.
Summarize articles
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 python | |
from __future__ import unicode_literals, print_function | |
import os | |
import urllib | |
import urllib2 | |
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 = urllib2.urlopen(sys.argv[1]).read() | |
title = re.search( | |
r'<{title}>.*</{title}>'.format(title=TITLE), | |
page, | |
re.IGNORECASE | |
).group(0)[len(TITLE)+2:-(len(TITLE)+3)] | |
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 = urllib2.Request( | |
aylein_url.format(urllib.quote(sys.argv[1])) | |
) | |
for key, value in headers.items(): | |
request.add_header(key, value) | |
summary = json.loads(urllib2.urlopen(request).read()) | |
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
No dependencies, python2.7