Skip to content

Commit

Permalink
Fix remote watch history not updating anxdpanic#1008
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Dec 3, 2024
1 parent 8b55240 commit c877a0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
54 changes: 28 additions & 26 deletions resources/lib/youtube_plugin/youtube/client/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ class YouTube(LoginClient):
'Host': 'www.youtube.com',
},
},
'watch_history': {
'_auth_required': True,
'_auth_type': 'personal',
'_video_id': None,
'headers': {
'Host': 's.youtube.com',
'Referer': 'https://www.youtube.com/watch?v={_video_id}',
},
'params': {
'referrer': 'https://accounts.google.com/',
'ns': 'yt',
'el': 'detailpage',
'ver': '2',
'fs': '0',
'volume': '100',
'muted': '0',
},
},
'_common': {
'_access_token': None,
'_access_token_tv': None,
Expand Down Expand Up @@ -156,29 +174,13 @@ def update_watch_history(self, context, video_id, url, status=None):
et=et,
state=state))

headers = {
'Host': 's.youtube.com',
'Connection': 'keep-alive',
'Accept-Encoding': 'gzip, deflate',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'DNT': '1',
'Referer': 'https://www.youtube.com/watch?v=' + video_id,
'User-Agent': ('Mozilla/5.0 (Linux; Android 10; SM-G981B)'
' AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/80.0.3987.162 Mobile Safari/537.36'),
}
params = {
'docid': video_id,
'referrer': 'https://accounts.google.com/',
'ns': 'yt',
'el': 'detailpage',
'ver': '2',
'fs': '0',
'volume': '100',
'muted': '0',
client_data = {
'_video_id': video_id,
'url': url,
'error_title': 'Failed to update watch history',
}

params = {}
if cmt is not None:
params['cmt'] = format(cmt, '.3f')
if st is not None:
Expand All @@ -187,11 +189,11 @@ def update_watch_history(self, context, video_id, url, status=None):
params['et'] = format(et, '.3f')
if state is not None:
params['state'] = state
if self._access_token:
params['access_token'] = self._access_token

self.request(url, params=params, headers=headers,
error_msg='Failed to update watch history')
self.api_request(client='watch_history',
client_data=client_data,
params=params,
no_content=True)

def get_streams(self,
context,
Expand Down
16 changes: 13 additions & 3 deletions resources/lib/youtube_plugin/youtube/helper/stream_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ def __init__(self,
self._calculate_n = True
self._cipher = None

self._auth_client = {}
self._selected_client = {}
self._client_groups = {
'custom': clients if clients else (),
Expand Down Expand Up @@ -1424,6 +1425,7 @@ def load_stream_info(self, video_id):
audio_only = self._audio_only
ask_for_quality = self._ask_for_quality
use_mpd = self._use_mpd
use_remote_history = settings.use_remote_history()

client_name = None
_client = None
Expand Down Expand Up @@ -1467,14 +1469,15 @@ def load_stream_info(self, video_id):
'videoId': video_id,
},
'_auth_required': False,
'_auth_requested': 'personal' if use_remote_history else False,
'_access_token': self._access_token,
'_access_token_tv': self._access_token_tv,
}

for name, clients in self._client_groups.items():
if not clients:
continue
if name == 'mpd' and not use_mpd:
if name == 'mpd' and not (use_mpd or use_remote_history):
continue
if name == 'ask' and use_mpd and not ask_for_quality:
continue
Expand Down Expand Up @@ -1588,6 +1591,11 @@ def load_stream_info(self, video_id):
'client': _client.copy(),
'result': _result,
}
if not self._auth_client and _client.get('_has_auth'):
self._auth_client = {
'client': _client.copy(),
'result': _result,
}

_streaming_data = _result.get('streamingData', {})
if audio_only or ask_for_quality or not use_mpd:
Expand Down Expand Up @@ -1667,12 +1675,14 @@ def load_stream_info(self, video_id):
'subtitles': None,
}

if settings.use_remote_history():
if use_remote_history and self._auth_client:
playback_stats = {
'playback_url': 'videostatsPlaybackUrl',
'watchtime_url': 'videostatsWatchtimeUrl',
}
playback_tracking = result.get('playbackTracking', {})
playback_tracking = (self._auth_client
.get('result', {})
.get('playbackTracking', {}))
cpn = self._generate_cpn()

for key, url_key in playback_stats.items():
Expand Down

0 comments on commit c877a0c

Please sign in to comment.