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

Commit

Permalink
Merge pull request #148 from alexmercerind/videorich
Browse files Browse the repository at this point in the history
Migrate from HTML requests to InnerTube requests for getting upload date & add some more variables to Video class
  • Loading branch information
mytja authored Dec 24, 2021
2 parents b99116c + fc68ef1 commit f762135
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions youtubesearchpython/core/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@ def prepare_innertube_request(self):
'api_key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
}

def __extractFromHTML(self):
f1 = "var ytInitialPlayerResponse = "
startpoint = self.htmlresponse.find(f1)
self.htmlresponse = self.htmlresponse[startpoint + len(f1):]
f2 = ';var meta = '
endpoint = self.htmlresponse.find(f2)
if startpoint and endpoint:
startpoint += len(f1)
endpoint += len(f2)
r = self.htmlresponse[:endpoint]
r = r.replace(';var meta = ', "")
self.htmlresponse = r
try:
self.HTMLresponseSource = json.loads(self.htmlresponse)
except Exception as e:
raise Exception('ERROR: Could not parse YouTube response.')

async def async_create(self):
self.prepare_innertube_request()
response = await self.asyncPostRequest()
Expand All @@ -80,25 +63,31 @@ def sync_create(self):
raise Exception('ERROR: Invalid status code.')

def prepare_html_request(self):
self.url = 'https://www.youtube.com/watch' + '?' + urlencode({'v': getVideoId(self.videoLink)})
self.url = 'https://www.youtube.com/youtubei/v1/player' + "?" + urlencode({
'key': searchKey,
'contentCheckOk': True,
'racyCheckOk': True,
"videoId": getVideoId(self.videoLink)
})
self.data = {
'context': {
'client': {
'clientName': 'MWEB',
'clientVersion': '2.20211109.01.00'
}
},
'api_key': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8'
}

def sync_html_create(self):
self.prepare_html_request()
response = self.syncGetRequest()
self.htmlresponse = response.text
if response.status_code == 200:
self.__extractFromHTML()
else:
raise Exception('ERROR: Invalid status code.')
response = self.syncPostRequest()
self.HTMLresponseSource = response.json()

async def async_html_create(self):
self.prepare_html_request()
response = await self.asyncGetRequest()
self.htmlresponse = response.text
if response.status_code == 200:
self.__extractFromHTML()
else:
raise Exception('ERROR: Invalid status code.')
response = await self.asyncPostRequest()
self.HTMLresponseSource = response.json()

def __parseSource(self) -> None:
try:
Expand Down Expand Up @@ -142,6 +131,8 @@ def __getVideoComponent(self, mode: str) -> None:
'isLiveContent': getValue(responseSource, ['videoDetails', 'isLiveContent']),
'publishDate': getValue(responseSource, ['microformat', 'playerMicroformatRenderer', 'publishDate']),
'uploadDate': getValue(responseSource, ['microformat', 'playerMicroformatRenderer', 'uploadDate']),
'isFamilySafe': getValue(responseSource, ['microformat', 'playerMicroformatRenderer', 'isFamilySafe']),
'category': getValue(responseSource, ['microformat', 'playerMicroformatRenderer', 'category']),
}
component['isLiveNow'] = component['isLiveContent'] and component['duration']['secondsText'] == "0"
component['link'] = 'https://www.youtube.com/watch?v=' + component['id']
Expand Down

0 comments on commit f762135

Please sign in to comment.