diff options
| author | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-06-19 08:44:31 -0400 |
|---|---|---|
| committer | Jakob L. Kreuze <zerodaysfordays@sdf.org> | 2024-06-19 08:44:31 -0400 |
| commit | 07ce254cae2528ed6f5118cc1ea5b834db8f3400 (patch) | |
| tree | 97550b8092d272cc2221d2db46eaa5c7f77564b9 /addon.py | |
| parent | deee3198c61da48e262ee7447335cd0515578195 (diff) | |
Initial search capability
Diffstat (limited to 'addon.py')
| -rw-r--r-- | addon.py | 100 |
1 files changed, 81 insertions, 19 deletions
@@ -59,6 +59,15 @@ def list_genres(): genres = ["Featured", "Latest", "Popular"] + # Add a search dialog + list_item = xbmcgui.ListItem(label="Search") + info_tag = list_item.getVideoInfoTag() + info_tag.setMediaType("video") + info_tag.setTitle("Search") + url = get_url(action="searchprompt") + is_folder = False + xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder) + for name in genres: list_item = xbmcgui.ListItem(label=name) # list_item.setArt({'icon': genre_info['icon'], 'fanart': genre_info['fanart']}) @@ -146,22 +155,39 @@ def get_addon_video_info(url): soup = BeautifulSoup(html, "html.parser") title = soup.find("div", id="embed_header").h2.string - thumbnail = soup.find("meta", property="og:image")["content"] + thumbnail = soup.find("meta", property="og:image") + if thumbnail is not None: + thumbnail = thumbnail["content"] sidestats_popularity = soup.find_all('dl', 'sidestats')[0].find_all('dd') views = sidestats_popularity[0].string - faves = sidestats_popularity[1].a.string + faves = sidestats_popularity[1].a + if faves is not None: + faves = faves.string + else: + faves = "0" votes = sidestats_popularity[2].string - score = sidestats_popularity[3].span.string + if votes is None: + votes = "0" + try: + score = sidestats_popularity[3].span.string + except IndexError: + score = 0 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 - genre = sidestats_date_and_genre[2].a.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() - authors = [author.string for author in soup.find('ul', 'authorlinks').find_all('a') if author.get("class") is None] + 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(soup.find_all('h2', 'rated-a')) != 0: rating = 'a' elif len(soup.find_all('h2', 'rated-m')) != 0: @@ -222,7 +248,25 @@ def get_video_urls( return [tag["href"] for tag in soup.find_all("a", "inline-card-portalsubmission")] -def list_videos(genre, category=0, offset=0): +def search(term, offset=0): + response = requests.get( + "https://www.newgrounds.com/search/conduct/movies", + params={ + "suitabilities": "etm", + "terms": term, + "page": offset, + "inner": 1, + }, + headers={ + "X-Requested-With": "XMLHttpRequest", + }, + ).json() + + soup = BeautifulSoup(response["content"], "html.parser") + return [tag["href"] for tag in soup.find_all("a", "item-portalsubmission")] + + +def list_videos(video_urls, title=None, next_page=None): """ Create the list of playable videos in the Kodi interface. @@ -230,22 +274,22 @@ def list_videos(genre, category=0, offset=0): :type genre: str :type offset: int """ - urls = get_video_urls(category=category, interval="all", offset=offset) videos = [] with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: # Start the load operations and mark each future with its URL - future_to_url = {executor.submit(get_addon_video_info, url): url for url in urls} + future_to_url = {executor.submit(get_addon_video_info, url): url for url in video_urls} for future in concurrent.futures.as_completed(future_to_url): url = future_to_url[future] videos.append(future.result()) - xbmcplugin.setPluginCategory(HANDLE, "Featured") # , genre_info['genre']) + xbmcplugin.setPluginCategory(HANDLE, title) xbmcplugin.setContent(HANDLE, "movies") # Get the list of videos in the category. # videos = genre_info['movies'] # Iterate through videos. for video in videos: list_item = xbmcgui.ListItem(label=video["title"]) - list_item.setArt({"thumb": video["thumbnail"]}) + if video["thumbnail"] is not None: + list_item.setArt({"thumb": video["thumbnail"]}) info_tag = list_item.getVideoInfoTag() info_tag.setMediaType("movie") @@ -279,13 +323,13 @@ def list_videos(genre, category=0, offset=0): xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder) # Add a "continue" folder. - list_item = xbmcgui.ListItem(label="Next Page") - info_tag = list_item.getVideoInfoTag() - info_tag.setMediaType("video") - info_tag.setTitle("Next Page") - url = get_url(action="listing", genre=genre, category=category, offset=offset + 20) - is_folder = True - xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder) + if next_page is not None: + list_item = xbmcgui.ListItem(label="Next Page") + info_tag = list_item.getVideoInfoTag() + info_tag.setMediaType("video") + info_tag.setTitle("Next Page") + is_folder = True + xbmcplugin.addDirectoryItem(HANDLE, next_page, list_item, is_folder) xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE) xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_VIDEO_YEAR) @@ -310,13 +354,31 @@ def router(paramstring): if not params: list_genres() elif params["action"] == "listing" and "category" in params: + offset = int(params.get("offset", 0)) + video_urls = get_video_urls(category=params["category"], interval="all", offset=offset) + next_page = get_url(action="listing", genre=params["genre"], category=params["category"], offset=offset + 20) list_videos( + video_urls, params["genre"], - category=params["category"], - offset=int(params.get("offset", 0)), + 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", 0)) + video_urls = search(term, offset=offset) + next_page = get_url(action="search", term=term, offset=offset + 1) + list_videos( + video_urls, + "Search Results: {}".format(term), + next_page + ) elif params["action"] == "play": play_video(params["video"]) else: |