summaryrefslogtreecommitdiff
path: root/addon.py
blob: 6dd59a8b50f09005f3352c01ad71c7a027fdd600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# Copyright © 2024 Jakob L. Kreuze <zerodaysfordays@sdf.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""
Video plugin for animations posted to Newgrounds.
"""

import concurrent.futures
from datetime import datetime
import dateutil
import json
import os
import sys
from urllib.parse import urlencode, parse_qsl

from bs4 import BeautifulSoup
import requests
import xbmc
import xbmcgui
import xbmcplugin
from xbmcaddon import Addon
from xbmcvfs import translatePath

URL = sys.argv[0]
HANDLE = int(sys.argv[1])

ADDON_PATH = translatePath(Addon().getAddonInfo("path"))
ICONS_DIR = os.path.join(ADDON_PATH, "resources", "images", "icons")
FANART_DIR = os.path.join(ADDON_PATH, "resources", "images", "fanart")


def get_url(**kwargs):
    """
    Create a URL for calling the plugin recursively from the given set of keyword arguments.

    :param kwargs: "argument=value" pairs
    :return: plugin call URL
    :rtype: str
    """
    return "{}?{}".format(URL, urlencode(kwargs))


def get_series(official=True, sort_by="date", offset=1):
    """
    Return a list of series on Newgrounds

    :param official: whether to exclude non-official series
    :param sort_by: one of "date", "views"
    :param offset: pagination offset
    :return: list of video list entries
    :rtype: list
    """
    html = requests.get(
        "https://www.newgrounds.com/series",
        params={
            "filter": "all" if not official else None,
            "order": sort_by,
            "page": offset,
        },
    ).text
    soup = BeautifulSoup(html, "html.parser")
    visual_links = [
        json.loads(li["data-visual-link"])
        for li in soup.find_all("li", "visual-link-container")
    ]
    return fetch_visual_links(visual_links)


def get_collections(official=True, sort_by="date", offset=1):
    """
    Return a list of playlists on Newgrounds

    :param official: whether to exclude non-official collections
    :param sort_by: one of "date", "views"
    :param offset: pagination offset
    :return: list of video list entries
    :rtype: list
    """
    html = requests.get(
        "https://www.newgrounds.com/collections",
        params={
            "filter": "all" if not official else None,
            "order": sort_by,
            "page": offset,
        },
    ).text
    soup = BeautifulSoup(html, "html.parser")
    visual_links = [
        json.loads(li["data-visual-link"])
        for li in soup.find_all("li", "visual-link-container")
    ]
    return fetch_visual_links(visual_links)


def get_series_videos(url):
    """
    Return videos in a series on Newgrounds

    :param url: URL of series page to scrape
    :return: list of video list entries
    :rtype: list
    """
    html = requests.get(url).text
    soup = BeautifulSoup(html, "html.parser")
    visual_links = [
        json.loads(li["data-visual-link"])
        for li in soup.find_all("li", "visual-link-container")
    ]
    return fetch_visual_links(visual_links)


def fetch_visual_links(visual_links):
    """
    Fetch a list of video playlist entries.

    :param kwargs: "argument=value" pairs
    :return: parsed out video metadata
    :rtype: list
    """

    r = requests.post(
        "https://www.newgrounds.com/visual-links-fetch",
        params={
            "X-Requested-With": "XMLHttpRequest",
        },
        data={
            "ids": json.dumps(visual_links).replace(" ", ""),
            "component_params[include_author]": "1",
            "include_all_suitabilities": "0",
            "isAjaxRequest": "1",
        },
    )

    result = r.json()
    entries = []
    if isinstance(result["partials"], dict):
        for partial in result["partials"].values():
            if isinstance(partial, dict):
                for partial in partial.values():
                    soup = BeautifulSoup(partial, "html.parser")
                    title = soup.find("h4").string
                    thumbnail = soup.find("img")["src"]
                    author = soup.find("strong").string
                    url = soup.find("a")["href"]
                    entries.append(
                        {
                            "title": title,
                            "thumbnail": thumbnail,
                            "author": author,
                            "url": url,
                        }
                    )
    return entries


def list_top_level():
    """
    Create the list of entrypoints in the Kodi interface.
    """
    xbmcplugin.setPluginCategory(HANDLE, "Newgrounds")
    xbmcplugin.setContent(HANDLE, "movies")

    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="prompt_search")
    is_folder = False
    xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    # Add a dialog for listing the series running on Newgrounds
    list_item = xbmcgui.ListItem(label="Series")
    info_tag = list_item.getVideoInfoTag()
    info_tag.setMediaType("video")
    info_tag.setTitle("Series")
    url = get_url(action="series")
    is_folder = True
    xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    # Add a dialog for listing playlists
    list_item = xbmcgui.ListItem(label="Collections")
    info_tag = list_item.getVideoInfoTag()
    info_tag.setMediaType("video")
    info_tag.setTitle("Collections")
    url = get_url(action="collections")
    is_folder = True
    xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    for name in genres:
        list_item = xbmcgui.ListItem(label=name)

        info_tag = list_item.getVideoInfoTag()
        info_tag.setMediaType("video")
        info_tag.setTitle(name)
        info_tag.setGenres([name])

        url = get_url(action="listing", genre=name)
        is_folder = True

        xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
    xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_NONE)
    xbmcplugin.endOfDirectory(HANDLE)


def list_categories(genre="Featured"):
    """
    Create the list of movie categories in the Kodi interface.
    """
    xbmcplugin.setPluginCategory(HANDLE, "Newgrounds")
    xbmcplugin.setContent(HANDLE, "movies")

    categories = {
        "All": 0,
        "Action": 45,
        "Comedy - Original": 60,
        "Comedy - Parody": 61,
        "Drama": 47,
        "Experimental": 49,
        "Informative": 48,
        "Music Video": 50,
        "Other": 51,
        "Spam": 55,
    }

    for name in categories.keys():
        list_item = xbmcgui.ListItem(label=name)
        info_tag = list_item.getVideoInfoTag()
        info_tag.setMediaType("video")
        info_tag.setTitle(name)
        info_tag.setGenres([name])
        url = get_url(action="listing", genre=genre, category=categories[name])
        is_folder = True
        xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
    xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_NONE)
    xbmcplugin.endOfDirectory(HANDLE)


def get_video_url(url):
    """
    Return the location of the stream for a movie.

    :param url: URL of Newgrounds movie page
    :return: stream URL
    :rtype: str
    """
    html = requests.get(url).text
    soup = BeautifulSoup(html, "html.parser")
    video_id = soup.find("meta", property="og:url")["content"].strip(
        "https://www.newgrounds.com/portal/view/"
    )
    response = requests.get(
        "https://www.newgrounds.com/portal/video/{}".format(video_id),
        headers={"X-Requested-With": "XMLHttpRequest"},
    ).json()
    preferences = ["1080p", "720p", "360p"]
    for preference in preferences:
        if preference in response["sources"]:
            source = next(
                filter(
                    lambda x: x["type"] == "video/mp4", response["sources"][preference]
                )
            )
            if source is not None:
                return source["src"]


def get_addon_video_info(url):
    """
    Scrape metadata from `url`.

    :param url: URL of Newgrounds movie page
    :return: dictionary of video metadata
    :rtype: dict
    """
    html = requests.get(url).text
    soup = BeautifulSoup(html, "html.parser")

    title = soup.find("div", id="embed_header").h2.string
    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")
    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.replace(",", ""))
        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")
    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:
        rating = "m"
    elif len(soup.find_all("h2", "rated-t")) != 0:
        rating = "t"
    else:
        rating = "e"

    return metadata


def get_frontpage_video_urls(
    grouping="Featured", interval="today", sort_by="date", category=0, offset=0
):
    """
    Return a list of movies from the Newgrounds frontpage.

    :param grouping: one of "Featured", "Latest", or "Popular"
    :param interval: one of "today", "yesterday", "week", "month", "year", "all"
    :param sort_by: one of "date", score", "views"
    :param category: numeric identifier for video category
    :param offset: pagination offset
    :return: list of video URLs
    :rtype: list
    """
    URLS = {
        "Featured": "https://www.newgrounds.com/movies/featured",
        "Latest": "https://www.newgrounds.com/movies/browse",
        "Popular": "https://www.newgrounds.com/movies/popular",
    }

    assert interval in ["today", "yesterday", "week", "month", "year", "all"]
    assert sort_by in ["date", "score", "views"]
    assert isinstance(category, int)
    assert isinstance(offset, int)

    url = URLS[grouping]
    response = requests.get(
        url,
        params={
            "interval": interval,
            "sort": sort_by,
            "genre": category,
            "isAjaxRequest": True,
            "offset": 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", "inline-card-portalsubmission")]


def search(term, sort_by="relevance", offset=1):
    """
    Return a list of movies matching a given search term.

    :param term: Term to search for
    :param sort_by: one of "relevance", "date-asc", "date-desc", "score-asc", "score-desc", "views-asc", "views-desc"
    :param offset: pagination offset
    :return: list of video URLs
    :rtype: list
    """
    assert isinstance(term, str)
    assert sort_by in [
        "relevance",
        "date-asc",
        "date-desc",
        "score-asc",
        "score-desc",
        "views-asc",
        "views-desc",
    ]
    assert isinstance(offset, int)

    response = requests.get(
        "https://www.newgrounds.com/search/conduct/movies",
        params={
            "suitabilities": "etm",
            "sort": sort_by,
            "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.

    :param genre_index: the index of genre in the list of movie genres
    :type genre: str
    :type offset: int
    """
    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 video_urls
        }
        for future in concurrent.futures.as_completed(future_to_url):
            url = future_to_url[future]
            videos.append(future.result())
    xbmcplugin.setPluginCategory(HANDLE, title)
    xbmcplugin.setContent(HANDLE, "movies")

    for video in videos:
        list_item = xbmcgui.ListItem(label=video["title"])
        if video["thumbnail"] is not None:
            list_item.setArt({"thumb": video["thumbnail"]})

        info_tag = list_item.getVideoInfoTag()
        info_tag.setMediaType("movie")
        info_tag.setTitle(video["title"])
        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(video["upload_date"].strftime("%Y-%m-%d"))
        info_tag.setTags(video["tags"])

        if video["rating"] == "a":
            info_tag.setMpaa("NC-17")
        elif video["rating"] == "m":
            info_tag.setMpaa("R")
        elif video["rating"] == "t":
            info_tag.setMpaa("PG-13")
        else:
            info_tag.setMpaa("PG")

        list_item.setProperty("IsPlayable", "true")

        url = get_url(action="play", video=video["url"])
        is_folder = False
        xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    # Add a "continue" folder.
    if next_page is not None and len(video_urls) > 0:
        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_NONE)
    xbmcplugin.endOfDirectory(HANDLE)


