from dateutil.parser import parse
from datetime import datetime
import re
import urlparse
from src.fb import graph
from facebook import GraphAPIError
import json
def fbfeedelement(h):
art={}
art["text"]=""
if h.has_key("story"):
art["text"]=art["text"]+h["story"]+"
"
if h.has_key("attachments") and len(h["attachments"]["data"])>0:
for a in h["attachments"]["data"]:
if a.has_key("media") and a["media"].has_key("image") and a["media"]["image"].has_key("src") and not art.has_key("image"):
art["image"]=a["media"]["image"]["src"]
if a.has_key("title"):
art["title"]=a["title"]
if a.has_key("type") and a["type"] in ["event"]:
art["url"]=a["url"]
if a.has_key("description"):
art["text"]=art["text"]+a["description"]+"
"
if not art.has_key("title") and h.has_key("story"):
art["title"]=h["story"]
if h.has_key("message"):
art["text"]=art["text"]+h["message"]
art["published"] =parse(h["created_time"])
if not art.has_key("url"):
art["url"]=urlparse.urlunsplit(("http","www.facebook.at",h["id"],"",""))
return art
def fbfeed(url, raw):
js = json.loads(raw)
arts=[]
u=urlparse.urlparse(url)
for m in js["data"]:
aa=fbfeedelement(m)
if not aa.has_key("title"):
aa["title"] = u[1]+ " at " + aa["published"].strftime("%Y-%m-%d %H:%M")
aa["section"]="Facebook: "+u[1]
arts.append(aa)
nx=None
if js.has_key("paging") and js["paging"].has_key("next"):
nx=js["paging"]["next"]
return {"url": url, "next_page": nx,"articles": arts}