ユーザーが放送中かどうかを確認する (ふわっち)

新着通知などに。

追記: これを使って 自動録画のサンプルコード (ふわっち) - Notes を書いてみた。

使用ライブラリ: requests
使用ソフト: python3

#!/usr/bin/env python3


import requests


_URL = 'https://api.whowatch.tv/lives'
# 'new' 新着順
# 'popular' 人気順
_SEARCH_ORDER = 'new'
_CATEGORY_ID_LIST = [
        98, # 映画出演
        4,  # 雑談
        8,  # 顔出し
        5,  # 音楽
        6,  # ゲーム
        32, # お絵かき
        40, # アウトドア
        73, # VTuber
        39, # キツネ
        50, # もぐりん禁止
        91, # ペンギン
        ]


def _get_search_category_list(category_list: list) -> list:

    """ _CATEGORY_ID_LIST で指定したカテゴリーのリストを返す

    Example:
        _CATEGORY_ID_LIST:
            [1, 2]
        category_list:
            [{'category_id': 0, ...},
            {'category_id': 1, ...},
            {'category_id': 2, ...},
            ...]
        戻り値:
            [{'category_id': 1, ...}, {'category_id': 2, ...}]

    """

    search_category_list = []
    for category_id in _CATEGORY_ID_LIST:
        for category in category_list:
            if category_id == category['category_id']:
                search_category_list.append(category)
    return search_category_list


class User:

    def __init__(self, user_path: str) -> None:
        # https://whowatch.tv/profile/{アカウント名}
        self.user_path = user_path


class Live:

    def __init__(self, id_: str, user_user_path: str) -> None:
        # https://whowatch.tv/viewer/{放送ID}
        self.id = id_

        self.user = User(
                user_path=user_user_path)


class LiveAPI:

    def update(self) -> None:

        """ 放送中のユーザー情報を読み込む

        self.live:
            {'USERID': {
                'id': '****',
                },
                'user': {
                    'user_path': '****',
                    },
            }

        """

        req = requests.get(_URL)
        category_list = _get_search_category_list(req.json())

        self.live = {}
        for category in category_list:
            for live_user in category[_SEARCH_ORDER]:
                userid = live_user['user']['user_path']
                self.live[userid] = live_user

    def exists(self, userid: str) -> bool:

        """ 放送中ユーザー内に userid が存在するかどうか """

        if userid in self.live:
            return True
        return False

    def get_user_info(self, userid: str) -> Live:

        """ ユーザー情報を返す """

        return Live(
                id_=str(self.live[userid]['id']),
                user_user_path=self.live[userid]['user']['user_path']
                )


if __name__ == '__main__':
    userid_list = [
            't:a',
            't:b',
            'w:a',
            ]

    api = LiveAPI()
    api.update()

    on_air = []
    off_air = []
    for userid in userid_list:
        if api.exists(userid):
            info = api.get_user_info(userid)
            on_air.append('{} {}'.format(info.user.user_path, info.id))
        else:
            off_air.append(userid)

    print('放送中ユーザー')
    for user in on_air: print(user)
    print('放送していないユーザー')
    for user in off_air: print(user)

出力

放送中ユーザー
t:a ****
w:a ****
放送していないユーザー
t:b