Skip to content

Instantly share code, notes, and snippets.

@Bachmann1234
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bachmann1234/ca61dfd411d0d97db1b8 to your computer and use it in GitHub Desktop.
Save Bachmann1234/ca61dfd411d0d97db1b8 to your computer and use it in GitHub Desktop.
Summarize articles
#!/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))
@Bachmann1234
Copy link
Author

No dependencies, python2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment