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
|
# 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 os
import sys
from urllib.parse import urlencode, parse_qsl
from newgrounds import Grouping, Newgrounds, MOVIE_CATEGORIES
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 f"{URL}?{urlencode(kwargs)}"
def authentication_specified():
addon = Addon()
username = addon.getSetting("newgrounds.username")
password = addon.getSetting("newgrounds.password")
return username != "" and password != ""
def get_session():
addon = Addon()
session = addon.getSetting("newgrounds.session")
if session is not None: # and test_logged_in(session):
return {SESSION_COOKIE_NAME: session}
username = addon.getSetting("newgrounds.username")
password = addon.getSetting("newgrounds.password")
if username == "" or password == "":
return {}
session = open_new_session(username, password)
addon.setSetting("newgrounds.session", session)
return {SESSION_COOKIE_NAME: session}
class NewgroundsPlugin(object):
def __init__(self):
self.ng = Newgrounds()
def invoke(paramstring):
ng = NewgroundsPlugin()
params = dict(parse_qsl(paramstring))
if "action" in params:
method = getattr(ng, params["action"])
if method is None:
raise ValueError(f"Invalid paramstring: {paramstring}!")
else:
method = ng.default
method(**params)
def default(self, **kwargs):
self.list_front_page()
def prompt_search(self, **kwargs):
dialog = xbmcgui.Dialog()
term = dialog.input(heading="Search")
if term is not None:
url = get_url(action="search", term=term)
xbmc.executebuiltin("Container.Update(%s)" % url)
def search(self, **kwargs):
assert "term" in kwargs
term = kwargs["term"]
offset = int(kwargs.get("offset", 1))
video_urls = self.ng.search(term, offset=offset)
next_page = get_url(action="search", term=term, offset=offset + 1)
self._list_videos(video_urls, "Search Results: {}".format(term), next_page)
def list_front_page(self, **kwargs):
"""
Create the list of entrypoints in the Kodi interface.
"""
xbmcplugin.setPluginCategory(HANDLE, "Newgrounds")
xbmcplugin.setContent(HANDLE, "movies")
groupings = {
"Featured": Grouping.FEATURED,
"Latest": Grouping.LATEST,
"Popular": Grouping.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 displaying the user's feed
if authentication_specified():
list_item = xbmcgui.ListItem(label="Your Feed")
info_tag = list_item.getVideoInfoTag()
info_tag.setMediaType("video")
info_tag.setTitle("Your Feed")
url = get_url(action="list_feed")
is_folder = True
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
for name, enum in groupings.items():
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="list_categories", grouping=enum.value)
is_folder = True
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_NONE)
xbmcplugin.endOfDirectory(HANDLE)
def list_categories(self, **kwargs):
"""
Create the list of movie categories in the Kodi interface.
"""
grouping = kwargs.get("grouping", Grouping.FEATURED.value)
xbmcplugin.setPluginCategory(HANDLE, "Categories")
xbmcplugin.setContent(HANDLE, "movies")
for name in MOVIE_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="list_videos", grouping=grouping, category=name)
is_folder = True
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_NONE)
xbmcplugin.endOfDirectory(HANDLE)
def list_videos(self, **kwargs):
"""
Create the list of playable videos in the Kodi interface.
"""
grouping = int(kwargs.get("grouping", 0))
offset = int(kwargs.get("offset", 0))
category = kwargs.get("category", "All")
videos = self.ng.front_page(
grouping=int(grouping),
interval="all",
category=category,
offset=offset,
)
next_page = get_url(
action="list_videos",
grouping=grouping,
category=category,
offset=offset + 20,
)
self._list_videos(videos, f"{category} - {grouping}", next_page)
# TODO: Support offsets.
def list_feed(self, **kwargs):
# offset = int(kwargs.get("offset", 1))
# next_page = get_url(action="list_series", offset=offset + 1)
addon = Addon()
username = addon.getSetting("newgrounds.username")
password = addon.getSetting("newgrounds.password")
self.ng.login(username, password)
videos = self.ng.feed()
self._list_videos(videos, f"Feed - {username}")
def list_series(self, **kwargs):
offset = int(kwargs.get("offset", 1))
cards = self.ng.series(offset=offset)
next_page = get_url(action="list_series", offset=offset + 1)
self._list_cards(
title="Series",
cards=cards,
next_page=next_page,
)
def list_collections(self, **kwargs):
offset = int(kwargs.get("offset", 1))
cards = self.ng.collections(offset=offset)
next_page = get_url(action="list_collections", offset=offset + 1)
self._list_cards(
title="Collections",
cards=cards,
next_page=next_page,
)
# TODO: Support offsets.
def list_playlist(self, **kwargs):
assert "url" in kwargs
cards = self.ng.playlist_entries(kwargs["url"])
# next_page = get_url(action="list_playlist", offset=offset + 1)
self._list_cards(
title="Series",
cards=cards,
# next_page=next_page,
)
def _list_cards(self, cards, next_page=None, title=""):
"""
Create the list of folders in the Kodi interface.
"""
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="list_playlist", 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 _list_videos(self, videos, title="Videos", next_page=None):
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.suitability == "a":
info_tag.setMpaa("NC-17")
elif video.suitability == "m":
info_tag.setMpaa("R")
elif video.suitability == "t":
info_tag.setMpaa("PG-13")
else:
info_tag.setMpaa("PG")
list_item.setProperty("IsPlayable", "true")
url = get_url(action="play_video", video=video.content_url)
is_folder = False
xbmcplugin.addDirectoryItem(HANDLE, url, list_item, is_folder)
# Add a "continue" folder.
if next_page is not None and len(videos) > 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(self, **kwargs):
assert "video" in kwargs
path = kwargs["video"]
play_item = xbmcgui.ListItem(path=path)
play_item.setProperty("IsPlayable", "true")
xbmcplugin.setResolvedUrl(HANDLE, True, play_item)
if __name__ == "__main__":
NewgroundsPlugin.invoke(sys.argv[2][1:])
|