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

Commit

Permalink
👍 Added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmercerind committed Sep 17, 2020
1 parent b3c89b3 commit 2d6284b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 135 deletions.
2 changes: 0 additions & 2 deletions youtubesearchpython/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from youtubesearchpython.videos__search import SearchVideos
from youtubesearchpython.playlist__search import SearchPlaylists

#########(Legacy)#########

from youtubesearchpython.videos__search import SearchVideos as searchYoutube
15 changes: 2 additions & 13 deletions youtubesearchpython/__requesthandler.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
import sys

#########python2#########
if sys.version_info[0]==2:
if sys.version_info[0] == 2:
from urllib import urlencode, urlopen

#########python3#########
else:
from urllib.request import urlopen
from urllib.parse import urlencode


class requesthandler:

class RequestHandler:
def request(self):

try:
query = urlencode({
"search_query": self.keyword,
"page": self.offset,
"sp": self.searchPreferences
})
request = "https://www.youtube.com/results" + "?" + query

#########Making Network Request#########

response = urlopen(request).read()

self.page = response.decode('utf_8')

#########Identifying the type of response returned.#########

if self.page[0:29] == ' <!DOCTYPE html><html lang="':
self.validResponse = True

Expand Down
20 changes: 4 additions & 16 deletions youtubesearchpython/playlist__scripthandler.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
class scripthandler:

def scriptResponseHandler(self):

#########playlistHandler PROPERTY#########

#########This property is later called in the another property exec() of the class. #########

temp = 0

#########Defining Result Arrays.#########

class ScriptHandler:
def scriptResponseHandler(self):
self.links = []
self.ids = []
self.titles = []

#########Transversing Through Network Request Array.#########

self.pageSource = self.page.split('":"')

for index in range(0, len(self.pageSource) - 1, 1):

#########Setting Playlist ID and link.#########
''' Setting Playlist ID and link. '''

if self.pageSource[index][-10:] == 'playlistId':
if self.pageSource[index+1].split('"')[0] not in self.ids:
Expand All @@ -30,7 +18,7 @@ def scriptResponseHandler(self):
self.ids+=[self.pageSource[index+1].split('"')[0]]
self.links+=["https://www.youtube.com/playlist?list=" + self.pageSource[index+1].split('"')[0]]

#########Setting Playlist Title.#########
''' Setting Playlist Title. '''

if self.pageSource[index][-20:] == '"title":{"simpleText' and self.pageSource[index+1][-3:] == 'url':
self.titles+=[self.pageSource[index+1].split('"},"')[0].replace("\\u0026", "&")]
Expand Down
56 changes: 28 additions & 28 deletions youtubesearchpython/playlist__search.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,59 @@
import json

from youtubesearchpython.__requesthandler import requesthandler
from youtubesearchpython.playlist__scripthandler import scripthandler


class SearchPlaylists(requesthandler, scripthandler):

#########https://github.com/alexmercerind/youtube-search-python#########

from youtubesearchpython.__requesthandler import RequestHandler
from youtubesearchpython.playlist__scripthandler import ScriptHandler


class SearchPlaylists(RequestHandler, ScriptHandler):
'''
Search for playlists in YouTube.
Parameters
----------
keyword : str
Used as a query to search for playlists in YouTube.
offset : int, optional
Offset for result pages on YouTube. Defaults to 1.
mode : str
Search result mode. Can be 'json', 'dict' or 'list'.
maxResults : int, optional
Maximum number of playlist results. Defaults to 20.
Methods
-------
result()
Returns the playlists fetched from YouTube by search().
'''
networkError = False
validResponse = False

def __init__(self, keyword, offset = 1, mode = "json", max_results = 20):

#########CLASS CONSTRUCTOR#########

#########Setting Feilds#########

self.offset = offset
self.mode = mode
self.keyword = keyword
self.max_results = max_results
self.searchPreferences = "EgIQAw%3D%3D"

#########mainuting Entry Point Of Class#########

self.main()

def main(self):

#########main PROPERTY#########

self.request()

if self.networkError:
self.networkError = True
else:
self.scriptResponseHandler()

def result(self):

#########result PROPERTY#########

#########Checking for network error and returning None to the user in case of it.#########

'''
Returns
-------
None, str, dict, list
Returns playlist results from YouTube. Returns None, if network error occurs.
'''
if self.networkError:
return None

else:

result = []

#########JSON Result Handling.#########

if self.mode in ["json", "dict"]:

Expand All @@ -69,8 +71,6 @@ def result(self):
else:
return {"search_result": result}

#########List Result Handling.#########

elif self.mode == "list":

for index in range(len(self.ids)):
Expand Down
33 changes: 8 additions & 25 deletions youtubesearchpython/videos__pagehandler.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import sys

#########python2#########
if sys.version_info < (2, 8):
if sys.version_info[0] == 2:
import HTMLParser

#########python3#########
else:
import html


class pagehandler:

class PageHandler:
def pageResponseHandler(self):

#########pageResponseHandler PROPERTY#########

