def graph(config, user_id, depth, max_follow, username_file, graph_file): ''' Get follower/following graph. Starting with the seed user identified by USER_ID, construct a directed follower/following graph by traversing up to --depth hops (default: 1). For each user, fetch only --max-follow followers and followees. ''' # Get username for this user_id. endpoint = '/users/{}'.format(user_id) response = _get_instagram(config, endpoint) if response.status_code != 200: raise click.ClickException( 'Unable to fetch user information: {} {}' .format(response.status_code, response.payload['meta']['error_message']) ) user_info = json.loads(response.text) username = user_info['data']['username'] # Get graph. node_fn = functools.partial(_get_graph, config, max_follow) users, graph = get_graph(node_fn, {user_id: username}, depth) write_users(users, username_file) write_graph(graph, graph_file) click.secho('Finished: {} nodes'.format(len(users)))
def graph(config, depth, max_follow, username, username_file, graph_file): ''' Get a twitter user's friends/follower graph. ''' session = _login_twitter(config) # Get user ID. home_url = '{}/{}'.format(config.twitter_url, username) response = session.get(home_url) if response.status_code != 200: raise click.ClickException('Not able to get home page for {}. ({})' .format(username, response.status_code)) html = bs4.BeautifulSoup(response.text, 'html.parser') profile_el = html.select('.ProfileNav-item--userActions .user-actions')[0] user_id = profile_el['data-user-id'] # Get graph. node_fn = functools.partial(_get_graph, session, config, max_follow) users, graph = get_graph(node_fn, {user_id: username}, depth) write_users(users, username_file) write_graph(graph, graph_file) click.secho('Finished: {} nodes'.format(len(users)))
def algorithm_basic( g, n ): threshold = 10 h = None current_density = -1 while( g.number_of_nodes() > 1 ): deg = g.degree( weight = 'weight' ) g.remove_node( min(deg, key=deg.get) ) temp_density = density(g) if ( temp_density > current_density ) & ( g.number_of_nodes() <= threshold ) & ( g.number_of_nodes() >= 2 ) : h = nx.Graph(g) current_density = temp_density u.write_graph(h, n) return h
termsfile = args.filename relationfile = args.relationfile relationtype = args.relationtype outdir = args.outdir minscore = args.minscore p_lambda = args.p_lambda logdir = args.logdir noise = args.noise wn = args.wn entities = util.read_terms(termsfile) pruned, stats = mtg(entities, relationfile, reltype=relationtype, minscore=minscore, lmda=p_lambda, noise=noise, wn=wn) bn = os.path.basename(termsfile) util.write_taxo(pruned, fname=os.path.join(outdir, bn.replace('.terms', '.taxo'))) util.write_graph(pruned, fname=os.path.join(outdir, bn.replace('.terms', '.graph'))) util.write_log(pruned, stats, fname=os.path.join( logdir, 'mtg.terms-%s.rel-%s.minsc%0.1f.lambda-%0.1f' % (bn, relationtype, minscore, p_lambda)))