Created
January 19, 2014 01:02
-
-
Save nikolak/8499032 to your computer and use it in GitHub Desktop.
Gets number of currently online friends on facebook
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
import webbrowser | |
import os | |
import urllib2 | |
import json | |
import urllib | |
from time import strftime, sleep | |
def write_to_file(data): | |
if os.path.exists("fb_data.txt"): | |
with open("fb_data.txt","a") as out_file: | |
out_file.write(data+"\n") | |
else: | |
with open("fb_data.txt","w") as out_file: | |
out_file.write(data+"\n") | |
app_id="1415058882068180" | |
api_url="https://www.facebook.com/dialog/oauth?client_id={}&redirect_uri=http://example.com&response_type=token&scope=friends_online_presence".format(app_id) | |
print "Press enter to open web page granting app access" | |
print "Once you allow access to data you'll be redirected to example.com page" | |
print "Just copy the whole URL you got redirected to here and press enter again" | |
print "Press enter to open web page now..." | |
raw_input() | |
webbrowser.open(api_url) | |
print "Facebook app opened" | |
url_response=raw_input("Enter url you got redirected to and press enter again") | |
token=url_response.split("#access_token=")[1].split("&expires_in")[0] | |
sql_query="SELECT uid, name FROM user WHERE online_presence IN ('active', 'idle') AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())" | |
query_url="https://graph.facebook.com/fql?" | |
request_timeout=120 #seconds | |
data = {} | |
data['q']=sql_query | |
data['access_token']=token | |
url_values = urllib.urlencode(data) | |
while True: | |
response=urllib2.urlopen(query_url+url_values) | |
json_resp=json.load(response) | |
output="{} - {}".format(strftime("%Y-%m-%d %H:%M:%S"), len(json_resp['data'])) | |
write_to_file(output) | |
print 'waiting {} seconds'.format(request_timeout) | |
sleep(request_timeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I run this script under Windows 7 x64 with Python 2.7, it doesn't writes a new line, like \n should do.
I tried several other commands like os.linesep and \r\n with no effect.
Could you make this script Windows compatible? Or I'm doing something wrong?
Also nice would be an automation of getting the access_token...
Tanks, Leo