diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-07-14 14:35:58 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-07-14 14:35:58 -0400 |
| commit | eaed1cd7308ea203e26c782b39ce2ee5c191257c (patch) | |
| tree | a5534b237aea54be0246ab562b122fbd0daa5ee7 | |
| parent | da213ea193973e079e68759da9e835202ed04aea (diff) | |
Better support for audio
| -rw-r--r-- | lib/newgrounds/newgrounds.py | 45 |
1 files changed, 32 insertions, 13 deletions
diff --git a/lib/newgrounds/newgrounds.py b/lib/newgrounds/newgrounds.py index 10a5b57..bfbc1d5 100644 --- a/lib/newgrounds/newgrounds.py +++ b/lib/newgrounds/newgrounds.py @@ -26,7 +26,7 @@ from dateutil import parser as dateutil_parser import json import os import sys -from typing import List +from typing import List, Optional from urllib.parse import urlencode, parse_qsl from bs4 import BeautifulSoup @@ -70,7 +70,7 @@ class VisualLink: class Movie: title: str thumbnail: str - content_url: str + content_url: Optional[str] url: str views: int = 0 faves: int = 0 @@ -100,11 +100,12 @@ class Audio: faves: int = 0 votes: int = 0 score: float = 0.0 + length: str = "0:00" upload_date: datetime = datetime.fromtimestamp(0) genre: str = "Unknown" tags: list[str] = field(default_factory=lambda: []) description: str = "" - authors: list[str] = field(default_factory=lambda: []) + author: str = field(default_factory=lambda: []) suitability: Suitability = Suitability.E def __post_init__(self): @@ -315,6 +316,9 @@ class Newgrounds(object): "https://www.newgrounds.com/portal/video/{}".format(movie_id), headers={"X-Requested-With": "XMLHttpRequest"}, ).json() + # This is a Flash movie, not an MP4 we'll be able to play in Kodi. + if "sources" not in response: + return None preferences = ["1080p", "720p", "360p"] for preference in preferences: if preference in response["sources"]: @@ -378,11 +382,12 @@ class Newgrounds(object): 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 authors is not None: + 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" @@ -492,7 +497,12 @@ class Newgrounds(object): ).json() soup = BeautifulSoup(response["content"], "html.parser") - urls = [tag["href"] for tag in soup.find_all("a", "item-portalsubmission")] + cls = ( + "item-portalsubmission" + if conduct == Conduct.MOVIES + else "item-audiosubmission" + ) + urls = [tag["href"] for tag in soup.find_all("a", cls)] return self._fetch_metadata_batch(urls) def front_page( @@ -522,6 +532,7 @@ class Newgrounds(object): assert category in AUDIO_CATEGORIES url = URLS[grouping] + categories = MOVIE_CATEGORIES if conduct == Conduct.MOVIES else AUDIO_CATEGORIES response = self._request( "GET", "https://www.newgrounds.com/movies/" + url @@ -530,7 +541,7 @@ class Newgrounds(object): params={ "interval": interval, "sort": stringify_enum(sort_by), - "genre": category, + "genre": categories[category], "offset": offset, "inner": 1, "isAjaxRequest": True, @@ -552,14 +563,22 @@ class Newgrounds(object): ] return self._fetch_metadata_batch(urls) - def feed(self): + def feed(self, conduct=Conduct.MOVIES): html = self._request( "GET", "https://www.newgrounds.com/social/feeds/show/favorite-artists-movies", - cookies=get_session(), ).text soup = BeautifulSoup(html, "html.parser") - urls = [tag["href"] for tag in soup.find_all("a", "item-portalsubmission")] + cards = [tag for tag in soup.find_all("a", "item-portalsubmission")] + if conduct == Conduct.MOVIES: + cards = [ + tag for tag in cards if len(tag.find_all("strong", "color-movie")) > 0 + ] + elif conduct == Conduct.AUDIO: + cards = [ + tag for tag in cards if len(tag.find_all("strong", "color-adio")) > 0 + ] + urls = [tag["href"] for tag in cards] return self._fetch_metadata_batch(urls) def radio_status(self): |