trackma 0.6.2__py3.4.egg → 0.7.1__py3.4.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.
Files changed (65) hide show
  1. EGG-INFO/PKG-INFO +1 -1
  2. EGG-INFO/SOURCES.txt +10 -6
  3. EGG-INFO/requires.txt +0 -2
  4. trackma/__pycache__/__init__.cpython-34.pyc +0 -0
  5. trackma/__pycache__/accounts.cpython-34.pyc +0 -0
  6. trackma/__pycache__/data.cpython-34.pyc +0 -0
  7. trackma/__pycache__/engine.cpython-34.pyc +0 -0
  8. trackma/__pycache__/messenger.cpython-34.pyc +0 -0
  9. trackma/__pycache__/utils.cpython-34.pyc +0 -0
  10. trackma/data/kitsu.png +0 -0
  11. trackma/data.py +26 -16
  12. trackma/engine.py +68 -17
  13. trackma/extras/AnimeInfoExtractor.py +1 -1
  14. trackma/extras/__pycache__/AnimeInfoExtractor.cpython-34.pyc +0 -0
  15. trackma/extras/__pycache__/__init__.cpython-34.pyc +0 -0
  16. trackma/lib/__pycache__/__init__.cpython-34.pyc +0 -0
  17. trackma/lib/__pycache__/lib.cpython-34.pyc +0 -0
  18. trackma/lib/__pycache__/libanilist.cpython-34.pyc +0 -0
  19. trackma/lib/__pycache__/libkitsu.cpython-34.pyc +0 -0
  20. trackma/lib/__pycache__/libmal.cpython-34.pyc +0 -0
  21. trackma/lib/__pycache__/libshikimori.cpython-34.pyc +0 -0
  22. trackma/lib/__pycache__/libvndb.cpython-34.pyc +0 -0
  23. trackma/lib/libanilist.py +3 -3
  24. trackma/lib/libkitsu.py +507 -0
  25. trackma/lib/libmal.py +6 -6
  26. trackma/lib/libshikimori.py +3 -3
  27. trackma/lib/libvndb.py +12 -9
  28. trackma/messenger.py +1 -1
  29. trackma/tracker/__init__.py +0 -0
  30. trackma/tracker/__pycache__/__init__.cpython-34.pyc +0 -0
  31. trackma/tracker/__pycache__/inotify.cpython-34.pyc +0 -0
  32. trackma/tracker/__pycache__/inotifyBase.cpython-34.pyc +0 -0
  33. trackma/tracker/__pycache__/plex.cpython-34.pyc +0 -0
  34. trackma/tracker/__pycache__/polling.cpython-34.pyc +0 -0
  35. trackma/tracker/__pycache__/pyinotify.cpython-34.pyc +0 -0
  36. trackma/tracker/__pycache__/tracker.cpython-34.pyc +0 -0
  37. trackma/tracker/__pycache__/win32.cpython-34.pyc +0 -0
  38. trackma/tracker/inotify.py +69 -0
  39. trackma/tracker/inotifyBase.py +110 -0
  40. trackma/tracker/plex.py +106 -0
  41. trackma/tracker/polling.py +55 -0
  42. trackma/tracker/pyinotify.py +90 -0
  43. trackma/tracker/tracker.py +221 -0
  44. trackma/tracker/win32.py +74 -0
  45. trackma/ui/__pycache__/__init__.cpython-34.pyc +0 -0
  46. trackma/ui/__pycache__/cli.cpython-34.pyc +0 -0
  47. trackma/ui/__pycache__/curses.cpython-34.pyc +0 -0
  48. trackma/ui/__pycache__/gtkui.cpython-34.pyc +0 -0
  49. trackma/ui/__pycache__/qt4ui.cpython-34.pyc +0 -0
  50. trackma/ui/__pycache__/qtui.cpython-34.pyc +0 -0
  51. trackma/ui/cli.py +110 -13
  52. trackma/ui/curses.py +88 -30
  53. trackma/ui/gtkui.py +120 -58
  54. trackma/ui/qtui.py +161 -49
  55. trackma/utils.py +16 -9
  56. trackma/__pycache__/tracker.cpython-34.pyc +0 -0
  57. trackma/data/hb.jpg +0 -0
  58. trackma/data/melative.jpg +0 -0
  59. trackma/extras/__pycache__/plex.cpython-34.pyc +0 -0
  60. trackma/extras/plex.py +0 -101
  61. trackma/lib/__pycache__/libhb.cpython-34.pyc +0 -0
  62. trackma/lib/__pycache__/libmelative.cpython-34.pyc +0 -0
  63. trackma/lib/libhb.py +0 -239
  64. trackma/lib/libmelative.py +0 -204
  65. trackma/tracker.py +0 -430
