From 34be8ffa372c9eb7753c1d810d4ea164b9284c1a Mon Sep 17 00:00:00 2001 From: Fabian Kokot Date: Mon, 13 Jun 2022 20:48:45 +0200 Subject: [PATCH] Added the follows_at field to the Followers and a method for requesting follow-information of a specific user --- twitchbot/data/follower.py | 1 + twitchbot/data/user_followers.py | 3 ++- twitchbot/util/twitch_api_util.py | 27 +++++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/twitchbot/data/follower.py b/twitchbot/data/follower.py index d8a14cc..8fc3a6e 100644 --- a/twitchbot/data/follower.py +++ b/twitchbot/data/follower.py @@ -8,3 +8,4 @@ class Follower(NamedTuple): following: str id: int name: str + followed_at: str diff --git a/twitchbot/data/user_followers.py b/twitchbot/data/user_followers.py index 013b1b1..ee5c9e4 100644 --- a/twitchbot/data/user_followers.py +++ b/twitchbot/data/user_followers.py @@ -13,7 +13,8 @@ def __init__(self, follower_count: int, following: str, following_id: int, name: Follower(following=following, following_id=int(following_id), id=int(json['from_id']), - name=json['from_name']) + name=json['from_name'], + followed_at=json['followed_at']) for json in followers if 'from_id' in json and 'from_name' in json ] diff --git a/twitchbot/util/twitch_api_util.py b/twitchbot/util/twitch_api_util.py index 87efda8..af33a6c 100644 --- a/twitchbot/util/twitch_api_util.py +++ b/twitchbot/util/twitch_api_util.py @@ -9,12 +9,13 @@ from async_timeout import timeout from ..config import get_client_id, get_oauth, DEFAULT_CLIENT_ID -from ..data import UserFollowers, UserInfo, RateLimit +from ..data import UserFollowers, UserInfo, RateLimit, Follower __all__ = ('CHANNEL_CHATTERS_URL', 'get_channel_chatters', 'get_stream_data', 'get_url', 'get_user_data', 'get_user_id', 'STREAM_API_URL', 'USER_API_URL', 'get_user_followers', 'USER_FOLLOWERS_API_URL', 'get_headers', 'get_user_info', 'USER_ACCOUNT_AGE_API', 'CHANNEL_INFO_API', 'get_channel_info', 'ChannelInfo', - 'get_channel_name_from_user_id', 'OauthTokenInfo', 'get_oauth_token_info', '_check_token', 'post_url') + 'get_channel_name_from_user_id', 'OauthTokenInfo', 'get_oauth_token_info', '_check_token', 'post_url', 'USER_FOLLOWAGE_API_URL', + 'get_user_followage') USER_API_URL = 'https://api.twitch.tv/helix/users?login={}' STREAM_API_URL = 'https://api.twitch.tv/helix/streams?user_login={}' @@ -22,6 +23,7 @@ USER_FOLLOWERS_API_URL = 'https://api.twitch.tv/helix/users/follows?to_id={}' USER_ACCOUNT_AGE_API = 'https://api.twitch.tv/kraken/users/{}' CHANNEL_INFO_API = 'https://api.twitch.tv/helix/channels?broadcaster_id={}' +USER_FOLLOWAGE_API_URL = 'https://api.twitch.tv/helix/users/follows?to_id={}&from_id={}' user_id_cache: Dict[str, int] = {} @@ -99,6 +101,27 @@ async def get_user_followers(user: str, headers: dict = None) -> UserFollowers: id=user_id_cache[user], followers=json['data']) +async def get_user_followage(user: str, follower: str, headers: dict = None) -> Follower: + headers = headers if headers is not None else get_headers() + if not _check_headers_has_auth(headers): + warnings.warn('[GET_USER_FOLLOWAGE] headers for the twitch api request are missing authorization') + return Follower(-1, '', -1, '', '') + + user_id = await get_user_id(user, headers) + follower_id = await get_user_id(follower, headers) + _, json = await get_url(USER_FOLLOWAGE_API_URL.format(user_id, follower_id), headers) + # ratelimit = RateLimit.from_headers(_.headers) + + # covers invalid user id, or some other API error, such as invalid client-id + if not json or json.get('status', -1) == 400: + return Follower(-1, '', -1, '', '') + + return Follower(following=user, + following_id=user_id, + id=follower_id, + name=json['data'][0]['from_name'], + followed_at=json['data'][0]['followed_at']) + async def get_user_data(user: str, headers: dict = None) -> dict: headers = headers if headers is not None else get_headers()