コード例 #1
0
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)))
コード例 #2
0
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)))