-
-
Save zed/07b4b2f5b13507ac33af to your computer and use it in GitHub Desktop.
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 | |
"""Fetch bio for a given person. | |
Usage: | |
$ ./bio Gandi | |
See http://stackoverflow.com/a/23171221 | |
""" | |
import json | |
import posixpath | |
import sys | |
from urllib import quote as urlquote | |
from urllib2 import urlopen | |
from urlparse import urlsplit, urljoin | |
def fetch_json(url): | |
return json.load(urlopen(url)) | |
# get person's slug | |
if len(sys.argv) < 2: | |
sys.exit(__doc__) | |
person = " ".join(sys.argv[1:]) | |
url = ("https://www.googleapis.com/customsearch/v1?q={}&" | |
"key=AIzaSyCMGfdDaSfjqv5zYoS0mTJnOT3e9MURWkU&" | |
"cx=011223861749738482324%3Aijiqp2ioyxw&num=1").format(urlquote(person)) | |
link = fetch_json(url)['items'][0]['link'] | |
slug = posixpath.basename(urlsplit(link).path) | |
# fetch bio | |
data = fetch_json(urljoin( | |
"http://api.saymedia-content.com/:apiproxy-anon/content-sites/" | |
"cs01a33b78d5c5860e/content-customs/@published/" | |
"@by-custom-type/ContentPerson/@by-slug/", slug)) | |
# pretty print as json | |
json.dump(data, sys.stdout, indent=2) | |
# show in a browser | |
import tempfile | |
import webbrowser | |
import time | |
def open_in_browser(html): | |
"""like lxml.html.open_in_browser() but `html` is a bytestring.""" | |
with tempfile.NamedTemporaryFile("wb", 0, suffix='.html') as file: | |
file.write(html) | |
webbrowser.open(file.name) | |
time.sleep(60) # give the browser a minute to open before | |
# deleting the file | |
open_in_browser(u'''<!doctype html> | |
<meta charset="utf-8"> | |
<title>{}</title> | |
{} | |
'''.format(person, data['entries'][0]['profileTml']).encode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment