Skip to content

Instantly share code, notes, and snippets.

@markpasc
Created January 14, 2010 20:09
Show Gist options
  • Save markpasc/277447 to your computer and use it in GitHub Desktop.
Save markpasc/277447 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""Searches for your friends on Hype Machine (hypem.com) by seeing if there are
Hype Machine users who have the same usernames as people you follow on TypePad
or Twitter."""
import logging
import sys
from time import sleep
import httplib2
import simplejson as json
import typepad
log = logging.getLogger(__file__)
def all_items(source):
"""Generates all items of a TypePad API List, traversing through subsequent
pages as necessary."""
i = 1
log.debug("Should be %d results", source.total_results)
while True:
page = source.filter(start_index=i, max_results=50)
log.debug("Yay page starting at %d is %r", i, page._location)
i += 50
if len(page):
log.debug("Yay %d items in page (first is %r)", len(page), page[0].target.preferred_username)
for item in page:
yield item
else:
log.debug("Whee returning since there were no items in page?")
return
def typepad_friends(url_id):
"""Generates all preferred usernames of TypePad accounts followed by the
user with preferred username `url_id`."""
typepad.TypePadObject.batch_requests = False
me = typepad.User.get_by_url_id(url_id)
# Re-get by XID to work around paginated list redirect XID bug.
me = typepad.User.get_by_url_id(me.xid)
log.debug("User is %r", me._location)
following = me.relationships.filter(following=True)
for rel in all_items(following):
username = rel.target.preferred_username
if username.startswith('6p'):
continue
yield username
def twitter_friends(name):
"""Generates all screen names of Twitter folks followed by the Twitter user
named `name`."""
root_url = 'http://twitter.com/statuses/friends/%s.json' % name
url = '%s?cursor=-1' % root_url
http = httplib2.Http()
while True:
resp, cont = http.request(uri=url)
if resp.status != 200:
log.warning("Unexpected status %d fetching twitter friends %r", resp.status, url)
return
friends = json.loads(cont)
for friend in friends['users']:
yield friend['screen_name']
next_cursor = friends.get('next_cursor', None)
if next_cursor:
url = '%s?cursor=%d' % (root_url, next_cursor)
else:
return
def find_hypem_user(username):
"""Prints `username` if there exists a Hype Machine user with that name."""
http = httplib2.Http()
resp, cont = http.request(uri='http://hypem.com/%s' % username, method='HEAD')
if resp.status == 200:
print username
elif resp.status == 404:
log.info("No such hypem user %r", username)
else:
log.warning("Unexpected status %d for potential hypem user %r", username)
sleep(1)
def main(argv=None):
if argv is None:
argv = sys.argv
logging.basicConfig(level=logging.WARNING)
log.setLevel(logging.INFO)
name = argv[1]
#for x in typepad_friends(name):
for x in twitter_friends(name):
find_hypem_user(x)
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment