diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-06-19 09:48:40 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-06-19 09:57:11 -0400 |
| commit | 9fea13c74e560ca015fa3ff731c78706b9e638c0 (patch) | |
| tree | e81cffb0ef5d464d2070242ec071e5b9d2024cbe | |
| parent | 3b704f0d7f42959a2c9dca4756da53cd62ed3d96 (diff) | |
Be more defensive in `get_addon_video_info`
| -rw-r--r-- | addon.py | 123 |
1 files changed, 61 insertions, 62 deletions
@@ -18,6 +18,7 @@ Video plugin for animations posted to Newgrounds. """ import concurrent.futures +from datetime import datetime import dateutil import os import sys @@ -64,7 +65,7 @@ def list_top_level(): info_tag = list_item.getVideoInfoTag() info_tag.setMediaType("video") info_tag.setTitle("Search") - url = get_url(action="searchprompt") + url = get_url(action="prompt_search") is_folder = False xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder) @@ -161,42 +162,57 @@ def get_addon_video_info(url): thumbnail = soup.find("meta", property="og:image") if thumbnail is not None: thumbnail = thumbnail["content"] + + metadata = { + "title": title, + "thumbnail": thumbnail, + "url": url, + "views": 0, + "faves": 0, + "votes": 0, + "score": 0.0, + "upload_date": datetime.fromtimestamp(0), + "genre": "Unknown", + "tags": [], + "description": "", + "authors": [], + "rating": "e", + } + sidestats_popularity = soup.find_all("dl", "sidestats")[0].find_all("dd") - views = sidestats_popularity[0].string - faves = sidestats_popularity[1].a - if faves is not None: - faves = faves.string - else: - faves = "0" - votes = sidestats_popularity[2].string - if votes is None: - votes = "0" - try: - score = sidestats_popularity[3].span.string - except IndexError: - score = 0 + if len(sidestats_popularity) == 4: + metadata["views"] = int(sidestats_popularity[0].string.replace(",", "")) + faves = sidestats_popularity[1].a + if faves is not None: + metadata["faves"] = int(faves.string.replace(",", "")) + metadata["votes"] = int(sidestats_popularity[2].string) + score = sidestats_popularity[3].span + if score is not None: + metadata["score"] = float(score.string) + sidestats_date_and_genre = soup.find_all("dl", "sidestats")[1].find_all("dd") - upload_date = sidestats_date_and_genre[0].string - upload_time = sidestats_date_and_genre[1].string - try: - genre = sidestats_date_and_genre[2].a.string - except IndexError as e: - genre = "Unknown" - try: - tags = [tag.string for tag in soup.find("dd", "tags").find_all("a")] - except AttributeError as e: - tags = [] - author_comments = "".join( - [s for s in soup.find("div", id="author_comments").strings] - ).strip() - try: - authors = [ - author.string - for author in soup.find("ul", "authorlinks").find_all("a") - if author.get("class") is None - ] - except AttributeError: - authors = [] + if len(sidestats_date_and_genre) == 3: + metadata["upload_date"] = dateutil.parser.parse( + sidestats_date_and_genre[0].string + + " " + + sidestats_date_and_genre[1].string + ) + genre = sidestats_date_and_genre[2].a + if genre is not None: + metadata["genre"] = genre.string + + tags = soup.find("dd", "tags") + if tags is not None: + metadata["tags"] = [tag.string for tag in tags.find_all("a")] + + author_comments = soup.find("div", id="author_comments") + metadata["description"] = " ".join([s for s in author_comments.strings]).strip() + + authors = soup.find("ul", "authorlinks") + metadata["authors"] = [ + author.string for author in authors.find_all("a") if author.get("class") is None + ] + if len(soup.find_all("h2", "rated-a")) != 0: rating = "a" elif len(soup.find_all("h2", "rated-m")) != 0: @@ -206,22 +222,7 @@ def get_addon_video_info(url): else: rating = "e" - return { - "title": title, - "thumbnail": thumbnail, - "views": views, - "faves": faves, - "votes": votes, - "score": score, - "upload_date": upload_date, - "upload_time": upload_time, - "genre": genre, - "tags": tags, - "description": author_comments, - "authors": authors, - "rating": rating, - "url": url, - } + return metadata def get_frontpage_video_urls( @@ -337,15 +338,13 @@ def list_videos(video_urls, title=None, next_page=None): info_tag = list_item.getVideoInfoTag() info_tag.setMediaType("movie") info_tag.setTitle(video["title"]) - info_tag.setYear(int(video["upload_date"][-4:])) - info_tag.setRating(float(video["score"]), int(video["votes"].replace(",", ""))) - info_tag.setPlaycount(int(video["views"].replace(",", ""))) + info_tag.setYear(video["upload_date"].year) + info_tag.setRating(video["score"], video["votes"]) + info_tag.setPlaycount(video["views"]) info_tag.setPlot(video["description"]) info_tag.setGenres([video["genre"]]) info_tag.setDirectors(video["authors"]) - info_tag.setPremiered( - dateutil.parser.parse(video["upload_date"]).strftime("%Y-%m-%d") - ) + info_tag.setPremiered(video["upload_date"].strftime("%Y-%m-%d")) info_tag.setTags(video["tags"]) if video["rating"] == "a": @@ -394,6 +393,11 @@ def router(paramstring): params = dict(parse_qsl(paramstring)) if not params: list_top_level() + elif params["action"] == "prompt_search": + dialog = xbmcgui.Dialog() + term = dialog.input(heading="Search") + url = get_url(action="search", term=term) + xbmc.executebuiltin("Container.Update(%s)" % url) elif params["action"] == "listing" and "category" in params: offset = int(params.get("offset", 0)) video_urls = get_frontpage_video_urls( @@ -411,11 +415,6 @@ def router(paramstring): list_videos(video_urls, params["genre"], next_page) elif params["action"] == "listing": list_categories(params["genre"]) - elif params["action"] == "searchprompt": - dialog = xbmcgui.Dialog() - term = dialog.input(heading="Search") - url = get_url(action="search", term=term) - xbmc.executebuiltin("Container.Update(%s)" % url) elif params["action"] == "search": term = params["term"] offset = int(params.get("offset", 1)) |