#!/usr/bin/python
import urllib
import sys
import os
import cPickle
import re
import psyco
psyco.full()
from sets import Set
from fcntl import *
if __name__ != '__main__':
from mod_python import apache
class MyURLopener(urllib.URLopener):
version = 'http://nikita.ca/lj/clique.py (ljclique@nikita.ca)'
global myurlopen
myurlopen = MyURLopener()
def parsefoaf(name,req):
cached = False
filename = 'fdata/' + name
url = 'http://www.livejournal.com/misc/fdata.bml?user=%s' % name
if os.path.exists(filename):
f = open(filename)
fcntl(f.fileno(), LOCK_SH)
friends = cPickle.load(f)
cached = True
f.close()
else:
print >>req, 'Retrieving', url, '
'
attemptsleft = 2
while True:
try:
fdata = myurlopen.open(url)
friends = []
lines = fdata.readlines()
friendsof = [l[2:-1] for l in lines if l.startswith('< ')]
friends = [l[2:-1] for l in lines if l.startswith('> ')]
friends = [f for f in friends if f in friendsof]
break
except:
attemptsleft -= 1
if attemptsleft == 0:
req.write('
Unable to retrieve '+url+' after 2 tries, giving up.
') print_form(req) f = open(filename, 'w') flock(f, LOCK_EX) cPickle.dump(friends, f) f.close() return (friends,cached) def print_form(req): print >>req, form(os.path.basename(req.uri)) print >>req, """This may take a little while, especially if it has to retrieve a lot of friends data. If your browser times out, try reloading.
""" print >>req, 'Created by Nikita Borisov / %s, with new algorithm by %s. This entry describes how it works.
' % (lj_user('hukuma'), lj_user('ciphergoth')) print >>req, '