当前位置: 首页>>代码示例>>Python>>正文


Python Twitter.get_friends方法代码示例

本文整理汇总了Python中twitter.Twitter.get_friends方法的典型用法代码示例。如果您正苦于以下问题:Python Twitter.get_friends方法的具体用法?Python Twitter.get_friends怎么用?Python Twitter.get_friends使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twitter.Twitter的用法示例。


在下文中一共展示了Twitter.get_friends方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: post

# 需要导入模块: from twitter import Twitter [as 别名]
# 或者: from twitter.Twitter import get_friends [as 别名]
    def post(self):
        """Handle the POST method
        """
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        try:
            username = escape(self.request.get('username'))
            password = escape(self.request.get('password'))
            logging.info("Handling request for %s" % username)
            t = Twitter(username, password)
            friends = set([f['screen_name'] for f in t.get_friends()])
            followers = set([f['screen_name'] for f in t.get_followers()])
            to_follow = followers.difference(friends)

            try:
                for user in to_follow:
                    try:
                        t.friendship_create(user, True)
                        logging.info("%s now follows %s" % (username, user))
                    except DownloadError:
                        logging.warning("Download error when %s tried to follow %s" % (username, user))
                        raise

                self.response.out.write(template.render(path, {"success": True}))

            except Exception, e:
                logging.warning("Caught an exception %s when %s tried to follow %s: %s" % (e, username, user))
                raise

        except DeadlineExceededError:
            self.response.out.write(template.render(path, {"error": True}))
开发者ID:ghoseb,项目名称:followtwits,代码行数:32,代码来源:followtwits.py

示例2: test_get_friends

# 需要导入模块: from twitter import Twitter [as 别名]
# 或者: from twitter.Twitter import get_friends [as 别名]
    def test_get_friends(self):
        tw = Twitter(**self.SETTINGS)
        print("Twitter object: ", tw)
        tw.get_access_token()
        user = tw.user_lookup(screen_name='neokluber')
        uid = user[0]['id_str']

        friend_list = tw.get_friends(uid)
        print("Friends list: ", friend_list)
        assert isinstance(friend_list, list)
        assert friend_list
开发者ID:joyanujoy,项目名称:twitter-graph,代码行数:13,代码来源:test_twitter.py

示例3: main

# 需要导入模块: from twitter import Twitter [as 别名]
# 或者: from twitter.Twitter import get_friends [as 别名]
def main(filename, screen_name):
    """
    filename : json settings file see the sample file in repository
    screen_name : screen name of a twitter user
    """

    with open(filename, 'r') as f:
        settings = json.load(f)

    twitter = Twitter(**settings)
    twitter.get_access_token()

    # Graph structure { node_a: [connected_node_1, connected_node_2,..]}
    graph = {}

    self_limit(twitter, 'users/lookup', 3)
    try:
        uid = call_api(twitter.user_lookup(screen_name=screen_name),
                       3)[0]['id_str']
    except Exception:
        logging.debug("Error getting twitter userid: " + screen_name)
        raise

    self_limit(twitter, 'friends/ids', 3)
    try:
        graph[uid] = call_api(twitter.get_friends(uid), 3)
    except Exception:
        logging.debug("Error getting friends list")
        raise

    # Get friends' friends and add to nodes; graph[friend] = [their friends]
    for friend in graph[uid]:
        self_limit(twitter, 'friends/ids', 3)
        try:
            graph[friend] = call_api(twitter.get_friends(friend), 3)
        except Exception:
            logging.debug("Error getting friends' friends!")
            raise
        # No point in hitting twitter too fast
        sleep(10)

    # Find indegree of connected nodes and build a frequency table. The freq
    # table can be used to idenfity nodes to include in the graph. ie include
    # only those users with at least 2 followers within the network.
    freq = {}
    freq[uid] = 0  # because main user may not appear in the below list
    for node in [x for y in graph.values() if y is not None for x in y]:
        if node in freq:
            freq[node] += 1
        else:
            freq[node] = 1

    # Add main user and friends to a list. Indices of elements in this list
    # will be used later to build graph node source->target table for d3.js
    node_list = graph.keys()

    # Add friends of friends to node list if they're friends with more than 1
    for node in [x for x in freq if freq[x] > 5]:
        if node not in node_list:
            node_list.append(node)

    logging.info(('Found %s vertices for the graph') % (len(node_list)))

    # Get user's name and screen name; store it in a dict for lookup later
    # bunch 100 ids into a comma separated query string
    n = 0
    query_str = ''
    user_details = {}

    for user in node_list:
        n += 1
        query_str += user + ","
        if (n % 100 == 0) or n == len(node_list):

            try:
                r = call_api(twitter.user_lookup(id=query_str), 3)
            except Exception:
                logging.debug("Error getting details for: " + user)
                raise

            for rec in r:
                # user_details[user_id] = [name, screen_name].
                user_details[rec['id_str']] = [rec.get('name'),
                                               rec.get('screen_name')]
            query_str = ''

    # Build json for d3.js
    # {
    #    "nodes" : [
    #                {"name": Name, "scr_name": Screen Name, "degree": Degree},
    #                {"name": Name, "scr_name": Screen Name, "degree": Degree},
    #            ],
    #    "links" : [
    #                {"source": index of source, "target": index of target},
    #                {"source": index of source, "target": indext of target}
    #            ]
    # }

    d3_json = {
        "nodes": [],
#.........这里部分代码省略.........
开发者ID:joyanujoy,项目名称:twitter-graph,代码行数:103,代码来源:graph.py


注:本文中的twitter.Twitter.get_friends方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。