trackma 0.4__py2.7.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +19 -0
- EGG-INFO/SOURCES.txt +35 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/entry_points.txt +8 -0
- EGG-INFO/not-zip-safe +1 -0
- EGG-INFO/requires.txt +10 -0
- EGG-INFO/top_level.txt +1 -0
- trackma/__init__.py +0 -0
- trackma/__init__.pyc +0 -0
- trackma/accounts.py +110 -0
- trackma/accounts.pyc +0 -0
- trackma/data/anilist.jpg +0 -0
- trackma/data/hb.jpg +0 -0
- trackma/data/icon.png +0 -0
- trackma/data/mal.jpg +0 -0
- trackma/data/melative.jpg +0 -0
- trackma/data/vndb.jpg +0 -0
- trackma/data.py +585 -0
- trackma/data.pyc +0 -0
- trackma/engine.py +742 -0
- trackma/engine.pyc +0 -0
- trackma/extras/AnimeInfoExtractor.py +308 -0
- trackma/extras/AnimeInfoExtractor.pyc +0 -0
- trackma/extras/__init__.py +0 -0
- trackma/extras/__init__.pyc +0 -0
- trackma/extras/plex.py +100 -0
- trackma/extras/plex.pyc +0 -0
- trackma/lib/__init__.py +0 -0
- trackma/lib/__init__.pyc +0 -0
- trackma/lib/lib.py +162 -0
- trackma/lib/lib.pyc +0 -0
- trackma/lib/libanilist.py +355 -0
- trackma/lib/libanilist.pyc +0 -0
- trackma/lib/libhb.py +248 -0
- trackma/lib/libhb.pyc +0 -0
- trackma/lib/libmal.py +705 -0
- trackma/lib/libmal.pyc +0 -0
- trackma/lib/libmelative.py +213 -0
- trackma/lib/libmelative.pyc +0 -0
- trackma/lib/libvndb.py +373 -0
- trackma/lib/libvndb.pyc +0 -0
- trackma/messenger.py +42 -0
- trackma/messenger.pyc +0 -0
- trackma/tracker.py +301 -0
- trackma/tracker.pyc +0 -0
- trackma/ui/__init__.py +0 -0
- trackma/ui/__init__.pyc +0 -0
- trackma/ui/cli.py +712 -0
- trackma/ui/cli.pyc +0 -0
- trackma/ui/curses.py +1049 -0
- trackma/ui/curses.pyc +0 -0
- trackma/ui/gtkui.py +2360 -0
- trackma/ui/gtkui.pyc +0 -0
- trackma/ui/qtui.py +2120 -0
- trackma/ui/qtui.pyc +0 -0
- trackma/utils.py +299 -0
- trackma/utils.pyc +0 -0
trackma/engine.py
ADDED
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
# This file is part of wMAL.
|
|
2
|
+
#
|
|
3
|
+
# This program is free software: you can redistribute it and/or modify
|
|
4
|
+
# it under the terms of the GNU General Public License as published by
|
|
5
|
+
# the Free Software Foundation, either version 3 of the License, or
|
|
6
|
+
# (at your option) any later version.
|
|
7
|
+
#
|
|
8
|
+
# This program is distributed in the hope that it will be useful,
|
|
9
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
+
# GNU General Public License for more details.
|
|
12
|
+
#
|
|
13
|
+
# You should have received a copy of the GNU General Public License
|
|
14
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
import re
|
|
18
|
+
import os
|
|
19
|
+
import subprocess
|
|
20
|
+
from decimal import Decimal
|
|
21
|
+
|
|
22
|
+
import threading
|
|
23
|
+
import difflib
|
|
24
|
+
import time
|
|
25
|
+
import datetime
|
|
26
|
+
import webbrowser
|
|
27
|
+
import shlex
|
|
28
|
+
|
|
29
|
+
import messenger
|
|
30
|
+
import data
|
|
31
|
+
import tracker
|
|
32
|
+
import utils
|
|
33
|
+
import extras.AnimeInfoExtractor
|
|
34
|
+
|
|
35
|
+
class Engine:
|
|
36
|
+
"""
|
|
37
|
+
The engine is the controller that handles commands coming from
|
|
38
|
+
the user interface and then queries the Data Handler for the necessary data.
|
|
39
|
+
It doesn't control nor care about how the data is fetched by the Data Handler.
|
|
40
|
+
|
|
41
|
+
After instantiating this class, the :func:`start` must be run to initialize the engine.
|
|
42
|
+
Likewise, the :func:`unload` function must be called when you're done using the engine.
|
|
43
|
+
|
|
44
|
+
The account and mediatype can be changed later on the fly by calling :func:`reload`.
|
|
45
|
+
|
|
46
|
+
The **account** parameter is an account dictionary passed by an Account Manager
|
|
47
|
+
and is used to run the engine.
|
|
48
|
+
|
|
49
|
+
The **message_handler** is a reference to a messaging function for the engine
|
|
50
|
+
to send to. Optional.
|
|
51
|
+
"""
|
|
52
|
+
data_handler = None
|
|
53
|
+
tracker = None
|
|
54
|
+
config = dict()
|
|
55
|
+
msg = None
|
|
56
|
+
loaded = False
|
|
57
|
+
playing = False
|
|
58
|
+
|
|
59
|
+
name = 'Engine'
|
|
60
|
+
|
|
61
|
+
signals = { 'show_added': None,
|
|
62
|
+
'show_deleted': None,
|
|
63
|
+
'episode_changed': None,
|
|
64
|
+
'score_changed': None,
|
|
65
|
+
'status_changed': None,
|
|
66
|
+
'show_synced': None,
|
|
67
|
+
'queue_changed': None,
|
|
68
|
+
'playing': None,
|
|
69
|
+
'prompt_for_update': None, }
|
|
70
|
+
|
|
71
|
+
def __init__(self, account, message_handler=None):
|
|
72
|
+
"""Reads configuration file and asks the data handler for the API info."""
|
|
73
|
+
self.msg = messenger.Messenger(message_handler)
|
|
74
|
+
|
|
75
|
+
self._load(account)
|
|
76
|
+
self._init_data_handler()
|
|
77
|
+
|
|
78
|
+
def _load(self, account):
|
|
79
|
+
self.account = account
|
|
80
|
+
|
|
81
|
+
# Create home directory
|
|
82
|
+
utils.make_dir('')
|
|
83
|
+
self.configfile = utils.get_root_filename('config.json')
|
|
84
|
+
|
|
85
|
+
# Create user directory
|
|
86
|
+
userfolder = "%s.%s" % (account['username'], account['api'])
|
|
87
|
+
utils.make_dir(userfolder)
|
|
88
|
+
|
|
89
|
+
self.msg.info(self.name, 'Trackma v{0} - using account {1}({2}).'.format(
|
|
90
|
+
utils.VERSION, account['username'], account['api']))
|
|
91
|
+
self.msg.info(self.name, 'Reading config files...')
|
|
92
|
+
try:
|
|
93
|
+
self.config = utils.parse_config(self.configfile, utils.config_defaults)
|
|
94
|
+
except IOError:
|
|
95
|
+
raise utils.EngineFatal("Couldn't open config file.")
|
|
96
|
+
|
|
97
|
+
def _init_data_handler(self, mediatype=None):
|
|
98
|
+
# Create data handler
|
|
99
|
+
self.data_handler = data.Data(self.msg, self.config, self.account, mediatype)
|
|
100
|
+
self.data_handler.connect_signal('show_synced', self._data_show_synced)
|
|
101
|
+
self.data_handler.connect_signal('queue_changed', self._data_queue_changed)
|
|
102
|
+
|
|
103
|
+
# Record the API details
|
|
104
|
+
(self.api_info, self.mediainfo) = self.data_handler.get_api_info()
|
|
105
|
+
|
|
106
|
+
def _data_show_synced(self, show):
|
|
107
|
+
self._emit_signal('show_synced', show)
|
|
108
|
+
|
|
109
|
+
def _data_queue_changed(self, queue):
|
|
110
|
+
self._emit_signal('queue_changed', queue)
|
|
111
|
+
|
|
112
|
+
def _tracker_playing(self, showid, playing, episode):
|
|
113
|
+
show = self.get_show_info(showid)
|
|
114
|
+
self._emit_signal('playing', show, playing, episode)
|
|
115
|
+
|
|
116
|
+
def _tracker_update(self, showid, episode):
|
|
117
|
+
show = self.get_show_info(showid)
|
|
118
|
+
if self.config['tracker_update_prompt']:
|
|
119
|
+
self._emit_signal('prompt_for_update', show, episode)
|
|
120
|
+
else:
|
|
121
|
+
self.set_episode(show['id'], episode)
|
|
122
|
+
|
|
123
|
+
def _emit_signal(self, signal, *args):
|
|
124
|
+
try:
|
|
125
|
+
if self.signals[signal]:
|
|
126
|
+
self.signals[signal](*args)
|
|
127
|
+
except KeyError:
|
|
128
|
+
raise Exception("Call to undefined signal.")
|
|
129
|
+
|
|
130
|
+
def _get_tracker_list(self, filter_num=None):
|
|
131
|
+
tracker_list = []
|
|
132
|
+
if filter_num:
|
|
133
|
+
source_list = self.filter_list(filter_num)
|
|
134
|
+
else:
|
|
135
|
+
source_list = self.get_list()
|
|
136
|
+
|
|
137
|
+
for show in source_list:
|
|
138
|
+
tracker_list.append({'id': show['id'],
|
|
139
|
+
'title': show['title'],
|
|
140
|
+
'my_progress': show['my_progress'],
|
|
141
|
+
'type': None,
|
|
142
|
+
'titles': self.data_handler.get_show_titles(show),
|
|
143
|
+
}) # TODO types
|
|
144
|
+
|
|
145
|
+
return tracker_list
|
|
146
|
+
|
|
147
|
+
def _update_tracker(self):
|
|
148
|
+
if self.tracker:
|
|
149
|
+
self.tracker.update_list(self._get_tracker_list())
|
|
150
|
+
|
|
151
|
+
def _cleanup(self):
|
|
152
|
+
# If the engine wasn't closed for whatever reason, do it
|
|
153
|
+
if self.loaded:
|
|
154
|
+
self.msg.info(self.name, "Forcing exit...")
|
|
155
|
+
self.data_handler.unload(True)
|
|
156
|
+
self.loaded = False
|
|
157
|
+
|
|
158
|
+
def connect_signal(self, signal, callback):
|
|
159
|
+
try:
|
|
160
|
+
self.signals[signal] = callback
|
|
161
|
+
except KeyError:
|
|
162
|
+
raise utils.EngineFatal("Invalid signal.")
|
|
163
|
+
|
|
164
|
+
def set_message_handler(self, message_handler):
|
|
165
|
+
"""Changes the message handler function on the fly."""
|
|
166
|
+
self.msg = messenger.Messenger(message_handler)
|
|
167
|
+
self.data_handler.set_message_handler(self.msg)
|
|
168
|
+
|
|
169
|
+
def start(self):
|
|
170
|
+
"""
|
|
171
|
+
Starts the engine.
|
|
172
|
+
This function should be called before doing anything with the engine,
|
|
173
|
+
as it initializes the data handler.
|
|
174
|
+
"""
|
|
175
|
+
if self.loaded:
|
|
176
|
+
raise utils.TrackmaError("Already loaded.")
|
|
177
|
+
|
|
178
|
+
# Start the data handler
|
|
179
|
+
try:
|
|
180
|
+
(self.api_info, self.mediainfo) = self.data_handler.start()
|
|
181
|
+
except utils.DataError, e:
|
|
182
|
+
raise utils.DataFatal(e.message)
|
|
183
|
+
except utils.APIError, e:
|
|
184
|
+
raise utils.APIFatal(e.message)
|
|
185
|
+
|
|
186
|
+
# Start tracker
|
|
187
|
+
if self.mediainfo.get('can_play') and self.config['tracker_enabled']:
|
|
188
|
+
self.tracker = tracker.Tracker(self.msg,
|
|
189
|
+
self._get_tracker_list(),
|
|
190
|
+
self.config['tracker_process'],
|
|
191
|
+
self.config['searchdir'],
|
|
192
|
+
int(self.config['tracker_interval']),
|
|
193
|
+
int(self.config['tracker_update_wait_s']),
|
|
194
|
+
self.config['tracker_update_close'],
|
|
195
|
+
)
|
|
196
|
+
self.tracker.connect_signal('playing', self._tracker_playing)
|
|
197
|
+
self.tracker.connect_signal('update', self._tracker_update)
|
|
198
|
+
|
|
199
|
+
self.loaded = True
|
|
200
|
+
return True
|
|
201
|
+
|
|
202
|
+
def unload(self):
|
|
203
|
+
"""
|
|
204
|
+
Closes the data handler and closes the engine cleanly.
|
|
205
|
+
This should be called when closing the client application, or when you're
|
|
206
|
+
sure you're not going to use the engine anymore. This does all the necessary
|
|
207
|
+
procedures to close the data handler cleanly and then itself.
|
|
208
|
+
|
|
209
|
+
"""
|
|
210
|
+
if self.loaded:
|
|
211
|
+
self.msg.info(self.name, "Unloading...")
|
|
212
|
+
self.data_handler.unload()
|
|
213
|
+
|
|
214
|
+
self.loaded = False
|
|
215
|
+
|
|
216
|
+
def reload(self, account=None, mediatype=None):
|
|
217
|
+
"""Changes the API and/or mediatype and reloads itself."""
|
|
218
|
+
if self.loaded:
|
|
219
|
+
self.unload()
|
|
220
|
+
|
|
221
|
+
if account:
|
|
222
|
+
self._load(account)
|
|
223
|
+
|
|
224
|
+
self._init_data_handler(mediatype)
|
|
225
|
+
self.start()
|
|
226
|
+
|
|
227
|
+
def get_config(self, key):
|
|
228
|
+
"""Returns the specified key from the configuration."""
|
|
229
|
+
return self.config[key]
|
|
230
|
+
|
|
231
|
+
def get_userconfig(self, key):
|
|
232
|
+
return self.data_handler.userconfig[key]
|
|
233
|
+
|
|
234
|
+
def set_config(self, key, value):
|
|
235
|
+
"""
|
|
236
|
+
Writes the defined key to the configuration.
|
|
237
|
+
Note that this writes the configuration only to memory; when you're
|
|
238
|
+
done doing all necessary changes, make sure to write the configuration file
|
|
239
|
+
with :func:`save_config`."""
|
|
240
|
+
self.config[key] = value
|
|
241
|
+
|
|
242
|
+
def save_config(self):
|
|
243
|
+
"""Writes all configuration files to disk."""
|
|
244
|
+
|
|
245
|
+
# Save config file
|
|
246
|
+
utils.save_config(self.config, self.configfile)
|
|
247
|
+
|
|
248
|
+
def get_list(self):
|
|
249
|
+
"""
|
|
250
|
+
Returns the full show list requested from the data handler as a list of show dictionaries.
|
|
251
|
+
If you only need shows in a specified status, use :func:`filter_list`.
|
|
252
|
+
"""
|
|
253
|
+
return self.data_handler.get().itervalues()
|
|
254
|
+
|
|
255
|
+
def get_show_info(self, showid):
|
|
256
|
+
"""
|
|
257
|
+
Returns the show dictionary for the specified **showid**.
|
|
258
|
+
"""
|
|
259
|
+
showdict = self.data_handler.get()
|
|
260
|
+
|
|
261
|
+
try:
|
|
262
|
+
return showdict[showid]
|
|
263
|
+
except KeyError:
|
|
264
|
+
raise utils.EngineError("Show not found.")
|
|
265
|
+
|
|
266
|
+
def get_show_info_title(self, pattern):
|
|
267
|
+
showdict = self.data_handler.get()
|
|
268
|
+
# Do title lookup, slower
|
|
269
|
+
for k, show in showdict.iteritems():
|
|
270
|
+
if show['title'] == pattern:
|
|
271
|
+
return show
|
|
272
|
+
raise utils.EngineError("Show not found.")
|
|
273
|
+
|
|
274
|
+
def get_show_details(self, show):
|
|
275
|
+
"""
|
|
276
|
+
Returns detailed information about **show** requested from the data handler.
|
|
277
|
+
"""
|
|
278
|
+
return self.data_handler.info_get(show)
|
|
279
|
+
|
|
280
|
+
def regex_list(self, regex):
|
|
281
|
+
"""
|
|
282
|
+
It asks the data handler to do a regex search for a show and returns the
|
|
283
|
+
list of show dictionaries with all the matches.
|
|
284
|
+
"""
|
|
285
|
+
showlist = self.data_handler.get()
|
|
286
|
+
return list(v for k, v in showlist.iteritems() if re.match(regex, v['title'], re.I))
|
|
287
|
+
|
|
288
|
+
def regex_list_titles(self, pattern):
|
|
289
|
+
# TODO : Temporal hack for the client autocomplete function
|
|
290
|
+
showlist = self.data_handler.get()
|
|
291
|
+
newlist = list()
|
|
292
|
+
for k, v in showlist.iteritems():
|
|
293
|
+
if re.match(pattern, v['title'], re.I):
|
|
294
|
+
if ' ' in v['title']:
|
|
295
|
+
newlist.append('"' + v['title'] + '" ')
|
|
296
|
+
else:
|
|
297
|
+
newlist.append(v['title'] + ' ')
|
|
298
|
+
|
|
299
|
+
return newlist
|
|
300
|
+
|
|
301
|
+
def search(self, criteria):
|
|
302
|
+
"""
|
|
303
|
+
Request a remote list of shows matching the criteria
|
|
304
|
+
and returns it as a list of show dictionaries.
|
|
305
|
+
This is useful to add a show.
|
|
306
|
+
"""
|
|
307
|
+
return self.data_handler.search(str(criteria).strip())
|
|
308
|
+
|
|
309
|
+
def add_show(self, show, status=None):
|
|
310
|
+
"""
|
|
311
|
+
Adds **show** to the list and queues the list update
|
|
312
|
+
for the next sync.
|
|
313
|
+
"""
|
|
314
|
+
# Check if operation is supported by the API
|
|
315
|
+
if not self.mediainfo.get('can_add'):
|
|
316
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
317
|
+
|
|
318
|
+
# Set to the requested status
|
|
319
|
+
if status:
|
|
320
|
+
if status not in self.mediainfo['statuses']:
|
|
321
|
+
raise utils.EngineError('Invalid status.')
|
|
322
|
+
|
|
323
|
+
show['my_status'] = status
|
|
324
|
+
|
|
325
|
+
# Add in data handler
|
|
326
|
+
self.data_handler.queue_add(show)
|
|
327
|
+
|
|
328
|
+
# Update the tracker with the new information
|
|
329
|
+
self._update_tracker()
|
|
330
|
+
|
|
331
|
+
# Emit signal
|
|
332
|
+
self._emit_signal('show_added', show)
|
|
333
|
+
|
|
334
|
+
def set_episode(self, showid, newep):
|
|
335
|
+
"""
|
|
336
|
+
Updates the progress of the specified **showid** to **newep**
|
|
337
|
+
and queues the list update for the next sync.
|
|
338
|
+
"""
|
|
339
|
+
# Check if operation is supported by the API
|
|
340
|
+
if not self.mediainfo.get('can_update'):
|
|
341
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
342
|
+
|
|
343
|
+
# Check for the episode number
|
|
344
|
+
try:
|
|
345
|
+
newep = int(newep)
|
|
346
|
+
except ValueError:
|
|
347
|
+
raise utils.EngineError('Episode must be numeric.')
|
|
348
|
+
|
|
349
|
+
# Get the show info
|
|
350
|
+
show = self.get_show_info(showid)
|
|
351
|
+
# More checks
|
|
352
|
+
if show['total'] and newep > show['total']:
|
|
353
|
+
raise utils.EngineError('Episode out of limits.')
|
|
354
|
+
if show['my_progress'] == newep:
|
|
355
|
+
raise utils.EngineError("Show already at episode %d" % newep)
|
|
356
|
+
|
|
357
|
+
# Change episode
|
|
358
|
+
self.msg.info(self.name, "Updating show %s to episode %d..." % (show['title'], newep))
|
|
359
|
+
self.data_handler.queue_update(show, 'my_progress', newep)
|
|
360
|
+
|
|
361
|
+
# Emit signal
|
|
362
|
+
self._emit_signal('episode_changed', show)
|
|
363
|
+
|
|
364
|
+
# Change status if required
|
|
365
|
+
if self.config['auto_status_change'] and self.mediainfo.get('can_status'):
|
|
366
|
+
try:
|
|
367
|
+
if newep == show['total'] and self.mediainfo.get('status_finish'):
|
|
368
|
+
if (
|
|
369
|
+
not self.config['auto_status_change_if_scored'] or
|
|
370
|
+
not self.mediainfo.get('can_score') or
|
|
371
|
+
show['my_score']
|
|
372
|
+
):
|
|
373
|
+
# Change to finished status
|
|
374
|
+
self.set_status(show['id'], self.mediainfo['status_finish'])
|
|
375
|
+
else:
|
|
376
|
+
self.msg.warn(self.name, "Updated episode but status won't be changed until a score is set.")
|
|
377
|
+
elif newep == 1 and self.mediainfo.get('status_start'):
|
|
378
|
+
# Change to watching status
|
|
379
|
+
self.set_status(show['id'], self.mediainfo['status_start'])
|
|
380
|
+
except utils.EngineError, e:
|
|
381
|
+
# Only warn about engine errors since status change here is not crtical
|
|
382
|
+
self.msg.warn(self.name, 'Updated episode but status wasn\'t changed: %s' % e)
|
|
383
|
+
|
|
384
|
+
# Change dates if required
|
|
385
|
+
if self.config['auto_date_change'] and self.mediainfo.get('can_date'):
|
|
386
|
+
start_date = finish_date = None
|
|
387
|
+
|
|
388
|
+
try:
|
|
389
|
+
if newep == 1:
|
|
390
|
+
start_date = datetime.date.today()
|
|
391
|
+
if newep == show['total']:
|
|
392
|
+
finish_date = datetime.date.today()
|
|
393
|
+
|
|
394
|
+
self.set_dates(show['id'], start_date, finish_date)
|
|
395
|
+
except utils.EngineError, e:
|
|
396
|
+
# Only warn about engine errors since date change here is not crtical
|
|
397
|
+
self.msg.warn(self.name, 'Updated episode but dates weren\'t changed: %s' % e)
|
|
398
|
+
|
|
399
|
+
# Clear neweps flag
|
|
400
|
+
if self.data_handler.get_show_attr(show, 'neweps'):
|
|
401
|
+
self.data_handler.set_show_attr(show, 'neweps', False)
|
|
402
|
+
|
|
403
|
+
# Update the tracker with the new information
|
|
404
|
+
self._update_tracker()
|
|
405
|
+
|
|
406
|
+
return show
|
|
407
|
+
|
|
408
|
+
def set_dates(self, showid, start_date=None, finish_date=None):
|
|
409
|
+
"""
|
|
410
|
+
Updates the start date and finish date of a show.
|
|
411
|
+
If any of the two are None, it won't be changed.
|
|
412
|
+
"""
|
|
413
|
+
if not self.mediainfo.get('can_date'):
|
|
414
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
415
|
+
|
|
416
|
+
show = self.get_show_info(showid)
|
|
417
|
+
|
|
418
|
+
# Change the start date if required
|
|
419
|
+
if start_date:
|
|
420
|
+
if not isinstance(start_date, datetime.date):
|
|
421
|
+
raise utils.EngineError('start_date must be a Date object.')
|
|
422
|
+
self.data_handler.queue_update(show, 'my_start_date', start_date)
|
|
423
|
+
|
|
424
|
+
if finish_date:
|
|
425
|
+
if not isinstance(finish_date, datetime.date):
|
|
426
|
+
raise utils.EngineError('finish_date must be a Date object.')
|
|
427
|
+
self.data_handler.queue_update(show, 'my_finish_date', finish_date)
|
|
428
|
+
|
|
429
|
+
def set_score(self, showid, newscore):
|
|
430
|
+
"""
|
|
431
|
+
Updates the score of the specified **showid** to **newscore**
|
|
432
|
+
and queues the list update for the next sync.
|
|
433
|
+
"""
|
|
434
|
+
# Check if operation is supported by the API
|
|
435
|
+
if not self.mediainfo.get('can_score'):
|
|
436
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
437
|
+
|
|
438
|
+
# Check for the correctness of the score
|
|
439
|
+
if (Decimal(str(newscore)) % Decimal(str(self.mediainfo['score_step']))) != 0:
|
|
440
|
+
raise utils.EngineError('Invalid score.')
|
|
441
|
+
|
|
442
|
+
# Convert to proper type
|
|
443
|
+
if isinstance( self.mediainfo['score_step'], int ):
|
|
444
|
+
newscore = int(newscore)
|
|
445
|
+
else:
|
|
446
|
+
newscore = float(newscore)
|
|
447
|
+
|
|
448
|
+
# Get the show and update it
|
|
449
|
+
show = self.get_show_info(showid)
|
|
450
|
+
# More checks
|
|
451
|
+
if newscore > self.mediainfo['score_max']:
|
|
452
|
+
raise utils.EngineError('Score out of limits.')
|
|
453
|
+
if show['my_score'] == newscore:
|
|
454
|
+
raise utils.EngineError("Score already at %s" % newscore)
|
|
455
|
+
|
|
456
|
+
# Change score
|
|
457
|
+
self.msg.info(self.name, "Updating show %s to score %s..." % (show['title'], newscore))
|
|
458
|
+
self.data_handler.queue_update(show, 'my_score', newscore)
|
|
459
|
+
|
|
460
|
+
# Emit signal
|
|
461
|
+
self._emit_signal('score_changed', show)
|
|
462
|
+
|
|
463
|
+
# Change status if required
|
|
464
|
+
if (
|
|
465
|
+
show['total'] and
|
|
466
|
+
show['my_progress'] == show['total'] and
|
|
467
|
+
show['my_score'] and
|
|
468
|
+
self.mediainfo.get('can_status') and
|
|
469
|
+
self.config['auto_status_change'] and
|
|
470
|
+
self.config['auto_status_change_if_scored'] and
|
|
471
|
+
self.mediainfo.get('status_finish')
|
|
472
|
+
):
|
|
473
|
+
try:
|
|
474
|
+
self.set_status(show['id'], self.mediainfo['status_finish'])
|
|
475
|
+
except utils.EngineError, e:
|
|
476
|
+
# Only warn about engine errors since status change here is not crtical
|
|
477
|
+
self.msg.warn(self.name, 'Updated episode but status wasn\'t changed: %s' % e)
|
|
478
|
+
|
|
479
|
+
return show
|
|
480
|
+
|
|
481
|
+
def set_status(self, showid, newstatus):
|
|
482
|
+
"""
|
|
483
|
+
Updates the score of the specified **showid** to **newstatus** (number)
|
|
484
|
+
and queues the list update for the next sync.
|
|
485
|
+
"""
|
|
486
|
+
# Check if operation is supported by the API
|
|
487
|
+
if not self.mediainfo.get('can_status'):
|
|
488
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
489
|
+
|
|
490
|
+
try:
|
|
491
|
+
newstatus = int(newstatus)
|
|
492
|
+
except ValueError:
|
|
493
|
+
pass # It's not necessary for it to be an int
|
|
494
|
+
|
|
495
|
+
# Check if the status is valid
|
|
496
|
+
_statuses = self.mediainfo['statuses_dict']
|
|
497
|
+
if newstatus not in _statuses.keys():
|
|
498
|
+
raise utils.EngineError('Invalid status.')
|
|
499
|
+
|
|
500
|
+
# Get the show and update it
|
|
501
|
+
show = self.get_show_info(showid)
|
|
502
|
+
# More checks
|
|
503
|
+
if show['my_status'] == newstatus:
|
|
504
|
+
raise utils.EngineError("Show already in %s." % _statuses[newstatus])
|
|
505
|
+
|
|
506
|
+
# Change status
|
|
507
|
+
old_status = show['my_status']
|
|
508
|
+
self.msg.info(self.name, "Updating show %s status to %s..." % (show['title'], _statuses[newstatus]))
|
|
509
|
+
self.data_handler.queue_update(show, 'my_status', newstatus)
|
|
510
|
+
|
|
511
|
+
# Emit signal
|
|
512
|
+
self._emit_signal('status_changed', show, old_status)
|
|
513
|
+
|
|
514
|
+
return show
|
|
515
|
+
|
|
516
|
+
def delete_show(self, show):
|
|
517
|
+
"""
|
|
518
|
+
Deletes **show** completely from the list and queues the list update for the next sync.
|
|
519
|
+
"""
|
|
520
|
+
if not self.mediainfo.get('can_delete'):
|
|
521
|
+
raise utils.EngineError('Operation not supported by API.')
|
|
522
|
+
|
|
523
|
+
# Add in data handler
|
|
524
|
+
self.data_handler.queue_delete(show)
|
|
525
|
+
|
|
526
|
+
# Update the tracker with the new information
|
|
527
|
+
self._update_tracker()
|
|
528
|
+
|
|
529
|
+
# Emit signal
|
|
530
|
+
self._emit_signal('show_deleted', show)
|
|
531
|
+
|
|
532
|
+
def _search_video(self, titles, episode):
|
|
533
|
+
best_candidate = (None, 0, None)
|
|
534
|
+
|
|
535
|
+
matcher = difflib.SequenceMatcher()
|
|
536
|
+
|
|
537
|
+
# Check over video files and propose our best candidate
|
|
538
|
+
for (fullpath, filename) in utils.regex_find_videos('mkv|mp4|avi', self.config['searchdir']):
|
|
539
|
+
# Analyze what's the title and episode of the file
|
|
540
|
+
aie = extras.AnimeInfoExtractor.AnimeInfoExtractor(filename)
|
|
541
|
+
candidate_title = aie.getName()
|
|
542
|
+
candidate_episode_start, candidate_episode_end = aie.getEpisodeNumbers()
|
|
543
|
+
|
|
544
|
+
# Skip this file if we couldn't analyze it or it isn't the episode we want
|
|
545
|
+
if (
|
|
546
|
+
not candidate_title or
|
|
547
|
+
not (episode >= candidate_episode_start and episode <= candidate_episode_end) or
|
|
548
|
+
(candidate_episode_end == '' and episode != candidate_episode_start)
|
|
549
|
+
):
|
|
550
|
+
continue
|
|
551
|
+
|
|
552
|
+
matcher.set_seq1(candidate_title.lower())
|
|
553
|
+
|
|
554
|
+
# We remember to compare all titles (aliases and whatnot)
|
|
555
|
+
for requested_title in titles:
|
|
556
|
+
matcher.set_seq2(requested_title.lower())
|
|
557
|
+
ratio = matcher.ratio()
|
|
558
|
+
|
|
559
|
+
# Propose as our new candidate if its ratio is
|
|
560
|
+
# better than threshold and it's better than
|
|
561
|
+
# what we've seen yet
|
|
562
|
+
if ratio > 0.7 and ratio > best_candidate[1]:
|
|
563
|
+
best_candidate = (fullpath, ratio, aie.getEpisode())
|
|
564
|
+
|
|
565
|
+
return best_candidate[0], best_candidate[2]
|
|
566
|
+
|
|
567
|
+
def get_new_episodes(self, showlist):
|
|
568
|
+
results = list()
|
|
569
|
+
total = len(showlist)
|
|
570
|
+
t = time.time()
|
|
571
|
+
|
|
572
|
+
for i, show in enumerate(showlist):
|
|
573
|
+
self.msg.info(self.name, "Searching %d/%d..." % (i+1, total))
|
|
574
|
+
|
|
575
|
+
titles = self.data_handler.get_show_titles(show)
|
|
576
|
+
|
|
577
|
+
(filename, ep) = self._search_video(titles, show['my_progress']+1)
|
|
578
|
+
if filename:
|
|
579
|
+
self.data_handler.set_show_attr(show, 'neweps', True)
|
|
580
|
+
results.append(show)
|
|
581
|
+
|
|
582
|
+
self.msg.info(self.name, "Time: %s" % (time.time() - t))
|
|
583
|
+
return results
|
|
584
|
+
|
|
585
|
+
def library(self):
|
|
586
|
+
return self.data_handler.library_get()
|
|
587
|
+
|
|
588
|
+
def scan_library(self, my_status=None):
|
|
589
|
+
# Check if operation is supported by the API
|
|
590
|
+
if not self.mediainfo.get('can_play'):
|
|
591
|
+
raise utils.EngineError('Operation not supported by current site or mediatype.')
|
|
592
|
+
if not self.config['searchdir']:
|
|
593
|
+
raise utils.EngineError('Media directory is not set.')
|
|
594
|
+
if not utils.dir_exists(self.config['searchdir']):
|
|
595
|
+
raise utils.EngineError('The set media directory doesn\'t exist.')
|
|
596
|
+
|
|
597
|
+
t = time.time()
|
|
598
|
+
library = {}
|
|
599
|
+
library_cache = self.data_handler.library_cache_get()
|
|
600
|
+
|
|
601
|
+
if not my_status:
|
|
602
|
+
my_status = self.mediainfo['status_start']
|
|
603
|
+
|
|
604
|
+
self.msg.info(self.name, "Scanning local library...")
|
|
605
|
+
tracker_list = self._get_tracker_list(my_status)
|
|
606
|
+
|
|
607
|
+
# Do a full listing of the media directory
|
|
608
|
+
for fullpath, filename in utils.regex_find_videos('mkv|mp4|avi', self.config['searchdir']):
|
|
609
|
+
show_id = None
|
|
610
|
+
if filename in library_cache.keys():
|
|
611
|
+
# If the filename was already seen before
|
|
612
|
+
# use the cached information, if there's no information (None)
|
|
613
|
+
# then it means it doesn't correspond to any show in the list
|
|
614
|
+
# and can be safely skipped.
|
|
615
|
+
if library_cache[filename]:
|
|
616
|
+
show_id = library_cache[filename][0]
|
|
617
|
+
show_ep = library_cache[filename][1]
|
|
618
|
+
else:
|
|
619
|
+
continue
|
|
620
|
+
else:
|
|
621
|
+
# If the filename has not been seen, extract
|
|
622
|
+
# the information from the filename and do a fuzzy search
|
|
623
|
+
# on the user's list. Cache the information.
|
|
624
|
+
# If it fails, cache it as None.
|
|
625
|
+
aie = extras.AnimeInfoExtractor.AnimeInfoExtractor(filename)
|
|
626
|
+
(show_title, show_ep) = (aie.getName(), aie.getEpisode())
|
|
627
|
+
if show_title:
|
|
628
|
+
show = utils.guess_show(show_title, tracker_list)
|
|
629
|
+
if show:
|
|
630
|
+
show_id = show['id']
|
|
631
|
+
library_cache[filename] = (show['id'], show_ep)
|
|
632
|
+
else:
|
|
633
|
+
library_cache[filename] = None
|
|
634
|
+
else:
|
|
635
|
+
library_cache[filename] = None
|
|
636
|
+
|
|
637
|
+
# After we got our information, add it to our library
|
|
638
|
+
if show_id:
|
|
639
|
+
if show_id not in library.keys():
|
|
640
|
+
library[show_id] = {}
|
|
641
|
+
library[show_id][show_ep] = fullpath
|
|
642
|
+
|
|
643
|
+
self.msg.debug(self.name, "Time: %s" % (time.time() - t))
|
|
644
|
+
self.data_handler.library_save(library)
|
|
645
|
+
self.data_handler.library_cache_save(library_cache)
|
|
646
|
+
return library
|
|
647
|
+
|
|
648
|
+
def play_episode(self, show, playep=0):
|
|
649
|
+
"""
|
|
650
|
+
Does a local search in the hard disk (in the folder specified by the config file)
|
|
651
|
+
for the specified episode (**playep**) for the specified **show**.
|
|
652
|
+
|
|
653
|
+
If no **playep** is specified, the next episode of the show will be played.
|
|
654
|
+
"""
|
|
655
|
+
# Check if operation is supported by the API
|
|
656
|
+
if not self.mediainfo.get('can_play'):
|
|
657
|
+
raise utils.EngineError('Operation not supported by current site or mediatype.')
|
|
658
|
+
if not self.config['searchdir']:
|
|
659
|
+
raise utils.EngineError('Media directory is not set.')
|
|
660
|
+
if not utils.dir_exists(self.config['searchdir']):
|
|
661
|
+
raise utils.EngineError('The set media directory doesn\'t exist.')
|
|
662
|
+
|
|
663
|
+
try:
|
|
664
|
+
playep = int(playep)
|
|
665
|
+
except ValueError:
|
|
666
|
+
raise utils.EngineError('Episode must be numeric.')
|
|
667
|
+
|
|
668
|
+
if show:
|
|
669
|
+
playing_next = False
|
|
670
|
+
if not playep:
|
|
671
|
+
playep = show['my_progress'] + 1
|
|
672
|
+
playing_next = True
|
|
673
|
+
|
|
674
|
+
if show['total'] and playep > show['total']:
|
|
675
|
+
raise utils.EngineError('Episode beyond limits.')
|
|
676
|
+
|
|
677
|
+
self.msg.info(self.name, "Searching for %s %s..." % (show['title'], playep))
|
|
678
|
+
|
|
679
|
+
titles = self.data_handler.get_show_titles(show)
|
|
680
|
+
|
|
681
|
+
filename, endep = self._search_video(titles, playep)
|
|
682
|
+
if filename:
|
|
683
|
+
self.msg.info(self.name, 'Found. Starting player...')
|
|
684
|
+
arg_list = shlex.split(self.config['player'])
|
|
685
|
+
arg_list.append(filename)
|
|
686
|
+
try:
|
|
687
|
+
with open(os.devnull, 'wb') as DEVNULL:
|
|
688
|
+
subprocess.Popen(arg_list, stdout=DEVNULL, stderr=DEVNULL)
|
|
689
|
+
except OSError:
|
|
690
|
+
raise utils.EngineError('Player not found, check your config.json')
|
|
691
|
+
return endep
|
|
692
|
+
else:
|
|
693
|
+
raise utils.EngineError('Episode file not found.')
|
|
694
|
+
|
|
695
|
+
def undoall(self):
|
|
696
|
+
"""Clears the data handler queue and discards any unsynced change."""
|
|
697
|
+
return self.data_handler.queue_clear()
|
|
698
|
+
|
|
699
|
+
def altname(self, showid, newname=None):
|
|
700
|
+
"""
|
|
701
|
+
If **newname** is specified, it gets the alternate name of **showid**.
|
|
702
|
+
Otherwise, it sets the alternate name of **showid** to **newname**.
|
|
703
|
+
"""
|
|
704
|
+
if newname is not None:
|
|
705
|
+
if newname == '':
|
|
706
|
+
self.data_handler.altname_clear(showid)
|
|
707
|
+
self.msg.info(self.name, 'Cleared alternate name.')
|
|
708
|
+
else:
|
|
709
|
+
self.data_handler.altname_set(showid, newname)
|
|
710
|
+
self.msg.info(self.name, 'Changed alternate name to %s.' % newname)
|
|
711
|
+
else:
|
|
712
|
+
return self.data_handler.altname_get(showid)
|
|
713
|
+
|
|
714
|
+
def altnames(self):
|
|
715
|
+
"""
|
|
716
|
+
Gets a dictionary of all set alternative names.
|
|
717
|
+
"""
|
|
718
|
+
return self.data_handler.altnames_get()
|
|
719
|
+
|
|
720
|
+
def filter_list(self, status_num):
|
|
721
|
+
"""
|
|
722
|
+
Returns a show list with the shows in the specified **status_num** status.
|
|
723
|
+
If you need a list with all the shows, use :func:`get_list`.
|
|
724
|
+
"""
|
|
725
|
+
showlist = self.data_handler.get()
|
|
726
|
+
return list(v for k, v in showlist.iteritems() if v['my_status'] == status_num)
|
|
727
|
+
|
|
728
|
+
def list_download(self):
|
|
729
|
+
"""Asks the data handler to download the remote list."""
|
|
730
|
+
self.undoall()
|
|
731
|
+
self.data_handler.download_data()
|
|
732
|
+
|
|
733
|
+
def list_upload(self):
|
|
734
|
+
"""Asks the data handler to upload the unsynced changes in the queue."""
|
|
735
|
+
result = self.data_handler.process_queue()
|
|
736
|
+
#for show in result:
|
|
737
|
+
# self._emit_signal('episode_changed', show)
|
|
738
|
+
|
|
739
|
+
def get_queue(self):
|
|
740
|
+
"""Asks the data handler for the items in the current queue."""
|
|
741
|
+
return self.data_handler.queue
|
|
742
|
+
|