Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit ed7eac4

Browse files
authored
Merge pull request #142 from alexmercerind/streamurlfetcher
Fixes to StreamURLFetcher - Resolves #134
2 parents 31dfb19 + 818d9ab commit ed7eac4

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

‎youtubesearchpython/core/streamurlfetcher.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ def __init__(self):
3232
Saving videoFormats inside a dictionary with key "player_response" for apply_descrambler & apply_signature methods.
3333
'''
3434
def _getDecipheredURLs(self, videoFormats: dict) -> None:
35+
# For some reason, we cannot fetch JavaScript the old way, as PyTube's RegEx doesn't like it.
3536
self.video_id = videoFormats["id"]
3637
self._player_response = videoFormats
38+
if not videoFormats['streamingData']:
39+
try:
40+
self.use_oauth = False
41+
self.allow_oauth_cache = False
42+
self.bypass_age_gate()
43+
self._player_response = self._vid_info
44+
except:
45+
raise Exception('ERROR: Could not make request.')
46+
47+
# We use this to retrieve JavaScript
3748
url = f"https://www.youtube.com/watch?v={self.video_id}"
3849
self.youtube = pytube.YouTube(url)
3950

@@ -50,7 +61,8 @@ def _getJS(self) -> None:
5061
self._js = self.youtube.js
5162

5263
async def getJavaScript(self):
53-
# we don't wanna break compatibility, so we just pass
64+
# we don't wanna break compatibility, so we just pass.
65+
# We retrieve Player JavaScript using _getDecipheredURLs()
5466
pass
5567

5668
'''
@@ -69,9 +81,6 @@ def _decipher(self, retry: bool = False):
6981
Used to decipher the stream URLs using player JavaScript & the player_response passed from the getStream method of this derieved class.
7082
These methods operate on the value of "player_response" key in dictionary of self._player_response & save _deciphered information in the "url_encoded_fmt_stream_map" key.
7183
'''
72-
#self._player_response = self.youtube.vid_info
73-
with open("test2.json", "w+", encoding="utf-8") as f:
74-
f.write(json.dumps(self.youtube.vid_info))
7584

7685
stream = apply_descrambler(self._player_response["streamingData"])
7786
apply_signature(

‎youtubesearchpython/core/video.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def __init__(self, videoLink: str, componentMode: str, resultMode: int, timeout:
2525
'context': {
2626
'client': {
2727
'clientName': 'ANDROID',
28-
'clientVersion': '16.20',
29-
'clientScreen': 'EMBED'
28+
'clientVersion': '16.20'
3029
}
3130
},
3231
'api_key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
@@ -56,8 +55,6 @@ def sync_create(self):
5655
def __parseSource(self) -> None:
5756
try:
5857
self.responseSource = json.loads(self.response)
59-
with open("test.json", "w+", encoding="utf-8") as f:
60-
f.write(self.response)
6158
except Exception as e:
6259
raise Exception('ERROR: Could not parse YouTube response.')
6360

@@ -95,5 +92,11 @@ def __getVideoComponent(self, mode: str) -> None:
9592
component['isLiveNow'] = component['isLiveContent'] and component['duration']['secondsText'] == "0"
9693
component['link'] = 'https://www.youtube.com/watch?v=' + component['id']
9794
component['channel']['link'] = 'https://www.youtube.com/channel/' + component['channel']['id']
98-
self.responseSource.update(component)
99-
self.__videoComponent = self.responseSource
95+
videoComponent.update(component)
96+
if mode in ['getFormats', None]:
97+
videoComponent.update(
98+
{
99+
"streamingData": getValue(self.responseSource, ["streamingData"])
100+
}
101+
)
102+
self.__videoComponent = videoComponent

0 commit comments

Comments
 (0)