Skip to content

Instantly share code, notes, and snippets.

@zed

zed/bio.py Secret

Created April 19, 2014 20:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/07b4b2f5b13507ac33af to your computer and use it in GitHub Desktop.
Save zed/07b4b2f5b13507ac33af to your computer and use it in GitHub Desktop.
#!/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