From c85dea215ae720e3df71d2ed1aaa82f7b8a6a2ed Mon Sep 17 00:00:00 2001 From: Tristan Lee Date: Tue, 8 Mar 2022 10:07:42 -0600 Subject: [PATCH] added capability to generate auth_token --- polyphemus/api.py | 45 +++++++++++++++++++++++++++++++++------------ polyphemus/base.py | 29 +++++++++++++++++------------ tests/api.py | 7 ++++--- tests/conftest.py | 5 ++++- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/polyphemus/api.py b/polyphemus/api.py index b4b1d1e..dc7430e 100644 --- a/polyphemus/api.py +++ b/polyphemus/api.py @@ -10,11 +10,6 @@ from urllib.parse import quote import requests -#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# - -#TODO Figure out how to reverse-engineer this -AUTH_TOKEN = 'BseGAiye641UqUsv4g31ZcUCRiLasv3U' - # API endpoints for Odysee data #-----------------------------------------------------------------------------# @@ -24,6 +19,7 @@ VIEW_API_URL = 'https://api.odysee.com/file/view_count' REACTION_API_URL = 'https://api.odysee.com/reaction/list' COMMENT_API_URL = 'https://comments.odysee.com/api/v2' RECOMMENDATION_API_URL = 'https://recsys.odysee.com/search' +NEW_USER_API_URL = 'https://api.odysee.com/user/new' #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# @@ -44,13 +40,29 @@ def make_request(request, kwargs): response = request(**kwargs) if response.status_code != 200: - msg = f'Maximum number of retries reached for request {request} with kwargs {kwargs}' + msg = f'Maximum number of retries reached for request {request} with kwargs {kwargs}: status code {response.status_code}' raise ValueError(msg) return response #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# +def get_auth_token(): + + """Get a fresh authorization token, to use for API calls that require it. + """ + + response = make_request( + request = requests.post, + kwargs = { + 'url' : NEW_USER_API_URL}) + + auth_token = json.loads(response.text)['data']['auth_token'] + + return auth_token + +#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# + def get_channel_info(channel_name): """Get the channel information and ID from the channel name. @@ -87,13 +99,16 @@ def get_channel_info(channel_name): #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# -def get_subscribers(channel_id): +def get_subscribers(channel_id, auth_token = None): """Get the number of subscribers for a channel. """ + if auth_token is None: + auth_token = get_auth_token() + json_data = { - 'auth_token': AUTH_TOKEN, + 'auth_token': auth_token, 'claim_id': channel_id } response = make_request( @@ -156,13 +171,16 @@ def get_all_videos(channel_id): #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# -def get_views(video_id): +def get_views(video_id, auth_token = None): """Get the number of views for a given video. """ + if auth_token is None: + auth_token = get_auth_token() + params = { - 'auth_token': AUTH_TOKEN, + 'auth_token': auth_token, 'claim_id': video_id } response = make_request( @@ -177,13 +195,16 @@ def get_views(video_id): #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# -def get_video_reactions(video_id): +def get_video_reactions(video_id, auth_token = None): """Get all reactions for a given video. """ + if auth_token is None: + auth_token = get_auth_token() + post_data = { - 'auth_token': AUTH_TOKEN, + 'auth_token': auth_token, 'claim_ids': video_id } response = make_request( diff --git a/polyphemus/base.py b/polyphemus/base.py index 1166c0c..a8492c3 100644 --- a/polyphemus/base.py +++ b/polyphemus/base.py @@ -12,11 +12,6 @@ from polyphemus import api #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# -#TODO Figure out how to reverse-engineer this -AUTH_TOKEN = 'BseGAiye641UqUsv4g31ZcUCRiLasv3U' - -#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# - class OdyseeChannel: #-------------------------------------------------------------------------# @@ -30,7 +25,11 @@ class OdyseeChannel: self.info = info self._channel_id = self.info['channel_id'] - self.info['subscribers'] = api.get_subscribers(channel_id = self.info['channel_id']) + self.auth_token = api.get_auth_token() + + self.info['subscribers'] = api.get_subscribers( + channel_id = self.info['channel_id'], + auth_token = self.auth_token) #-------------------------------------------------------------------------# @@ -40,7 +39,7 @@ class OdyseeChannel: """ all_video_info = api.get_all_videos(channel_id=self.info['channel_id']) - self.all_videos = (OdyseeVideo(video) for video in all_video_info) + self.all_videos = (OdyseeVideo(video, self.auth_token) for video in all_video_info) return self.all_videos @@ -67,7 +66,12 @@ class OdyseeVideo: #-------------------------------------------------------------------------# - def __init__(self, full_video_info): + def __init__(self, full_video_info, auth_token = None): + + if auth_token is None: + self.auth_token = api.get_auth_token() + else: + self.auth_token = auth_token # Handle edge cases #.....................................................................# @@ -129,10 +133,11 @@ class OdyseeVideo: self.claim_id = self.info['claim_id'] - self.info['views'] = api.get_views(video_id=self.claim_id) + self.info['views'] = api.get_views(video_id=self.claim_id, auth_token = self.auth_token) - self.info['likes'], self.info['dislikes']= api.get_video_reactions( - video_id = self.claim_id) + self.info['likes'], self.info['dislikes'] = api.get_video_reactions( + video_id = self.claim_id, + auth_token = self.auth_token) self.info['streaming_url'] = api.get_streaming_url(self.info['canonical_url']) @@ -151,7 +156,7 @@ class OdyseeVideo: recommended_video_info = api.get_recommended( video_title=self.info['title'], video_id=self.claim_id) - recommended_videos = [OdyseeVideo(video_info) for video_info in recommended_video_info] + recommended_videos = [OdyseeVideo(video_info, self.auth_token) for video_info in recommended_video_info] return recommended_videos diff --git a/tests/api.py b/tests/api.py index ecd3da8..ff9e60f 100644 --- a/tests/api.py +++ b/tests/api.py @@ -20,11 +20,12 @@ from polyphemus import api #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# KWARGS_LIST = [ + ('get_auth_token', []), ('get_channel_info', ['channel_name']), - ('get_subscribers', ['channel_id']), + ('get_subscribers', ['channel_id', 'auth_token']), ('get_all_videos', ['channel_id']), - ('get_views', ['video_id']), - ('get_video_reactions', ['video_id']), + ('get_views', ['video_id', 'auth_token']), + ('get_video_reactions', ['video_id', 'auth_token']), ('get_all_comments', ['video_id']), ('append_comment_reactions', ['comment_info_list']), ('normalized_name_to_video_info', ['normalized_name']), diff --git a/tests/conftest.py b/tests/conftest.py index 91b8018..2b528fe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,8 @@ import pytest +from polyphemus.api import get_auth_token + #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++# CHANNEL_NAME = 'Mak1nBacon' @@ -90,7 +92,8 @@ def resources(): canonical_url = CANONICAL_URL, full_video_info = FULL_VIDEO_INFO, full_comment_info = {**COMMENT_INFO_LIST[0], **{'likes' : 8, 'dislikes' : 0}}, - comment_info_list = COMMENT_INFO_LIST) + comment_info_list = COMMENT_INFO_LIST, + auth_token = get_auth_token()) return resources_dict