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

Commit

Permalink
Merge pull request #142 from alexmercerind/streamurlfetcher
Browse files Browse the repository at this point in the history
Fixes to StreamURLFetcher - Resolves #134
  • Loading branch information
mytja authored Nov 20, 2021
2 parents 31dfb19 + 818d9ab commit ed7eac4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 13 additions & 4 deletions youtubesearchpython/core/streamurlfetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,19 @@ def __init__(self):
Saving videoFormats inside a dictionary with key "player_response" for apply_descrambler & apply_signature methods.
'''
def _getDecipheredURLs(self, videoFormats: dict) -> None:
# For some reason, we cannot fetch JavaScript the old way, as PyTube's RegEx doesn't like it.
self.video_id = videoFormats["id"]
self._player_response = videoFormats
if not videoFormats['streamingData']:
try:
self.use_oauth = False
self.allow_oauth_cache = False
self.bypass_age_gate()
self._player_response = self._vid_info
except:
raise Exception('ERROR: Could not make request.')

# We use this to retrieve JavaScript
url = f"https://www.youtube.com/watch?v={self.video_id}"
self.youtube = pytube.YouTube(url)

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

async def getJavaScript(self):
# we don't wanna break compatibility, so we just pass
# we don't wanna break compatibility, so we just pass.
# We retrieve Player JavaScript using _getDecipheredURLs()
pass

'''
Expand All @@ -69,9 +81,6 @@ def _decipher(self, retry: bool = False):
Used to decipher the stream URLs using player JavaScript & the player_response passed from the getStream method of this derieved class.
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.
'''
#self._player_response = self.youtube.vid_info
with open("test2.json", "w+", encoding="utf-8") as f:
f.write(json.dumps(self.youtube.vid_info))

stream = apply_descrambler(self._player_response["streamingData"])
apply_signature(
Expand Down
15 changes: 9 additions & 6 deletions youtubesearchpython/core/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def __init__(self, videoLink: str, componentMode: str, resultMode: int, timeout:
'context': {
'client': {
'clientName': 'ANDROID',
'clientVersion': '16.20',
'clientScreen': 'EMBED'
'clientVersion': '16.20'
}
},
'api_key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
Expand Down Expand Up @@ -56,8 +55,6 @@ def sync_create(self):
def __parseSource(self) -> None:
try:
self.responseSource = json.loads(self.response)
with open("test.json", "w+", encoding="utf-8") as f:
f.write(self.response)
except Exception as e:
raise Exception('ERROR: Could not parse YouTube response.')

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

0 comments on commit ed7eac4

Please sign in to comment.