#########This property is later called in the another property exec() of the class. #########

temp = 0

#########Defining Result Arrays.#########

self.links = []
self.ids = []
self.titles = []
Expand All @@ -29,11 +16,8 @@ def pageResponseHandler(self):
self.durations = []
self.thumbnails = []

#########Transversing Through Network Request Array.#########

self.pageSource = self.page.split()

#########python2#########
if sys.version_info < (2, 8):
html = HTMLParser.HTMLParser()

Expand All @@ -43,7 +27,7 @@ def pageResponseHandler(self):
elementNext = self.pageSource[index+1]
elementPrev = self.pageSource[index-1]

#########Setting Video View Counts.#########
''' Setting Video View Counts. '''

if element == "views</li></ul></div><div":
viewCount = 0
Expand All @@ -52,20 +36,19 @@ def pageResponseHandler(self):
viewCount = viewCount * 10 + int(character)
self.views+=[viewCount]

#########Setting Video Links, IDs And Thumbnails.#########
''' Setting Video Links, IDs And Thumbnails. '''

if element[0:15] == 'href="/watch?v=' and len('www.youtube.com'+element[6:len(element)-1]) == 35:
thumbnailbuffer = []
modes = ["default", "hqdefault", "mqdefault", "sddefault", "maxresdefault"]
temp+=1
if temp%2 ==0:
if element[15:len(element) - 1] not in self.ids:
self.links+=['https://www.youtube.com'+element[6:len(element)-1]]
self.ids+=[element[15:len(element) - 1]]
for mode in modes:
thumbnailbuffer+=["https://img.youtube.com/vi/" + element[15:len(element) - 1] + "/" + mode + ".jpg"]
self.thumbnails+=[thumbnailbuffer]

#########Setting Video Channels.#########
''' Setting Video Channels. '''

if (element[-15:] == '</a>&nbsp;<span' and self.pageSource[index+1] == 'title="Verified"') or (element[-14:] == '</a></div><div' and self.pageSource[index+1] == 'class="yt-lockup-meta'):
channelBuffer = ""
Expand All @@ -84,7 +67,7 @@ def pageResponseHandler(self):
channelBuffer+=channelText[index]
self.channels+= [channelBuffer]

#########Setting Video Durations.#########
''' Setting Video Durations. '''

if element[0:19] == 'aria-hidden="true">' and element[19].isnumeric():
buffer = ""
Expand All @@ -98,7 +81,7 @@ def pageResponseHandler(self):
break
self.durations+=[buffer[1::]]

#########Setting Video Titles.#########
''' Setting Video Titles. '''

if (element[0:23] == 'data-sessionlink="itct=') and (elementNext[0:7] == 'title="'):
buffer = ""
Expand Down
22 changes: 5 additions & 17 deletions youtubesearchpython/videos__scripthandler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
class scripthandler:

class ScriptHandler:
def scriptResponseHandler(self):

#########scriptResponseHandler PROPERTY#########

#########This property is later called in the another property exec() of the class. #########

temp = 0

#########Defining Result Arrays.#########

self.links = []
self.ids = []
self.titles = []
Expand All @@ -18,13 +8,11 @@ def scriptResponseHandler(self):
self.durations = []
self.thumbnails = []

#########Transversing Through Network Request Array.#########

self.pageSource = self.page.split('":"')

for index in range(0, len(self.pageSource) - 1, 1):

#########Setting Video Durations And View Counts.#########
''' Setting Video Durations And View Counts. '''

if self.pageSource[index][-14:] == '}},"simpleText' and self.pageSource[index+1][-28:] == '"viewCountText":{"simpleText':
durationBuffer = ""
Expand All @@ -40,7 +28,7 @@ def scriptResponseHandler(self):
self.durations[-1] = durationBuffer
self.views[-1] = viewCountBuffer

#########Setting Video Links, IDs And Thumbnails.#########
''' Setting Video Links, IDs And Thumbnails. '''

if self.pageSource[index][0:9] == '/watch?v=':
id = self.pageSource[index][9:20]
Expand All @@ -52,7 +40,7 @@ def scriptResponseHandler(self):
thumbnailbuffer+=["https://img.youtube.com/vi/" + id + "/" + mode + ".jpg"]
self.thumbnails+=[thumbnailbuffer]

#########Setting Video Titles.#########
''' Setting Video Titles. '''

if self.pageSource[index][-23:] == '"title":{"runs":[{"text' and self.pageSource[index+1][-44:] == '"accessibility":{"accessibilityData":{"label':
titleBuffer = ""
Expand All @@ -66,7 +54,7 @@ def scriptResponseHandler(self):
self.durations+=["LIVE"]
self.channels+= [""]

#########Setting Video Channels.#########
''' Setting Video Channels. '''

if self.pageSource[index][-32:] == '"longBylineText":{"runs":[{"text' and self.pageSource[index+1][-42:] == '"navigationEndpoint":{"clickTrackingParams':
channelBuffer = ""
Expand Down
Loading

0 comments on commit 2d6284b

Please sign in to comment.