def build_graph(): pair_list = WeiboUser.get_top_100_pair() DG = nx.DiGraph() DG.add_edges_from([(foer, twitter_user) for twitter_user, foer in pair_list]) for twitter_id in DG.nodes(): t = WeiboUser.get_by_id(twitter_id) node = DG.node[twitter_id] node['weibo_id'] = t.user_id node['label'] = t.scrn_name node['scrn_name'] = t.scrn_name node['name'] = t.name node['follower_count'] = t.foer_cnt node['friend_count'] = t.friend_cnt node['status_count'] = t.status_cnt node['description'] = t.desc node['location'] = t.location node['created_at'] = str(t.created_at) node['verified'] = t.verified node['twitter_age'] = (date.today() - t.created_at).days node['daily_tweet'] = t.status_cnt*1.0/node['twitter_age'] node['follower_count_top100'] = len([(id, foer) for id, foer in pair_list if id == twitter_id]) node['friend_count_top100'] = len([(id, foer) for id, foer in pair_list if foer == twitter_id]) return DG
def login(us): result = {} for u in us: user = WeiboUser() user.wblogin(u['username'], u['passwd']) result[u['username']] = user return result
def relationship_top(): for id in TOP_100: print 'analysis id', id sleep(1) for foer in TOP_100: if id != foer and not WeiboUser.get_relation(id, foer)\ and not WeiboUser.get_non_relation(id, foer): print 'analysis', id, 'and', foer, '...' try: e = a.exists_friendship(id,foer) except: continue if e.friends: print id, 'is followed by', foer, 'saving..' WeiboUser.save_relationship(id, foer) else: print id, 'is not followed by', foer, 'saving..' WeiboUser.save_non_relationship(id, foer) sleep(1)
def fetch(): top_user = api.friends_ids().ids for id in top_user[-2:]: u = api.get_user(id) WeiboUser.save_weibo_api(u)
def relationship(): for id in TOP_100: not_foer = WeiboUser.get_top_not_pair(id) for foer in list(set(TOP_100)-set(not_foer)): WeiboUser.save_relationship(id, foer)
from weibo_api import get_api from weibo_user import WeiboUser from time import sleep a = get_api() TOP_100 = WeiboUser.get_top_100_by_foer() def relationship(): for id in TOP_100: not_foer = WeiboUser.get_top_not_pair(id) for foer in list(set(TOP_100)-set(not_foer)): WeiboUser.save_relationship(id, foer) def relationship_top(): for id in TOP_100: print 'analysis id', id sleep(1) for foer in TOP_100: if id != foer and not WeiboUser.get_relation(id, foer)\ and not WeiboUser.get_non_relation(id, foer): print 'analysis', id, 'and', foer, '...' try: e = a.exists_friendship(id,foer) except: continue if e.friends: print id, 'is followed by', foer, 'saving..' WeiboUser.save_relationship(id, foer) else: print id, 'is not followed by', foer, 'saving..'