-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathrating.py
More file actions
245 lines (208 loc) · 9.24 KB
/
rating.py
File metadata and controls
245 lines (208 loc) · 9.24 KB
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
# -*- coding: utf-8 -*-
"""Module used to launch rating dialogues and send ratings to Trakt"""
import xbmcaddon
import xbmcgui
from resources.lib import utilities
from resources.lib import kodiUtilities
from typing import Dict, List, Optional, Any, Union
from resources.lib import globals
import logging
logger = logging.getLogger(__name__)
__addon__ = xbmcaddon.Addon("script.trakt")
def ratingCheck(media_type: str, items_to_rate: List[Dict], watched_time: float, total_time: float) -> None:
"""Check if a video should be rated and if so launches the rating dialog"""
logger.debug("Rating Check called for '%s'" % media_type)
if not kodiUtilities.getSettingAsBool("rate_%s" % media_type):
logger.debug("'%s' is configured to not be rated." % media_type)
return
if items_to_rate is None:
logger.debug("Summary information is empty, aborting.")
return
watched = (watched_time / total_time) * 100
if watched >= kodiUtilities.getSettingAsFloat("rate_min_view_time"):
rateMedia(media_type, items_to_rate)
else:
logger.debug("'%s' does not meet minimum view time for rating (watched: %0.2f%%, minimum: %0.2f%%)" % (
media_type, watched, kodiUtilities.getSettingAsFloat("rate_min_view_time")))
def rateMedia(media_type: str, itemsToRate: List[Dict], unrate: bool = False, rating: Optional[Union[int, str]] = None) -> None:
"""Launches the rating dialog"""
for summary_info in itemsToRate:
if not utilities.isValidMediaType(media_type):
logger.debug("Not a valid media type")
return
elif 'user' not in summary_info:
logger.debug("No user data")
return
s = utilities.getFormattedItemName(media_type, summary_info)
logger.debug("Summary Info %s" % summary_info)
if unrate:
rating = None
if summary_info['user']['ratings']['rating'] > 0:
rating = 0
if rating is not None:
logger.debug("'%s' is being unrated." % s)
__rateOnTrakt(rating, media_type, summary_info, unrate=True)
else:
logger.debug("'%s' has not been rated, so not unrating." % s)
return
rerate = kodiUtilities.getSettingAsBool('rate_rerate')
if rating is not None:
if summary_info['user']['ratings']['rating'] == 0:
logger.debug(
"Rating for '%s' is being set to '%d' manually." % (s, rating))
__rateOnTrakt(rating, media_type, summary_info)
else:
if rerate:
if not summary_info['user']['ratings']['rating'] == rating:
logger.debug(
"Rating for '%s' is being set to '%d' manually." % (s, rating))
__rateOnTrakt(rating, media_type, summary_info)
else:
kodiUtilities.notification(
kodiUtilities.getString(32043), s)
logger.debug(
"'%s' already has a rating of '%d'." % (s, rating))
else:
kodiUtilities.notification(
kodiUtilities.getString(32041), s)
logger.debug("'%s' is already rated." % s)
return
if summary_info['user']['ratings'] and summary_info['user']['ratings']['rating']:
if not rerate:
logger.debug("'%s' has already been rated." % s)
kodiUtilities.notification(kodiUtilities.getString(32041), s)
return
else:
logger.debug("'%s' is being re-rated." % s)
gui = RatingDialog(
"script-trakt-RatingDialog.xml",
__addon__.getAddonInfo('path'),
media_type,
summary_info,
rerate
)
gui.doModal()
if gui.rating:
rating = gui.rating
if rerate:
if summary_info['user']['ratings'] and summary_info['user']['ratings']['rating'] > 0 and rating == summary_info['user']['ratings']['rating']:
rating = 0
if rating == 0 or rating == "unrate":
__rateOnTrakt(rating, gui.media_type, gui.media, unrate=True)
else:
__rateOnTrakt(rating, gui.media_type, gui.media)
else:
logger.debug("Rating dialog was closed with no rating.")
del gui
# Reset rating and unrate for multi part episodes
unrate = False
rating = None
def __rateOnTrakt(rating: Union[int, str], media_type: str, media: Dict, unrate: bool = False) -> None:
logger.debug("Sending rating (%s) to Trakt.tv" % rating)
params = media
if utilities.isMovie(media_type):
key = 'movies'
params['rating'] = rating
if 'movieid' in media:
kodiUtilities.kodiJsonRequest({"jsonrpc": "2.0", "id": 1, "method": "VideoLibrary.SetMovieDetails", "params": {
"movieid": media['movieid'], "userrating": rating}})
elif utilities.isShow(media_type):
key = 'shows'
# we need to remove this key or trakt will be confused
del(params["seasons"])
params['rating'] = rating
if 'tvshowid' in media:
kodiUtilities.kodiJsonRequest({"jsonrpc": "2.0", "id": 1, "method": "VideoLibrary.SetTVShowDetails", "params": {
"tvshowid": media['tvshowid'], "userrating": rating}})
elif utilities.isSeason(media_type):
key = 'shows'
params['seasons'] = [{'rating': rating, 'number': media['season']}]
elif utilities.isEpisode(media_type):
key = 'episodes'
params['rating'] = rating
if 'episodeid' in media:
kodiUtilities.kodiJsonRequest({"jsonrpc": "2.0", "id": 1, "method": "VideoLibrary.SetEpisodeDetails", "params": {
"episodeid": media['episodeid'], "userrating": rating}})
else:
return
root = {key: [params]}
if not unrate:
data = globals.traktapi.addRating(root)
else:
data = globals.traktapi.removeRating(root)
if data:
s = utilities.getFormattedItemName(media_type, media)
if 'not_found' in data and not data['not_found']['movies'] and not data['not_found']['episodes'] and not data['not_found']['shows']:
if not unrate:
kodiUtilities.notification(kodiUtilities.getString(32040), s)
else:
kodiUtilities.notification(kodiUtilities.getString(32042), s)
else:
kodiUtilities.notification(kodiUtilities.getString(32044), s)
class RatingDialog(xbmcgui.WindowXMLDialog):
media_type: str
media: Dict
rating: Optional[Union[int, str]]
rerate: bool
default_rating: int
buttons = {
11030: 1,
11031: 2,
11032: 3,
11033: 4,
11034: 5,
11035: 6,
11036: 7,
11037: 8,
11038: 9,
11039: 10
}
focus_labels = {
11030: 32028,
11031: 32029,
11032: 32030,
11033: 32031,
11034: 32032,
11035: 32033,
11036: 32034,
11037: 32035,
11038: 32036,
11039: 32027
}
def __init__(self, xmlFile: str, resourcePath: str, media_type: str, media: Dict, rerate: bool) -> None:
self.media_type = media_type
self.media = media
self.rating = None
self.rerate = rerate
self.default_rating = kodiUtilities.getSettingAsInt('rating_default')
def __new__(cls, xmlFile: str, resourcePath: str, media_type: str, media: Dict, rerate: bool) -> Any:
return super(RatingDialog, cls).__new__(cls, xmlFile, resourcePath)
def onInit(self) -> None:
s = utilities.getFormattedItemName(self.media_type, self.media)
self.getControl(10012).setLabel(s)
rateID = 11029 + self.default_rating
if self.rerate and self.media['user']['ratings'] and int(self.media['user']['ratings']['rating']) > 0:
rateID = 11029 + int(self.media['user']['ratings']['rating'])
self.setFocus(self.getControl(rateID))
def onClick(self, controlID: int) -> None:
if controlID in self.buttons:
self.rating = self.buttons[controlID]
self.close()
def onFocus(self, controlID: int) -> None:
if controlID in self.focus_labels:
s = kodiUtilities.getString(self.focus_labels[controlID])
if self.rerate:
if self.media['user']['ratings'] and self.media['user']['ratings']['rating'] == self.buttons[controlID]:
if utilities.isMovie(self.media_type):
s = kodiUtilities.getString(32037)
elif utilities.isShow(self.media_type):
s = kodiUtilities.getString(32038)
elif utilities.isEpisode(self.media_type):
s = kodiUtilities.getString(32039)
elif utilities.isSeason(self.media_type):
s = kodiUtilities.getString(32132)
else:
pass
self.getControl(10013).setLabel(s)
else:
self.getControl(10013).setLabel('')