def test_fql(): graph = GraphAPI('<access token>') mock_request.return_value.content = json.dumps({ 'id': 1, 'name': 'Thomas \'Herc\' Hauk', 'first_name': 'Thomas', 'last_name': 'Hauk', 'link': 'http://facebook.com/herc', 'username': '******', }) mock_request.return_value.status_code = 200 try: graph.fql('SELECT id,name,first_name,last_name,username FROM user WHERE uid=me()') except GraphAPI.FacebookError: pass mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/fql?q=SELECT+id%2Cname%2Cfirst_name%2Clast_name%2Cusername+FROM+user+WHERE+uid%3Dme%28%29', allow_redirects=True, verify=True, timeout=None, params={ 'access_token': '<access token>' } )
def test_fql(): graph = GraphAPI('<access token>') mock_request.return_value.content = json.dumps({ 'id': 1, 'name': 'Thomas \'Herc\' Hauk', 'first_name': 'Thomas', 'last_name': 'Hauk', 'link': 'http://facebook.com/herc', 'username': '******', }) mock_request.return_value.status_code = 200 try: graph.fql( 'SELECT id,name,first_name,last_name,username FROM user WHERE uid=me()' ) except GraphAPI.FacebookError: pass mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/fql?q=SELECT+id%2Cname%2Cfirst_name%2Clast_name%2Cusername+FROM+user+WHERE+uid%3Dme%28%29', allow_redirects=True, verify=True, timeout=None, params={'access_token': '<access token>'})
def test_fql(): graph = GraphAPI("<access token>") mock_request.return_value.content = json.dumps( { "id": 1, "name": "Thomas 'Herc' Hauk", "first_name": "Thomas", "last_name": "Hauk", "link": "http://facebook.com/herc", "username": "******", } ) mock_request.return_value.status_code = 200 try: graph.fql("SELECT id,name,first_name,last_name,username FROM user WHERE uid=me()") except GraphAPI.FacebookError: pass mock_request.assert_called_with( "GET", "https://graph.facebook.com/fql?q=SELECT+id%2Cname%2Cfirst_name%2Clast_name%2Cusername+FROM+user+WHERE+uid%3Dme%28%29", allow_redirects=True, verify=True, timeout=None, params={"access_token": "<access token>"}, )
def process_events(access_token): graph = GraphAPI(access_token) query_events = """ SELECT all_members_count, attending_count, creator, declined_count, description, eid, end_time, name, not_replied_count, pic, pic_big, pic_small, pic_square, privacy, start_time, ticket_uri, timezone, unsure_count, venue FROM event WHERE eid IN (SELECT eid FROM event_member WHERE (uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()))) AND start_time > now() """ events_results = graph.fql(query_events) events_objects = [] eids = [] for event_data in events_results["data"]: eid = event_data["eid"] if eid in eids: continue else: eids.append(eid) try: event = FbEvent.objects.get(pk=eid) # update event except FbEvent.DoesNotExist: print event_data event = FbEvent.objects.create_event(**event_data) events_objects.append(event) location_objects = [] for event in events_objects: if (event.venue is not None and event.venue not in location_objects): location_objects.append(event.venue) FbLocation.objects.bulk_create(location_objects) FbEvent.objects.bulk_create(events_objects) for eid in eids: process_users_in_events.delay(access_token, eid) time.sleep(3)
def findFriendShashank(self, theirname): graph = GraphAPI(self.access_token_Shashank) #graph.get('me/friends') json = graph.fql('SELECT uid, username, name, pic_square FROM user WHERE CONTAINS("Michael Moskie") and strpos(name, "Michael Moskie") >=0') for result in json['data']: if(theirname==result['name']): return(result['username']) return('whoops not found')
def facebook(lat,lon,radius): graph = GraphAPI(access_token) #Select ids of users who are friends with me, Sam Przezdziecki ids = graph.fql("SELECT uid2 FROM friend WHERE uid1 = me()") ids_data = ids['data'] ids = [] for n in range(len(ids_data)): ids.append(ids_data[n]['uid2']) #Pull geotagged posts location_posts = [] for num in ids: posts = graph.fql("SELECT latitude,longitude,id,message,\ timestamp FROM location_post WHERE \ author_uid = " + num)['data'] if len(posts) != 0: location_posts.extend(posts) #Grab posts that are within 60 km of (41.8954,-87.6243) -- i.e., the center of Chicago filtered_posts = [] for n in range(len(location_posts)): lat1 = location_posts[n]['latitude'] lon1 = location_posts[n]['longitude'] if(lat1 != None and lon1 != None): if (distance_in_km(float(lat1),float(lon1),lat,lon) < radius): filtered_posts.append(location_posts[n]) #Save posts in database s = Service.objects.get(name="facebook") for x in filtered_posts: find = Post.objects.filter(identifier = x['id'], service = s) if len(find) == 0: d0 = datetime(1970,1,1) date = d0 + timedelta(seconds = x['timestamp']) post = Post(service = s, latitude = x['latitude'],\ longitude = x['longitude'], identifier = x['id'],\ text = x['message'][:256], link = "", image= "",\ timestamp = date) post.save() print "Facebook scraped."
def facebook(lat, lon, radius): graph = GraphAPI(access_token) #Select ids of users who are friends with me, Sam Przezdziecki ids = graph.fql("SELECT uid2 FROM friend WHERE uid1 = me()") ids_data = ids['data'] ids = [] for n in range(len(ids_data)): ids.append(ids_data[n]['uid2']) #Pull geotagged posts location_posts = [] for num in ids: posts = graph.fql("SELECT latitude,longitude,id,message,\ timestamp FROM location_post WHERE \ author_uid = " + num)['data'] if len(posts) != 0: location_posts.extend(posts) #Grab posts that are within 60 km of (41.8954,-87.6243) -- i.e., the center of Chicago filtered_posts = [] for n in range(len(location_posts)): lat1 = location_posts[n]['latitude'] lon1 = location_posts[n]['longitude'] if (lat1 != None and lon1 != None): if (distance_in_km(float(lat1), float(lon1), lat, lon) < radius): filtered_posts.append(location_posts[n]) #Save posts in database s = Service.objects.get(name="facebook") for x in filtered_posts: find = Post.objects.filter(identifier=x['id'], service=s) if len(find) == 0: d0 = datetime(1970, 1, 1) date = d0 + timedelta(seconds=x['timestamp']) post = Post(service = s, latitude = x['latitude'],\ longitude = x['longitude'], identifier = x['id'],\ text = x['message'][:256], link = "", image= "",\ timestamp = date) post.save() print "Facebook scraped."
def collect_data(self,query,section,api_key): graph = GraphAPI(api_key) data = graph.fql(query) for k,v in data.items(): self.datadict['collected_data'][self.facebookID].update({section:[]}) for i in range(0,len(v),1): for k1,v1 in v[i].items(): if str(type(v1)) == "<type 'dict'>": for k2,v2 in v1.items(): data[k][i][k1][k2] = str(v2) self.datadict['collected_data'][self.facebookID][section].append(v[i])
def process_events(access_token): graph = GraphAPI(access_token) query_events = """ SELECT all_members_count, attending_count, creator, declined_count, description, eid, end_time, name, not_replied_count, pic, pic_big, pic_cover, pic_small, pic_square, privacy, start_time, ticket_uri, timezone, unsure_count, venue FROM event WHERE eid IN (SELECT eid FROM event_member WHERE (uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me()))) AND start_time > now() LIMIT 20 """ events_results = graph.fql(query_events) events_objects = [] eids = [] for event_data in events_results["data"]: eid = event_data["eid"] if eid in eids: continue else: eids.append(eid) try: event = FbEvent.objects.get(pk=eid) update_event.s(args=(event, event_data)) except FbEvent.DoesNotExist: log_event("CREATED", event_data) event = FbEvent.objects.create_event(**event_data) events_objects.append(event) FbEvent.objects.bulk_create(events_objects) for eid in eids: # process_users_in_events.delay(access_token, eid) process_users_in_events(access_token, eid)
def new(request, redirect_to="/games/"): c = {} #Context user = request.user creation_form = GameCreateForm if request.method == "POST": form = creation_form(data=request.POST) if form.is_valid(): redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) game = form.save(commit=False); game.creator = user game.group_gid = request.POST['selected-group'] game.name = request.POST[game.group_gid + '-name'] group_photo_json = request.POST[game.group_gid + '-photo'] if (group_photo_json != 'None'): group_photo_json = json.loads(group_photo_json.replace("'", '"')) #Replace single quotes with double game.group_photo = group_photo_json['source'] game.save() return HttpResponseRedirect(redirect_to) else: form = creation_form(None) c['form'] = form access_token = None for account in user.socialaccount_set.all(): access_token = str(account.socialtoken_set.get()) groups = None if (access_token != None): graph = GraphAPI(access_token) groups_data = graph.get('me/groups', paginate=False)[u'data'] #groups = sorted(groups_data, key=lambda k: k['bookmark_order']) groups = graph.fql('SELECT creator, gid, name, pic_cover FROM group WHERE gid in (select gid from group_member where uid = me()) ORDER BY update_time DESC')[u'data'] c['groups'] = groups return render(request, 'games/new.html', c,)
def have_page_permission(request, page_id, permission): graph = GraphAPI(request.session['facebook_access_token']) try: current_permission = graph.fql( 'select perms from page_admin where page_id=' + page_id + ' and uid= me()') except: return False if not current_permission['data']: return False if permission in current_permission['data'][0]['perms']: return True return False
def process_users_in_events(access_token, eid): graph = GraphAPI(access_token) query_users = (""" SELECT uid, age_range, current_address, sex FROM user WHERE uid IN (SELECT uid FROM event_member WHERE eid = %s) """) % (eid) users_results = graph.fql(query_users) ThroughModel = FbEvent.users.through users_objects = [] through_objects = [] location_objects = [] for user_data in users_results["data"]: uid = user_data['uid'] try: user = FbUser.objects.get(pk=uid) # update user except FbUser.DoesNotExist: print user_data user = FbUser.objects.create_user(**user_data) users_objects.append(user) if (user.current_address is not None and user.current_address not in location_objects): location_objects.append(user.current_address) through_props = dict( fbevent_id=eid, fbuser_id=uid) if ThroughModel.objects.filter(**through_props).exists() is False: through_objects.append(ThroughModel(**through_props)) FbLocation.objects.bulk_create(location_objects) FbUser.objects.bulk_create(users_objects) ThroughModel.objects.bulk_create(through_objects)
def facebook_authorized(resp): last_list = [] if resp is None: return 'Access denied: reason=%s error=%s' % ( request.args['error_reason'], request.args['error_description'] ) session['oauth_token'] = resp['access_token'] graph = GraphAPI(session['oauth_token']) fql_get_freinds = graph.fql('SELECT uid, pic_square, name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY mutual_friend_count DESC LIMIT 10') i=0 for user in fql_get_freinds['data']: name = user['name'] image = user['pic_square'] position = positions[i] i+=1 dict1 = {'name': name, 'image':image, 'position' : position} last_list.append(dict1) return render_template("home.html", results= last_list)
def messages(): #sorts friends by sentiment and modality of their last message to you. Returns rankings as "Friends' Happiness" and "Friends' Confidence" graph = GraphAPI(token) me = f.profile() happiness = {} confidence = {} snippets = graph.fql('SELECT snippet, snippet_author FROM thread WHERE folder_id = 0 OR folder_id = 1 Limit 10000',3) #the above code was heavily influenced by arofcoding.blogspot.com/2012/10/python-script-to-fetch-messages-from.html #returns a dictionary of message snippets along with the corresponding facebook friend IDs for dictionary in snippets['data']: #puts snippets in a dictionary where each author is mapped to the sentiment of their message happiness[sentiment(dictionary['snippet'])] = dictionary['snippet_author'] confidence[modality(dictionary['snippet'])] = dictionary['snippet_author'] #ranks dictionary entries by positivity of sentiment happiness_rankings = rank(happiness) confidence_rankings = rank(confidence) print "Friends' Happiness (low to high):" print happiness_rankings print "Friends' Confidence (low to high):" print confidence_rankings
def mood_calculate(): graph = GraphAPI( 'CAACEdEose0cBAGZB8uP6RcS48CoMCqWcbwi1r5gtBMxuOqdiOxHjJTRZCPZC7ZCLCjbiXVBAKxwwncU3XvXIiiLRl0DY3ZAm9QoPdzd8agKbOqvb7T6JsXEDD6nqnOv801SExEGNjPZCOUrP6A74bZBGn4D72H1r2chJnXopyaC6jXbgntxkKIR7elgcWDhxQJvzNvZAkRnRiYTZBzZBZCyJrKG' ) feed = graph.fql('SELECT message FROM status WHERE uid = me()') parsed_text = " " for message in feed['data']: parsed_text += message['message'].encode('ascii', 'ignore') blob = TextBlob(parsed_text) polarity = [] for sentences in blob.sentences: polarity.append(sentences.sentiment.polarity) positive_count = 0 negative_count = 0 neutral_count = 0 for values in polarity: if values > 0.0: positive_count += 1 elif values < 0.0: negative_count += 1 else: neutral_count += 1 maximum = positive_count if (neutral_count > positive_count and neutral_count > negative_count): return "Neutral" elif negative_count > positive_count: return "Negative" else: return "Positive"
def mood_calculate(): graph = GraphAPI('CAACEdEose0cBAGZB8uP6RcS48CoMCqWcbwi1r5gtBMxuOqdiOxHjJTRZCPZC7ZCLCjbiXVBAKxwwncU3XvXIiiLRl0DY3ZAm9QoPdzd8agKbOqvb7T6JsXEDD6nqnOv801SExEGNjPZCOUrP6A74bZBGn4D72H1r2chJnXopyaC6jXbgntxkKIR7elgcWDhxQJvzNvZAkRnRiYTZBzZBZCyJrKG') feed = graph.fql('SELECT message FROM status WHERE uid = me()') parsed_text = " " for message in feed['data']: parsed_text += message['message'].encode('ascii','ignore') blob = TextBlob(parsed_text) polarity = [] for sentences in blob.sentences: polarity.append(sentences.sentiment.polarity) positive_count=0 negative_count=0 neutral_count=0 for values in polarity: if values > 0.0: positive_count +=1 elif values < 0.0: negative_count +=1 else: neutral_count +=1 maximum = positive_count if (neutral_count > positive_count and neutral_count > negative_count): return "Neutral" elif negative_count > positive_count: return "Negative" else: return "Positive"
def facebook(authtoken, bac, service, lat, lon): """ Facebook data posting. Notice that the location coordinates are used with reverse search: within 50 m radius of the center point all found places are listed and the first one is chosen. """ data = dbwrapper.get_user_data(authtoken) msg = "Promilleja veressä %s ‰" % bac try: graph = GraphAPI(data[5]) if lat != None: # make a fql search for the nearby places page_id = graph.fql('SELECT page_id FROM place\ WHERE distance(latitude, longitude, \"%s\", \"%s\") < 50\ LIMIT 1' % (lat, lon)) if len(page_id['data'][0]) != 0: graph.post('/me/feed', message=msg, retry=0, place=page_id['data'][0]['page_id']) else: graph.post('/me/feed', message=msg, retry=0) else: graph.post('/me/feed', message=msg, retry=0) return True except: return False
where uid in ( select page_id from place where distance(latitude, longitude, "59.9500", "10.7500") < 50000 ) and start_time > "2012-01-01" ) and end_time < "2013-01-01" and end_time <> 'null' """ events = graph.fql(events_fql) print len(events) print type(events) event_search = """search?fields=location&q="Oslo, Norway"&type=event&since=1356994800""" sample_event = graph.get(event_search)['data'][150] sample_event_fql = """SELECT eid, name, attending_count, unsure_count, declined_count, not_replied_count, location, start_time, end_time from event where eid = """ + sample_event['id'] sample_event_info = graph.fql(sample_event_fql) print sample_event_info #Setup access tokens #Get graph results #Process results
for message in message_list: print "from: ", get_message_author(message) print "to: ", get_recipients_list(message) print "Message: ", get_message_body(message) print "-" * 140 #Creating facebook GraphAPI object using the long live token. graph = GraphAPI(LONG_LIVE_ACCESS_TOKEN) #Output of the facebook query language(FQL) #This FQL queries for message body, author, recipients for unread messages. try: json_output = graph.fql( 'SELECT snippet, snippet_author, recipients FROM thread WHERE folder_id = 0 and unread != 0 Limit 4' ) except exceptions.OAuthError as e: print e.message sys.exit(0) #Extracting the data content part from the returned json response. The data part will #contain snippet, author, recepients. message_list = json_output['data'] #Checking if empty or not. if message_list: pretty_print(message_list) else: print "No New Messages" sys.exit(0)
class FacebookPhoto(object): def __init__(self, access_token): self.access_token = access_token self.graph = GraphAPI(self.access_token) def photos(self): query = {'query': 'SELECT object_id, pid, aid, images, caption, position FROM photo WHERE owner=me() or pid in (SELECT pid FROM photo_tag WHERE subject=me()) LIMIT 1500'} return self._fql_result(self.graph.fql(query)) def album(self, aid): query = {'query': 'SELECT object_id, type, owner, name, description, size, link, photo_count FROM album WHERE aid="'+str(aid)+'"'} return self._fql_result(self.graph.fql(query)) def album_user(self, userid): query = {'query': 'SELECT name FROM user WHERE uid = "'+str(userid)+'"'} return self._fql_result(self.graph.fql(query)) def album_photos(self, aid): query = {'query': 'SELECT object_id, pid, aid, images, caption, position FROM photo WHERE aid="'+ str(aid) +'" LIMIT 1000'} return self._fql_result(self.graph.fql(query)) def albums(self): print "Getting your tagged photos albums..." albums = set() for photo in self.photos(): albums.add(photo['aid']) results = [] i = 1 num_albums = len(albums) for aid in albums: print "\t(%d, %d) grabbing album id %s" % (i, num_albums, aid) album_data = {'data': {}, 'photos': {}} try: album = self.album(aid) if album != []: album_data['data'] = album[0] userid = album_data['data']['owner'] album_data['user'] = self.album_user(userid)[0] album_data['data']['name'] = self._augment_album_name(album_data['data']['name'], album_data['user']['name']) album_data['photos'] = self.album_photos(aid) results.append(album_data) except FacepyError as e: print "Error Retreiving album_id: %s" % aid i += 1 return results def _augment_album_name(self, album_name, augment): if album_name == 'Mobile Uploads': album_name = augment + ' Mobile Uploads' elif album_name == 'Profile Pictures': album_name = augment + ' Profile Pictures' else: album_name += ' (' + augment + ')' return album_name def _fql_result(self, result): return result['data'][0]['fql_result_set']
def postProcessing(userid, accessToken): userloggedin = UserTomonotomo.objects.get(userid=userid) graph = GraphAPI(accessToken) logger.debug("social_auth_pipeline.postProcessing - Processing " + accessToken + " - " + str(userid)) try: ## Checking if accesstoken is valid friendnumber = graph.fql('SELECT friend_count FROM user where uid=me()') numberfriends = friendnumber.get('data')[0].get('friend_count') logger.debug("social_auth_pipeline.postProcessing - number of friends " + str(numberfriends)) except Exception as e: logger.exception("social_auth_pipeline.postProcessing - Accesstoken is invalid - " + str(e) + " - " + str(e.args)) raise friendgraphdata= [] friendgraph= [] for i in range(0,10): query = 'SELECT uid,first_name,last_name,username,name,birthday,education,work,sex,hometown_location,current_location,relationship_status FROM user WHERE uid in (SELECT uid2 FROM friend where uid1=me() limit '+str(max(0,(i*500)-100))+',500)' friendgraphoutput = graph.fql(query) if friendgraphoutput: friendgraphdata.append(friendgraphoutput) try: friendgraph.extend(friendgraphoutput.get('data')) logger.debug("social_auth_pipeline.postProcessing - received data for " + str(len(friendgraph)) + " friends - some are duplicate") except: pass logger.debug("social_auth_pipeline.postProcessing - list of friends data "+str(len(friendgraph))+" - some are duplicate") count = 0 fqlerror = 0 for frienddata in friendgraph: count = count + 1 try: logger.debug("social_auth_pipeline.postProcessing - Saving detailed information for friendid - " + str(frienddata.get('uid')) + " - count " + str(count)) except: logger.exception("social_auth_pipeline.postProcessing - Saving detailed information for friendid without uid - " + str(frienddata) + " - count " + str(count)) try: profilefriends = UserFriends.objects.get(userid=userloggedin, friendid=frienddata.get('uid')) except UserFriends.DoesNotExist: profilefriends = UserFriends() profilefriends.userid = userloggedin profilefriends.friendid = frienddata.get('uid') try: if UserFriends.objects.filter(userid=userloggedin, friendid=frienddata.get('uid')).count()==0: profilefriends.save() except Exception as e: logger.exception("social_auth_pipeline.postProcessing - Exception saving userfriends data - error - " + str(frienddata.get('uid')) + " - " + str(e.args)) pass except UserFriends.MultipleObjectsReturned: logger.exception("social_auth_pipeline.postProcessing - Exception saving userfriends data - multiple entries in userfriends race error " + str(frienddata.get('uid'))) pass except Exception as e: try: logger.exception("social_auth_pipeline.postProcessing - Exception saving UserFriends - " + str(frienddata) + " " + str(e.args)) except: pass logger.exception("social_auth_pipeline.postProcessing - Exception saving UserFriends - " + str(e) + " - " + str(e.args)) raise try: userfriend = UserTomonotomo.objects.get(userid=frienddata.get('uid')) except UserTomonotomo.DoesNotExist: userfriend = UserTomonotomo() userfriend.userid = frienddata.get('uid') try: if frienddata.get('work'): userfriend.work= getSanitizedWork(frienddata['work']) if frienddata.get('education'): userfriend.education= getSanitizedEducation(frienddata['education']) userfriend.first_name = frienddata.get('first_name') userfriend.last_name = frienddata.get('last_name') try: userfriend.gender = genderdict[frienddata.get('sex') or "not specified"] except: userfriend.gender = genderdict["not specified"] if frienddata.get('hometown_location'): userfriend.hometown = frienddata.get('hometown_location').get('name') if frienddata.get('current_location'): userfriend.location = frienddata.get('current_location').get('name') saveLocation(frienddata.get('current_location').get('id')) try: userfriend.relstatus = relstatusdict[frienddata.get('relationship_status') or "not specified"] except: userfriend.relstatus = relstatusdict["not specified"] userfriend.username = frienddata.get('username') if frienddata.get('birthday'): try: userfriend.birthday = time.strftime("%m/%d/%Y", time.strptime(frienddata.get('birthday'), "%B %d, %Y")) except: pass try: # TODO: Gets interests only if interests are not there. Need to change this. if (not userfriend.interests) and (not fqlerror): query = 'SELECT page_id, name FROM page WHERE page_id IN (SELECT page_id FROM page_fan WHERE uid=' + str(frienddata.get('uid')) + ')' userfriend.interests = pickle.dumps(graph.fql(query).get('data')).decode('latin1') except Exception as e: fqlerror = 1 logger.exception("social_auth_pipeline.postProcessing - Error getting friends' likes - " + str(frienddata.get('uid')) + " - " + str(e.args)) pass except Exception as e: logger.exception("social_auth_pipeline.postProcessing - Error collecting information - " + str(e) + " - " + str(e.args)) raise try: if UserTomonotomo.objects.filter(userid=frienddata.get('uid')).count() == 0: userfriend.save() except Exception as e: logger.exception("social_auth_pipeline.postProcessing - Error saving userdata - " + str(e) + " - " + str(e.args)) pass return
def create_custom_user(backend, details, user=None, user_exists=UserSocialAuth.simple_user_exists, *args, **kwargs): logger.debug("social_auth_pipeline.create_custom_user - Creating Custom User") ## TODO: Make not updating condition stricter. stop only if (not new) and (updated in last 10 days) if kwargs['is_new'] == False: logger.debug("social_auth_pipeline.create_custom_user - Returning user " + str(user)) else: logger.debug("social_auth_pipeline.create_custom_user - Getting data for first time user " + str(user)) if user is None: logger.exception("social_auth_pipeline.create_custom_user - User came as None in the function create_custom_user") return if backend.__class__ != FacebookBackend: return res = kwargs['response'] logger.debug("social_auth_pipeline.create_custom_user - Getting/Updating data for userid " + res.get('id')) logger.debug("social_auth_pipeline.create_custom_user - " + str(res)) try: profile = UserTomonotomo.objects.get(userid=res.get('id')) except UserTomonotomo.DoesNotExist: profile = UserTomonotomo() profile.accesstoken = res.get('access_token') profile.expiresin = res.get('expires') if res.get('work'): profile.work= getSanitizedWork(res['work']) if res.get('education'): profile.education= getSanitizedEducation(res['education']) profile.email = res.get('email') if not res.get('email'): profile.email = str(res.get('username'))+"@facebook.com" logger.info("social_auth_pipeline.create_custom_user - Email not found for user " + str(res.get('id')) + ". Using " + profile.email) if not res.get('username'): logger.exception("social_auth_pipeline.create_custom_user - Critical - Could neither get email nor [email protected] for user " + str(res.get('id'))) profile.first_name = res.get('first_name') profile.last_name = res.get('last_name') profile.gender = genderdict[res.get('gender') or "not specified"] if res.get('hometown'): profile.hometown = res.get('hometown').get('name') if res.get('location'): profile.location = res.get('location').get('name') saveLocation(res.get('location').get('id')) if res.get('relationship_status'): profile.relstatus = relstatusdict[res.get('relationship_status') or "not specified"] profile.username = res.get('username') profile.userid = res.get('id') # "----" graph = GraphAPI(res.get('access_token')) responsegraph = graph.get(str(res['id'])+'?fields=birthday,likes') profile.birthday = str(responsegraph.get('birthday')) try: if responsegraph.get('likes'): # TODO: Gets interests only if interests are not there. Need to change this. if not profile.interests: profile.interests = pickle.dumps(extractAllSanitizedLikes(responsegraph.get('likes'))).decode('latin1') except Exception as e: logger.exception("social_auth_pipeline.create_custom_user - Error getting likes for user " + str(res.get('id'))) profile.save() # "----" userloggedin = UserTomonotomo.objects.get(userid=res['id']) # "----" try: userprocessing = UserProcessing.objects.get(userloggedin=userloggedin) except UserProcessing.DoesNotExist: userprocessing = UserProcessing() userprocessing.userloggedin = userloggedin except UserProcessing.MultipleObjectsReturned: userprocessing = userProcessing() userprocessing.userloggedin = userloggedin userprocessing.accesstoken = res.get('access_token') userprocessing.save() # "----" try: userquota = UserQuota.objects.get(userid=res.get('id')) except UserQuota.DoesNotExist: userquota = UserQuota() userquota.userid = res.get('id') userquota.quota=20 userquota.save() # "----" friendlist = graph.fql('SELECT uid2 FROM friend where uid1=me()') peopleontnt = UserTomonotomo.objects.values('userid') friendsontnt = list(set(map(lambda x: int(x['uid2']), friendlist.get('data'))) & set(map(lambda x: x['userid'], peopleontnt))) for friendontnt in friendsontnt: try: profilefriends = UserFriends.objects.get(userid=userloggedin, friendid=friendontnt) except UserFriends.DoesNotExist: profilefriends = UserFriends() profilefriends.userid = userloggedin profilefriends.friendid = friendontnt try: profilefriends.save() except Exception as e: logger.exception('social_auth_pipeline.create_custom_user - error - seeing which friends are on tnt ' + str(e)) pass if kwargs['is_new']==False: logger.debug("social_auth_pipeline.create_custom_user - completed for returning user " + str(res.get('id'))) return else: logger.debug("social_auth_pipeline.create_custom_user - completed for first time user " + str(userloggedin)) friendsregisteredontnt = list(set(friendsontnt) & set(map(lambda x: x['userid'], UserTomonotomo.objects.exclude(email=None).values('userid')))) shuffle(list(set(friendsregisteredontnt))) for friendontnt in []: try: dbutils.sendemailnotification(friendontnt, "One of your friends just joined tomonotomo", "One of your friends just joined tomonotomo to meet interesting friends of friends. We'll keep the name of your friend to ourselves out of respect for his privacy. A new friend is a good news for you as your friend of friend network just increased by "+ str(400 + randint(0,200)) + ", and you have a larger pool of potential dates. Congratulations and visit www.tomonotomo.com right away!") except Exception as e: logger.exception("social_auth_pipeline.create_custom_user - error in sendemailnotification " + str(e) + str(friendontnt)) pass # "----" return
def get_messages(message_list): for message in message_list: if message: print "FROM" print " " + get_name_from_uid(message['snippet_author']) print "RECIPIENT(S)" print print_recipients(message['recipients'], message) print "BODY: " + message['snippet'] print "-" * 40 + '\n' # Initialize the Graph API with a valid access token oauth_access_token = 'your_access_token_goes_here_in_single_quotes' graph = GraphAPI(oauth_access_token) # FQL: get all messages from inbox fql_get_messages = graph.fql( 'SELECT snippet, recipients, snippet_author, updated_time FROM thread WHERE folder_id = 1 and unread != 0') # Get the data of the messages in a list message_list = fql_get_messages['data'] # print the messages if message_list: get_messages(message_list) else: print "Inbox is Empty"
def get_thread(thread_list): return thread_list['thread_id'] graph = GraphAPI(LONG_LIVE_ACCESS_TOKEN) try: json_thread = graph.fql('SELECT thread_id,recipients FROM thread WHERE folder_id = 1 limit 10 ') except exceptions.OAuthError as e: print e.message sys.exit(0) thread_list=json_thread['data'] for t in thread_list: try: json_output = graph.fql('select body,viewer_id,author_id from message where thread_id = ' + get_thread(t) + 'and author_id= me()') message_list = json_output['data'] for m in message_list: print get_message_viewer(m,t)," ",get_message_body(m) print "="*100
from facepy import GraphAPI access_token = "CAACEdEose0cBAOGb8xQKDDGt9mEQ9qZCCsYxcSb0uZC3VkWmh27814iyhGgRLSFyL6D5jVB6d6zZBRBsmCjFtZCfua4zLNwi7sQZCD47nPNLn1nK0r6WXaHeFCx2CSXcZCAe6qbw68ZBWvzzEmHZCCR30kdoA6XGB2rk8NOZBRmOjyJBJ2UEJsMUYuyV5rPZCKZAtkZD"; graph = GraphAPI(access_token) #writer = csv.writer(sys.stdout) # Make a FQL query #g = graph.fql('SELECT name FROM user WHERE uid = me()') # Make a FQL multiquery g = graph.fql({ 'link_status': 'SELECT comments_fbid FROM link_stat WHERE url = "www.liangfang.us/test/test.html"', 'likes': 'SELECT user_id FROM like WHERE object_id IN (SELECT comments_fbid FROM #link_status)', 'users': 'SELECT name, profile_url FROM user WHERE uid IN (SELECT user_id FROM #likes)' }) #print g['data'][2] users = g['data'][2] csvfile = file('all_user.csv', 'wb') writers = csv.writer(csvfile) #writers.writerow(['profile_url']) #print users['fql_result_set'] for user in users['fql_result_set']: print user['profile_url']
class facebook_client: def __init__(self): self.login(config.user_id, config.user_pass) self.graph = GraphAPI(LONG_LIVE_ACCESS_TOKEN) json_output = self.graph.fql( 'SELECT uid, name FROM user WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me())') self.friendlist = {} for friend in json_output['data'][:]: self.friendlist[friend['uid']] = friend['name'] self.friendlist[1011722861] = 'Jonathan Piat' def getLatestStreamForAll(self): msg = {} for k in self.friendlist: msg[self.friendlist[k]] = self.getLatestStream(k) return msg def getLatestStream(self, user): try: json_output = self.graph.fql('SELECT source_id, post_id, message, type, attachment, updated_time FROM stream WHERE source_id='+str(user)+' order by created_time') except exceptions.OAuthError as e : raise FaceBookException(str(e)) cts = time.time() ret_msg = [] nb = 0 for msg in reversed(json_output['data'][:]): msg_uid = msg['source_id'] #print msg['type'] try: ret_msg.append(options[msg['type']](msg, self.friendlist)) except KeyError: #print 'No key for '+str(msg['type']) pass nb = nb + 1 if nb > 2 : return ret_msg return ret_msg def login(self, user, password): cj = cookielib.LWPCookieJar('/root/facebook.ck') try: cj.load() print 'cookie loaded from file' return except cookielib.LoadError: print 'cookie can not be loaded' except IOError: print 'cookie file does not exist' opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('Referer', 'http://login.facebook.com/login.php'), ('Content-Type', 'application/x-www-form-urlencoded'), ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)')] data = "locale=en_US&non_com_login=&email="+user+"&pass="******"&lsd=20TOl" usock = opener.open('http://www.facebook.com') usock = opener.open(LOGIN_URL, data) if "Logout" in usock.read(): cj.save() print "Logged in." else: print "failed login" print usock.read()
# Initialize the Graph API with a valid access token. #Generate access token here: https://developers.facebook.com/tools/explorer/. Make sure you choose the API version 1.0 oauth_access_token = 'Enter access token here' graph = GraphAPI(oauth_access_token) #Will be used to launch a web browser and view the output filepath = 'file:///FBHeatMap.html' #Initialise a list to store the locations of friends. myLocations=[] #A string to hold the JS array arguments that will be used later to create the heatmap. htmlCoords="" #Fetch the locations using FQL locations=graph.fql('SELECT uid,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())') for friend in locations['data']: if friend['current_location'] is not None: if friend['current_location']['name'] in myLocations: pass else: city=friend['current_location']['name'] myLocations.append(city) lat=friend['current_location']['latitude'] lon=friend['current_location']['longitude'] #Format of JS: new google.maps.LatLng(X,X) htmlCoords+='new google.maps.LatLng('+str(lat)+","+str(lon)+")," #To remove the last comma which gets added by iteration of the previous statement. jsInput = htmlCoords[:-1]
def fetch_pictures(): """ Fetch pictures every 10 minutes """ oauth_access_token = 'BAACEdEose0cBAFZCtR47HwYA7A9ZAOuekN4EZCQLDyQfyZA19uf0K4xIa8oBnrq8e8GdTcs80hpN5ZAoxzlHrRlvmXTJRdQCPtZAaD3IMrehFTL7kkTjADCDekwjLxfuK7M8EVMZBrrocwqfj8vrfHaRaiIaPoXBRoiTEJB2BeRxVZC7HZBc8luPE1k0phZB3jTR5K2An27MVx7YKT8ZA0gMD1DcuJ0XZAeROIyOVA4wUqSiqQZDZD' graph = GraphAPI(oauth_access_token) # get last album from facebook album = graph.get('me/albums?limit=1') albums = models.Album.objects.all().order_by('-create_at')[:1] # photos fql query full_album = 12 limit = 5 photos_fql = "SELECT src, created, modified FROM photo WHERE album_object_id = '{}' ORDER BY created DESC LIMIT {}" if albums and album['data']: album_id = album['data'][0].get('id') pictures_album = albums[0] pictures = pictures_album.pictures() count_pics = pictures.count() if count_pics == full_album: # Create new album pictures_album = models.Album(name=album['data'][0].get('name')) pictures_album.save() count_pics = 0 # Send email send_mail('Album is full email', 'Album {} is full with {} pictures'.format(albums[0].name, count), settings.EMAIL_HOST_USER, ['*****@*****.**'], fail_silently=False) else: if count_pics > 7: limit = full_album - count_pics # Get pictures photos = graph.fql(photos_fql.format(album_id, limit)) for index, photo in enumerate(photos['data']): picture = models.Picture() picture.album = pictures_album picture.name = "picture_{}".format(count_pics + index + 1) picture.path.save(picture.name + '.jpg', get_image_from_url(photo.get('src')), save=True) picture.thumbnail = picture.path picture.save() pictures = pictures_album.pictures() # Set album cover if pictures_album.cover.name == 'img/no-cover-picture.png' and pictures: pictures_album.cover = pictures[0].thumbnail pictures_album.save() elif album['data']: # No albums album_id = album['data'][0].get('id') pictures_album = models.Album(name=album['data'][0].get('name')) pictures_album.save() photos = graph.fql(photos_fql.format(album_id, limit)) for index, photo in enumerate(photos['data']): picture = models.Picture() picture.album = pictures_album picture.name = "picture_{}".format(count_pics + index) picture.path.save(picture.name + '.jpg', get_image_from_url(photo.get('src')), save=True) picture.thumbnail.save(picture.name + '.jpg', get_image_from_url(photo.get('src')), save=True) picture.save() pictures = pictures_album.pictures() # Set album cover if pictures: pictures_album.cover = pictures[0].thumbnail pictures_album.save()
from motionless import * import urllib2 # Initialize the Graph API with a valid access token #Generate access token here: https://developers.facebook.com/tools/explorer/ oauth_access_token = 'Enter access token here' graph = GraphAPI(oauth_access_token) #Initialise the output map dmap = DecoratedMap() #Initialise a list for the locations. friendLocations=[] #Get friends' locations using FQL. locations=graph.fql('SELECT uid,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())') for location in locations['data']: if location['current_location'] is not None: if location['current_location']['name'] in friendLocations: pass else: placeMark=location['current_location']['name'] dmap.add_marker(AddressMarker(placeMark,label='')) friendLocations.append(placeMark) print placeMark #Static Map URLs can only be of 2048 characters or less. try: mapurl = dmap.generate_url() except: break
class facebook_client: def __init__(self): self.login(config.user_id, config.user_pass) self.graph = GraphAPI(LONG_LIVE_ACCESS_TOKEN) json_output = self.graph.fql( 'SELECT uid, name FROM user WHERE uid IN (SELECT uid1 FROM friend WHERE uid2=me())' ) self.friendlist = {} for friend in json_output['data'][:]: self.friendlist[friend['uid']] = friend['name'] self.friendlist[1011722861] = 'Jonathan Piat' def getLatestStreamForAll(self): msg = {} for k in self.friendlist: msg[self.friendlist[k]] = self.getLatestStream(k) return msg def getLatestStream(self, user): try: json_output = self.graph.fql( 'SELECT source_id, post_id, message, type, attachment, updated_time FROM stream WHERE source_id=' + str(user) + ' order by created_time') except exceptions.OAuthError as e: raise FaceBookException(str(e)) cts = time.time() ret_msg = [] nb = 0 for msg in reversed(json_output['data'][:]): msg_uid = msg['source_id'] #print msg['type'] try: ret_msg.append(options[msg['type']](msg, self.friendlist)) except KeyError: #print 'No key for '+str(msg['type']) pass nb = nb + 1 if nb > 2: return ret_msg return ret_msg def login(self, user, password): cj = cookielib.LWPCookieJar('/root/facebook.ck') try: cj.load() print 'cookie loaded from file' return except cookielib.LoadError: print 'cookie can not be loaded' except IOError: print 'cookie file does not exist' opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [ ('Referer', 'http://login.facebook.com/login.php'), ('Content-Type', 'application/x-www-form-urlencoded'), ('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)' ) ] data = "locale=en_US&non_com_login=&email=" + user + "&pass="******"&lsd=20TOl" usock = opener.open('http://www.facebook.com') usock = opener.open(LOGIN_URL, data) if "Logout" in usock.read(): cj.save() print "Logged in." else: print "failed login" print usock.read()
from facepy import GraphAPI access_token = "" graph = GraphAPI(access_token) # Get my latest posts graph.get('me/posts') # Post a photo of a parrot #graph.post( path = 'me/photos', source = open('parrot.jpg') ) # Make a FQL query graph.fql('SELECT name FROM user WHERE uid = me()') # Make a FQL multiquery graph.fql({ 'rsvp_status': 'SELECT uid, rsvp_status FROM event_member WHERE eid=12345678', 'details': 'SELECT name, url, pic FROM profile WHERE id IN (SELECT uid FROM #rsvp_status)' }