Skip to content

Instantly share code, notes, and snippets.

@ibaaj ibaaj/grabber.py
Last active Aug 29, 2015

Embed
What would you like to do?
facebook Graph online people / hour
import sys
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")
token="" # create a FB token (60 days)
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?"
data = {}
data['q']=sql_query
data['access_token']=token
url_values = urllib.urlencode(data)
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)
require(scales)
require(ggplot2)
file <- readLines("fb_data.txt")
# let it be csv
file <- gsub("^([0-9-]+) ([0-9:]+) - ([0-9]+)", "\\1,\\2,\\3", file)
tc <- textConnection(file)
tab <- read.csv(tc)
close(tc)
colnames(tab) <- c("day", "time", "online")
tab$day = as.Date(tab$day, "%Y-%m-%d")
tab$time = as.POSIXct(tab$time, format="%H:%M:%S")
list <- split(tab, as.factor(tab$day))
ggplot(data=tab, aes(x=tab$time,y=tab$online)) + geom_point(aes(colour = tab$online))+ scale_colour_gradient(low = "blue", name="personnes en ligne")+ geom_smooth() + ylab("Nombre de personnes en ligne") + xlab("Heure") + scale_x_datetime(labels=date_format("%H:%M"))
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.