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

Commit

Permalink
Fixes to Playlist class - get more videos - Resolves #135
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Nov 6, 2021
1 parent da8212a commit 4514122
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
9 changes: 9 additions & 0 deletions asyncExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ async def main():
print(playlistsResult)


playlist = Playlist('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')
while playlist.hasMoreVideos:
print('Getting more videos...')
await playlist.getNextVideos()
print(f'Videos Retrieved: {len(playlist.videos)}')

print('Found all the videos.')




'''
Expand Down
12 changes: 12 additions & 0 deletions syncExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@
print(playlistVideos)


playlist = Playlist('https://www.youtube.com/playlist?list=PLRBp0Fe2GpgmsW46rJyudVFlY6IYjFBIK')

print(f'Videos Retrieved: {len(playlist.videos)}')

while playlist.hasMoreVideos:
print('Getting more videos...')
playlist.getNextVideos()
print(f'Videos Retrieved: {len(playlist.videos)}')

print('Found all the videos.')




'''
Expand Down
10 changes: 6 additions & 4 deletions youtubesearchpython/core/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,24 @@ def next_post_processing(self):
def _next(self):
self.prepare_next_request()
if self.continuationKey:
statusCode = self.syncGetRequest()
statusCode = self.syncPostRequest()
self.response = statusCode.text
if statusCode.status_code == 200:
self.next_post_processing()
else:
raise Exception('ERROR: Invalid status code.')

async def _async_next(self):
self.prepare_next_request()
if self.continuationKey:
statusCode = await self.asyncGetRequest()
self.prepare_next_request()
statusCode = await self.asyncPostRequest()
self.response = statusCode.text
if statusCode.status_code == 200:
self.next_post_processing()
else:
raise Exception('ERROR: Invalid status code.')
else:
await self.async_create()

def __extractFromHTML(self):
f1 = "var ytInitialData = "
Expand All @@ -94,7 +96,7 @@ def __makeRequest(self) -> int:
def prepare_next_request(self):
requestBody = copy.deepcopy(requestPayload)
requestBody['continuation'] = self.continuationKey
self.data = json.dumps(requestBody)
self.data = requestBody
self.url = 'https://www.youtube.com/youtubei/v1/browse' + '?' + urlencode({
'key': searchKey,
})
Expand Down
8 changes: 7 additions & 1 deletion youtubesearchpython/core/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def __init__(self):
self.proxy["https://"] = https_proxy

def syncPostRequest(self) -> httpx.Response:
return httpx.post(self.url, headers={"User-Agent": userAgent}, json=self.data, timeout=self.timeout, proxies=self.proxy)
return httpx.post(
self.url,
headers={"User-Agent": userAgent},
json=self.data,
timeout=self.timeout,
proxies=self.proxy
)

async def asyncPostRequest(self) -> httpx.Response:
async with httpx.AsyncClient(proxies=self.proxy) as client:
Expand Down
2 changes: 1 addition & 1 deletion youtubesearchpython/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def __init__(self, playlistLink: str, timeout: int = None):
`hasMoreVideos` bool indicates whether more videos can be fetched or not.
'''
def getNextVideos(self) -> None:
self.__playlist.next()
self.__playlist._next()
self.videos = self.__playlist.result['videos']
self.hasMoreVideos = self.__playlist.continuationKey != None

Expand Down

0 comments on commit 4514122

Please sign in to comment.