app.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:twitch-zoo 作者: dbernard 项目源码 文件源码
def build_streamer_json(stream, user_info):
    """Compile useful streamer information from a stream object.

    :param user: The username of the streamer
    :param stream: The complete stream JSON object
    :param participant_id: The user's Extra Life participant ID
    :return: A subset object of relevant streamer info
    """
    participant_id = user_info.get('EXTRALIFE')
    user = user_info.get('TWITCH')
    donate_url = 'https://www.extra-life.org/index.cfm?fuseaction=donorDrive.' \
                 'participant&participantID={}'.format(participant_id)
    s = {
        'dispname': user_info['NAME'],
        'username': user_info['TWITCH'],
        'playing': 'Offline',
        'viewers': 0,
        'url': 'https://www.twitch.tv/{}'.format(user),
        'preview': 'http://placehold.it/640x360',
        'participant_id': participant_id,
        'donate': donate_url if participant_id else None,
        'fps': 0,
        'views': 0,
    }

    if not stream['stream']:
        return s

    mapping = [
        ('pubg', 'PUBG', "PLAYERUNKNOWN'S BATTLEGROUNDS", ),
        ('overwatch', 'BLIZZARD', 'Overwatch', ),
        ('rocketleague', 'STEAM', 'Rocket League', ),
        ('destiny2', 'DESTINY2', 'Destiny 2', )
    ]

    for key, lookup, twitch_name in mapping:
        module = importlib.import_module('games.{}'.format(key))
        if user_info.get(lookup):
            if stream['stream'].get('game') != twitch_name:
                continue
            try:
                s[key] = module.stats(user_info[lookup])
            except KeyError as exc:
                s[key] = {}

    s['username'] = stream['stream']['channel']['display_name']
    s['playing'] = stream['stream']['game']
    s['viewers'] = humanize.intcomma(int(stream['stream']['viewers']))
    s['preview'] = stream['stream']['preview']['large']
    s['fps'] = stream['stream']['average_fps']
    s['views'] = humanize.intword(int(stream['stream']['channel']['views']))

    return s
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号