trackma/tracker.py DELETED
@@ -1,430 +0,0 @@
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 subprocess
18
- import threading
19
- import re
20
- import time
21
- import os
22
- import difflib
23
-
24
- import ctypes
25
-
26
- from trackma import messenger
27
- from trackma import utils
28
- from trackma.extras import plex
29
- from trackma.extras import AnimeInfoExtractor
30
-
31
- inotify_available = 0
32
- INOTIFY_PYINOTIFY = 1 # seb-m/pyinotify - pyinotify on PyPi
33
- INOTIFY_INOTIFY = 2 # dsoprea/PyInotify - inotify on PyPi
34
-
35
- STATE_PLAYING = 0
36
- STATE_NOVIDEO = 1
37
- STATE_UNRECOGNIZED = 2
38
- STATE_NOT_FOUND = 3
39
-
40
- try:
41
- import inotify.adapters
42
- import inotify.constants
43
- inotify_available = INOTIFY_INOTIFY
44
- except ImportError:
45
- try:
46
- import pyinotify
47
- inotify_available = INOTIFY_PYINOTIFY
48
- except:
49
- pass # If we ignore this the tracker will just use lsof
50
-
51
-
52
- class Tracker():
53
- msg = None
54
- active = True
55
- list = None
56
- last_show_tuple = None
57
- last_filename = None
58
- last_state = STATE_NOVIDEO
59
- last_time = 0
60
- last_updated = False
61
- last_close_queue = False
62
- plex_enabled = False
63
- plex_log = [None, None]
64
-
65
- name = 'Tracker'
66
-
67
- signals = {'detected': None,
68
- 'playing': None,
69
- 'removed': None,
70
- 'update': None, }
71
-
72
- def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close):
73
- self.msg = messenger
74
- self.msg.info(self.name, 'Initializing...')
75
-
76
- self.list = tracker_list
77
- self.process_name = process_name
78
- self.plex_enabled = plex.get_config()[0]
79
-
80
- tracker_args = (watch_dir, interval)
81
- self.wait_s = update_wait
82
- self.wait_close = update_close
83
- tracker_t = threading.Thread(target=self._tracker, args=tracker_args)
84
- tracker_t.daemon = True
85
- self.msg.debug(self.name, 'Enabling tracker...')
86
- tracker_t.start()
87
-
88
- def set_message_handler(self, message_handler):
89
- """Changes the message handler function on the fly."""
90
- self.msg = message_handler
91
-
92
- def disable(self):
93
- self.active = False
94
-
95
- def enable(self):
96
- self.active = True
97
-
98
- def update_list(self, tracker_list):
99
- self.list = tracker_list
100
-
101
- def connect_signal(self, signal, callback):
102
- try:
103
- self.signals[signal] = callback
104
- except KeyError:
105
- raise utils.EngineFatal("Invalid signal.")
106
-
107
- def _emit_signal(self, signal, *args):
108
- try:
109
- if self.signals[signal]:
110
- self.signals[signal](*args)
111
- except KeyError:
112
- raise Exception("Call to undefined signal.")
113
-
114
- def _get_playing_file_lsof(self, players):
115
- try:
116
- lsof = subprocess.Popen(['lsof', '+w', '-n', '-c', ''.join(['/', players, '/']), '-Fn'], stdout=subprocess.PIPE)
117
- except OSError:
118
- self.msg.warn(self.name, "Couldn't execute lsof. Disabling tracker.")
119
- self.disable()
120
- return False
121
-
122
- output = lsof.communicate()[0].decode('utf-8')
123
- fileregex = re.compile("n(.*(\.mkv|\.mp4|\.avi))")
124
-
125
- for line in output.splitlines():
126
- match = fileregex.match(line)
127
- if match is not None:
128
- return os.path.basename(match.group(1))
129
-
130
- return False
131
-
132
- def _foreach_window(self, hwnd, lParam):
133
- # Get class name and window title of the current hwnd
134
- # and add it to the list of the found windows
135
- length = ctypes.windll.user32.GetWindowTextLengthW(hwnd)
136
-
137
- buff_class = ctypes.create_unicode_buffer(32)
138
- buff_title = ctypes.create_unicode_buffer(length + 1)
139
-
140
- ctypes.windll.user32.GetClassNameW(hwnd, buff_class, 32)
141
- ctypes.windll.user32.GetWindowTextW(hwnd, buff_title, length + 1)
142
-
143
- self.win32_hwnd_list.append( (buff_class.value, buff_title.value) )
144
- return True
145
-
146
- def _get_playing_file_win32(self):
147
- # Enumerate all windows using the win32 API
148
- # This will call _foreach_window for each window handle
149
- # Then return the window title if the class name matches
150
- # Currently supporting MPC(-HC) and mpv
151
-
152
- self.win32_hwnd_list = []
153
- self.EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
154
- ctypes.windll.user32.EnumWindows(self.EnumWindowsProc(self._foreach_window), 0)
155
- winregex = re.compile("(\.mkv|\.mp4|\.avi)")
156
-
157
- for classname, title in self.win32_hwnd_list:
158
- if classname == 'MediaPlayerClassicW' and winregex.search(title) is not None:
159
- return title
160
- elif classname == 'mpv' and winregex.search(title) is not None:
161
- return title.replace('mpv - ', '')
162
-
163
- return False
164
-
165
- def _get_plex_file(self):
166
- playing_file = plex.playing_file()
167
- return playing_file
168
-
169
- def _poll_lsof(self):
170
- filename = self._get_playing_file_lsof(self.process_name)
171
- (state, show_tuple) = self._get_playing_show(filename)
172
- self.update_show_if_needed(state, show_tuple)
173
-
174
- def _observe_inotify(self, watch_dir):
175
- # Note that this lib uses bytestrings for filenames and paths.
176
- self.msg.info(self.name, 'Using inotify.')
177
- mask = (inotify.constants.IN_OPEN
178
- | inotify.constants.IN_CLOSE
179
- | inotify.constants.IN_CREATE
180
- | inotify.constants.IN_MOVE
181
- | inotify.constants.IN_DELETE)
182
- i = inotify.adapters.InotifyTree(watch_dir, mask=mask)
183
- try:
184
- for event in i.event_gen():
185
- if event is not None:
186
- # With inotifyx impl., only the event type was used,
187
- # such that it only served to poll lsof when an
188
- # open or close event was received.
189
- (header, types, path, filename) = event
190
- if 'IN_ISDIR' not in types:
191
- # If the file is gone, we remove from library
192
- if ('IN_MOVED_FROM' in types
193
- or 'IN_DELETE' in types):
194
- self._emit_signal('removed', str(path, 'utf-8'), str(filename, 'utf-8'))
195
- # Otherwise we attempt to add it to library
196
- # Would check for IN_MOVED_TO or IN_CREATE but no need
197
- else:
198
- self._emit_signal('detected', str(path, 'utf-8'), str(filename, 'utf-8'))
199
- if ('IN_OPEN' in types
200
- or 'IN_CLOSE_NOWRITE' in types
201
- or 'IN_CLOSE_WRITE' in types):
202
- self._poll_lsof()
203
- elif self.last_state != STATE_NOVIDEO:
204
- # Default blocking duration is 1 second
205
- # This will count down like inotifyx impl. did
206
- self.update_show_if_needed(self.last_state, self.last_show_tuple)
207
- finally:
208
- self.msg.info(self.name, 'Tracker has stopped.')
209
- # inotify resource is cleaned-up automatically
210
-
211
- def _observe_pyinotify(self, watch_dir):
212
- self.msg.info(self.name, 'Using pyinotify.')
213
- wm = pyinotify.WatchManager() # Watch Manager
214
- mask = (pyinotify.IN_OPEN
215
- | pyinotify.IN_CLOSE_NOWRITE
216
- | pyinotify.IN_CLOSE_WRITE
217
- | pyinotify.IN_CREATE
218
- | pyinotify.IN_MOVED_FROM
219
- | pyinotify.IN_MOVED_TO
220
- | pyinotify.IN_DELETE)
221
-
222
- class EventHandler(pyinotify.ProcessEvent):
223
- def my_init(self, parent=None):
224
- self.parent = parent
225
-
226
- def process_IN_OPEN(self, event):
227
- if not event.mask & pyinotify.IN_ISDIR:
228
- self.parent._emit_signal('detected', event.path, event.name)
229
- self.parent._poll_lsof()
230
-
231
- def process_IN_CLOSE_NOWRITE(self, event):
232
- if not event.mask & pyinotify.IN_ISDIR:
233
- self.parent._emit_signal('detected', event.path, event.name)
234
- self.parent._poll_lsof()
235
-
236
- def process_IN_CLOSE_WRITE(self, event):
237
- if not event.mask & pyinotify.IN_ISDIR:
238
- self.parent._emit_signal('detected', event.path, event.name)
239
- self.parent._poll_lsof()
240
-
241
- def process_IN_CREATE(self, event):
242
- if not event.mask & pyinotify.IN_ISDIR:
243
- self.parent._emit_signal('detected', event.path, event.name)
244
-
245
- def process_IN_MOVED_TO(self, event):
246
- if not event.mask & pyinotify.IN_ISDIR:
247
- self.parent._emit_signal('detected', event.path, event.name)
248
-
249
- def process_IN_MOVED_FROM(self, event):
250
- if not event.mask & pyinotify.IN_ISDIR:
251
- self.parent._emit_signal('removed', event.path, event.name)
252
-
253
- def process_IN_DELETE(self, event):
254
- if not event.mask & pyinotify.IN_ISDIR:
255
- self.parent._emit_signal('removed', event.path, event.name)
256
-
257
- handler = EventHandler(parent=self)
258
- notifier = pyinotify.Notifier(wm, handler)
259
- wdd = wm.add_watch(watch_dir, mask, rec=True, auto_add=True)
260
-
261
- try:
262
- #notifier.loop()
263
- timeout = None
264
- while self.active:
265
- if notifier.check_events(timeout):
266
- notifier.read_events()
267
- notifier.process_events()
268
- if self.last_state == STATE_NOVIDEO:
269
- timeout = None # Block indefinitely
270
- else:
271
- timeout = 1000 # Check each second for counting
272
- else:
273
- self.update_show_if_needed(self.last_state, self.last_show_tuple)
274
- finally:
275
- notifier.stop()
276
- self.msg.info(self.name, 'Tracker has stopped.')
277
-
278
- def _observe_polling(self, interval):
279
- self.msg.info(self.name, "pyinotify not available; using polling (slow).")
280
- while self.active:
281
- # This runs the tracker and update the playing show if necessary
282
- self._poll_lsof()
283
-
284
- # Wait for the interval before running check again
285
- time.sleep(interval)
286
-
287
- def _observe_win32(self, interval):
288
- self.msg.info(self.name, "Using Win32.")
289
-
290
- while self.active:
291
- # This runs the tracker and update the playing show if necessary
292
- filename = self._get_playing_file_win32()
293
- (state, show_tuple) = self._get_playing_show(filename)
294
- self.update_show_if_needed(state, show_tuple)
295
-
296
- # Wait for the interval before running check again
297
- time.sleep(1)
298
-
299
- def _observe_plex(self, interval):
300
- self.msg.info(self.name, "Using Plex.")
301
-
302
- while self.active:
303
- # This stores the last two states of the plex server and only
304
- # updates if it's ACTIVE.
305
- plex_status = plex.status()
306
- self.plex_log.append(plex_status)
307
-
308
- if self.plex_log[-1] == "ACTIVE" or self.plex_log[-1] == "IDLE":
309
- self.wait_s = plex.timer_from_file()
310
- filename = self._get_plex_file()
311
- (state, show_tuple) = self._get_playing_show(filename)
312
- self.update_show_if_needed(state, show_tuple)
313
- elif (self.plex_log[-2] != "NOT_RUNNING" and self.plex_log[-1] == "NOT_RUNNING"):
314
- self.msg.warn(self.name, "Plex Media Server is not running.")
315
-
316
- del self.plex_log[0]
317
- # Wait for the interval before running check again
318
- time.sleep(30)
319
-
320
- def _tracker(self, watch_dir, interval):
321
- if self.plex_enabled:
322
- self._observe_plex(interval)
323
- else:
324
- if os.name == 'nt':
325
- self._observe_win32(interval)
326
- elif inotify_available == INOTIFY_INOTIFY:
327
- self._observe_inotify(watch_dir.encode('utf-8'))
328
- elif inotify_available == INOTIFY_PYINOTIFY:
329
- self._observe_pyinotify(watch_dir)
330
- else:
331
- self._observe_polling(interval)
332
-
333
- def update_show_if_needed(self, state, show_tuple):
334
- if show_tuple:
335
- (show, episode) = show_tuple
336
-
337
- if not self.last_show_tuple or show['id'] != self.last_show_tuple[0]['id'] or episode != self.last_show_tuple[1]:
338
- # There's a new show detected, so
339
- # let's save the show information and
340
- # the time we detected it first
341
-
342
- # But if we're watching a new show, let's make sure turn off
343
- # the Playing flag on that one first
344
- if self.last_show_tuple and self.last_show_tuple[0] != show:
345
- self._emit_signal('playing', self.last_show_tuple[0]['id'], False, 0)
346
-
347
- self.last_show_tuple = (show, episode)
348
- self._emit_signal('playing', show['id'], True, episode)
349
-
350
- self.last_time = time.time()
351
- self.last_updated = False
352
-
353
- if not self.last_updated:
354
- # Check if we need to update the show yet
355
- if episode == (show['my_progress'] + 1):
356
- timedif = time.time() - self.last_time
357
-
358
- if timedif > self.wait_s:
359
- # Time has passed, let's update
360
- if self.wait_close:
361
- # Queue update for when the player closes
362
- self.msg.info(self.name, 'Waiting for the player to close.')
363
- self.last_close_queue = True
364
- self.last_updated = True
365
- else:
366
- # Update now
367
- self._emit_signal('update', show['id'], episode)
368
- self.last_updated = True
369
- else:
370
- self.msg.info(self.name, 'Will update %s %d in %d seconds' % (show['title'], episode, self.wait_s-timedif+1))
371
- else:
372
- # We shouldn't update to this episode!
373
- self.msg.warn(self.name, 'Player is not playing the next episode of %s. Ignoring.' % show['title'])
374
- self.last_updated = True
375
- else:
376
- # The episode was updated already. do nothing
377
- pass
378
- elif self.last_state != state:
379
- # React depending on state
380
- # STATE_NOVIDEO : No video is playing anymroe
381
- # STATE_UNRECOGNIZED : There's a new video playing but the regex didn't recognize the format
382
- # STATE_NOT_FOUND : There's a new video playing but an associated show wasn't found
383
- if state == STATE_NOVIDEO and self.last_show_tuple:
384
- # Update now if there's an update queued
385
- if self.last_close_queue:
386
- self._emit_signal('update', self.last_show_tuple[0]['id'], self.last_show_tuple[1])
387
- elif not self.last_updated:
388
- self.msg.info(self.name, 'Player was closed before update.')
389
- elif state == STATE_UNRECOGNIZED:
390
- self.msg.warn(self.name, 'Found video but the file name format couldn\'t be recognized.')
391
- elif state == STATE_NOT_FOUND:
392
- self.msg.warn(self.name, 'Found player but show not in list.')
393
-
394
- # Clear any show previously playing
395
- if self.last_show_tuple:
396
- self._emit_signal('playing', self.last_show_tuple[0]['id'], False, self.last_show_tuple[1])
397
- self.last_updated = False
398
- self.last_close_queue = False
399
- self.last_time = 0
400
- self.last_show_tuple = None
401
-
402
- self.last_state = state
403
-
404
- def _get_playing_show(self, filename):
405
- if not self.active:
406
- # Don't do anything if the Tracker is disabled
407
- return (STATE_NOVIDEO, None)
408
-
409
- if filename:
410
- if filename == self.last_filename:
411
- # It's the exact same filename, there's no need to do the processing again
412
- return (4, self.last_show_tuple)
413
-
414
- self.last_filename = filename
415
-
416
- # Do a regex to the filename to get
417
- # the show title and episode number
418
- aie = AnimeInfoExtractor(filename)
419
- (show_title, show_ep) = (aie.getName(), aie.getEpisode())
420
- if not show_title:
421
- return (STATE_UNRECOGNIZED, None) # Format not recognized
422
-
423
- playing_show = utils.guess_show(show_title, self.list)
424
- if playing_show:
425
- return (STATE_PLAYING, (playing_show, show_ep))
426
- else:
427
- return (STATE_NOT_FOUND, None) # Show not in list
428
- else:
429
- self.last_filename = None
430
- return (STATE_NOVIDEO, None) # Not playing