Beispiel #1
0
def insert_friends(uid, graph_db, client, version):
	# Getting data from twitter using API
	entry = twitter.get_friends(uid, 0, version, client)
	
	if(version==1):
		limit = int(entry['response']['x-ratelimit-remaining'])
	else:
		limit = int(entry['response']['x-rate-limit-remaining'])

	# Didn't get any followers.
	if(entry['friends'] == []):
		return [0, limit ]
	else:
		batch = neo4j.WriteBatch(graph_db)
		author = graph_db.get_indexed_node("users", "uid", str(uid))
		# this should be true as the authors profiles have been created first.
		if(author):
			friends = entry['friends']
			for fid in friends:
				batch.get_or_create_indexed_node("users","uid", fid ,{"uid" : fid,"is_author": "0"})
			nodes = batch.submit()
			# Create the relations
			for n in nodes:
				batch.get_or_create_relationship(author, "follows", n)
			rels = batch.submit()

		return [len(friends), limit]
def get_friends(version, app, f1, f2):

	CONSUMER_KEY = app['c_key']
	CONSUMER_SECRET = app['c_sec']
	ACCESS_KEY = app['a_key']
	ACCESS_SECRET = app['a_sec']
	consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
	access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
	client = oauth.Client(consumer, access_token)
	
	ret =0;count =0;limit=0

	# uid = "117373597"
	# entry = twitter.get_followers(uid,0,version,client)
	# print entry
	
	# entry = twitter.get_friends(uid,0,version,client)
	# print entry
	
	# return [ret, limit, count]

	line = f1.readline()
	while line:
		entry = json.loads(line)
		# uid = entry['author']
		uid = entry['user_id']
	# 	#GETTING FOLLOWERS FROM TWITTER by user_id
		entry = twitter.get_friends(uid,0,version,client)
		
		if(version==1):
			limit = int(entry['response']['x-ratelimit-remaining'])
		else:
			limit = int(entry['response']['x-rate-limit-remaining'])
		if(limit<3):
			endtime = datetime.now()
			ret =1
			print "limit reached"
			break
	# 	DUMP FRIENDS IDs
		f2.write(json.dumps(entry)+"\n")	
		count+=1

		line = f1.readline()

	# BREAKING LOOP BECAUSE FILES IS COMPLETE
	if(limit >=3):
		ret =2
	return [ret, limit, count]
def insert_friends(uid, client, version):
    """
	Get friends ids of author from twitter using API.
	uid : User id of the author
	graph_db : neo4j graph 
	client : client twitter API
	version : API version

	"""
    entry = twitter.get_friends(uid, 0, version, client)

    if (version == 1):
        limit = int(entry['response']['x-ratelimit-remaining'])
    else:
        limit = int(entry['response']['x-rate-limit-remaining'])

    if (entry['response']['status'] != '200'):
        return [entry, limit, 0]
    else:
        return [entry, limit, 1]
def insert_friends(uid, client, version):
	"""
	Get friends ids of author from twitter using API.
	uid : User id of the author
	graph_db : neo4j graph 
	client : client twitter API
	version : API version

	"""
	entry = twitter.get_friends(uid, 0, version, client)
	
	if(version==1):
		limit = int(entry['response']['x-ratelimit-remaining'])
	else:
		limit = int(entry['response']['x-rate-limit-remaining'])

	if(entry['response']['status'] != '200'):
		return [entry, limit, 0]
	else:
		return [entry, limit, 1]
def get_friends_parallel(conn, username):
    friends = twitter.get_friends(username)
    conn.send(friends)