Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Noisebridge PyClass Homework
import urllib2
from lxml.html import fromstring
import json
def collate_data():
print "beginning collating..."
# get the list of users from noisebridge wiki
# and put them in a dictionary with userid as key
nb_url = "https://www.noisebridge.net/index.php?title=Special:ListUsers&offset=&limit=10"
fp = urllib2.urlopen(nb_url)
data = fp.read()
fp.close()
doc = fromstring(data)
names = doc.xpath("//div[@id='mw-content-text']/ul/li/a")
"""
TODO: Lookup the list of users that the noisebridge twitter account
follows and save that data in a list for later.
"""
nb_follows_url = "https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=noisebridge"
people = {}
for name in names:
uid = name.text
if name not in people:
people[uid] = {'nb_id': uid,
'nb_following': False,
'is_following_nb': False,
'gender': None,
'location': '',
'twitter_id': '',
'facebook_id': '',
'name' : '',
}
# for each user, try to find their facebook info.
# base FB url is https://graph.facebook.com/<username>
# if they are a FB user, and in the US, collect their
# info in the dictionary
for uid, user in people.items():
fb_url = 'https://graph.facebook.com/%s' % user['nb_id']
print fb_url
try:
fp = urllib2.urlopen(fb_url)
fb_data = fp.read()
fp.close()
except urllib2.HTTPError:
continue
try:
fb_obj = json.loads(fb_data)
except:
continue
user['name'] = fb_obj['name']
user['gender'] = fb_obj['gender']
user['location'] = fb_obj['locale']
user['facebook_id'] = fb_obj['id']
"""
TODO: continue going to see if each user has a twitter account
add that info here. The base url for gathering twitter info
is below and you will need to replace with their name, being
sure to catch any bad username.
If they have a username, check to see if they are following
noisebridge and if noisebridge is following them. noisebridges
twitter id is 16460799.
"""
twittter_user_url = "https://api.twitter.com/1/users/lookup.json?screen_name=noisebridge&include_entities=true"
print people
# print a list of people that noisebridge should follow on
# twitter. bonus if you figure out the facebook api page and do the same
# there
if __name__ == "__main__":
collate_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.