Skip to content

Commit

Permalink
fix/channel_playlist_parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 4, 2024
1 parent 30829c0 commit d9c5c27
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/ch_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
c = Channel(url)
print(c.videos_url)
print(c.vanity_url)
print(c.playlist_urls)
print(c.video_urls)
for v in c.videos:
print(v)
Expand Down
15 changes: 10 additions & 5 deletions examples/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from tutubo.models import *


def search(query, parse=False, max_res=50):
def search(query, parse=False, max_res=50, live=False):
res = {
'channels': [],
'playlists': [],
'mix': [],
'related_queries': [],
'related_videos': [],
'videos': []
'videos': [],
'live': []
}
for v in search_yt(query, parse=parse, max_res=max_res, as_dict=False):
if isinstance(v, YoutubeMixPreview):
Expand All @@ -22,13 +23,17 @@ def search(query, parse=False, max_res=50):
res["playlists"].append(v.as_dict)
elif isinstance(v, RelatedVideoPreview) or isinstance(v, RelatedVideo):
res["related_videos"].append(v.as_dict)
else:
res["videos"].append(v.as_dict)
elif isinstance(v, VideoPreview):
if v.is_live:
res["live"].append(v.as_dict)
else:
res["videos"].append(v.as_dict)
return res


from pprint import pprint

res = search("rob zombie", parse=False, max_res=50)
res = search("Shout TV live", parse=False, max_res=50)

pprint(res)
"""
Expand Down
30 changes: 22 additions & 8 deletions tutubo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def length(self):
60 * 60 * int(h)
return 0

@property
def is_live(self) -> bool:
return self.length == 0

@property
def thumbnail_url(self):
return f"https://img.youtube.com/vi/{self.video_id}/default.jpg"
Expand Down Expand Up @@ -371,16 +375,26 @@ def _extract_playlists(raw_json: str) -> Tuple[List[str], Optional[str]]:

# this is the json tree structure, if the json was extracted from
# html
playlists = []
try:
playlists = initial_data["contents"][
"twoColumnBrowseResultsRenderer"][
"tabs"][2]["tabRenderer"]["content"][
"sectionListRenderer"]["contents"][0][
"itemSectionRenderer"]["contents"][0][
'shelfRenderer']["content"][
'horizontalListRenderer']["items"]
tabs = initial_data["contents"]["twoColumnBrowseResultsRenderer"]["tabs"]
for t in tabs:
if "content" not in t["tabRenderer"]:
continue
data = t["tabRenderer"]["content"]
if "sectionListRenderer" not in t["tabRenderer"]["content"]:
continue
for c in data["sectionListRenderer"]["contents"][0]["itemSectionRenderer"]["contents"]:
if 'shelfRenderer' in c:
playlists = c['shelfRenderer']["content"]['horizontalListRenderer']["items"]
break
elif 'gridRenderer' in c:
playlists = c['gridRenderer']["items"]
break
if playlists:
break
except (KeyError, IndexError, TypeError):
playlists = []
pass

continuation = None
# remove duplicates
Expand Down

0 comments on commit d9c5c27

Please sign in to comment.