def list_cards(cards, title="Cards", next_page=None):
    """
    Create the list of folders in the Kodi interface.

    :param cards: card summaries from `fetch_visual_links`
    """
    videos = []
    xbmcplugin.setPluginCategory(HANDLE, title)
    xbmcplugin.setContent(HANDLE, "movies")

    for card in cards:
        list_item = xbmcgui.ListItem(label=card["title"])
        if card["thumbnail"] is not None:
            list_item.setArt({"thumb": card["thumbnail"]})

        info_tag = list_item.getVideoInfoTag()
        info_tag.setMediaType("movie")
        info_tag.setTitle(card["title"])
        info_tag.setDirectors([card["author"]])

        url = get_url(action="listing_series", url=card["url"])
        is_folder = True
        xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)

    # Add a "continue" folder.
    if next_page is not None and len(cards) > 0:
        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_NONE)
    xbmcplugin.endOfDirectory(HANDLE)


def play_video(path):
    play_item = xbmcgui.ListItem(path=get_video_url(path))
    play_item.setProperty("IsPlayable", "true")
    xbmcplugin.setResolvedUrl(HANDLE, True, play_item)


def router(paramstring):
    """
    Router function that calls other functions
    depending on the provided paramstring

    :param paramstring: URL encoded plugin paramstring
    :type paramstring: str
    """
    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(
            grouping=params["genre"],
            interval="all",
            category=params["category"],
            offset=offset,
        )
        next_page = get_url(
            action="listing",
            genre=params["genre"],
            category=params["category"],
            offset=offset + 20,
        )
        list_videos(video_urls, params["genre"], next_page)
    elif params["action"] == "listing":
        list_categories(params["genre"])
    elif params["action"] == "series":
        offset = int(params.get("offset", 1))
        cards = get_series(offset=offset)
        next_page = get_url(action="series", offset=offset + 1)
        list_cards(cards, "Series", next_page)
    elif params["action"] == "collections":
        offset = int(params.get("offset", 1))
        cards = get_collections(offset=offset)
        next_page = get_url(action="collections", offset=offset + 1)
        list_cards(cards, "Collections", next_page)
    elif params["action"] == "listing_series":
        cards = get_series_videos(params["url"])
        video_urls = [card["url"] for card in cards]
        list_videos(video_urls)
    elif params["action"] == "search":
        term = params["term"]
        offset = int(params.get("offset", 1))
        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:
        raise ValueError(f"Invalid paramstring: {paramstring}!")


if __name__ == "__main__":
    router(sys.argv[2][1:])