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.
Files changed (57) hide show
  1. EGG-INFO/PKG-INFO +19 -0
  2. EGG-INFO/SOURCES.txt +35 -0
  3. EGG-INFO/dependency_links.txt +1 -0
  4. EGG-INFO/entry_points.txt +8 -0
  5. EGG-INFO/not-zip-safe +1 -0
  6. EGG-INFO/requires.txt +10 -0
  7. EGG-INFO/top_level.txt +1 -0
  8. trackma/__init__.py +0 -0
  9. trackma/__init__.pyc +0 -0
  10. trackma/accounts.py +110 -0
  11. trackma/accounts.pyc +0 -0
  12. trackma/data/anilist.jpg +0 -0
  13. trackma/data/hb.jpg +0 -0
  14. trackma/data/icon.png +0 -0
  15. trackma/data/mal.jpg +0 -0
  16. trackma/data/melative.jpg +0 -0
  17. trackma/data/vndb.jpg +0 -0
  18. trackma/data.py +585 -0
  19. trackma/data.pyc +0 -0
  20. trackma/engine.py +742 -0
  21. trackma/engine.pyc +0 -0
  22. trackma/extras/AnimeInfoExtractor.py +308 -0
  23. trackma/extras/AnimeInfoExtractor.pyc +0 -0
  24. trackma/extras/__init__.py +0 -0
  25. trackma/extras/__init__.pyc +0 -0
  26. trackma/extras/plex.py +100 -0
  27. trackma/extras/plex.pyc +0 -0
  28. trackma/lib/__init__.py +0 -0
  29. trackma/lib/__init__.pyc +0 -0
  30. trackma/lib/lib.py +162 -0
  31. trackma/lib/lib.pyc +0 -0
  32. trackma/lib/libanilist.py +355 -0
  33. trackma/lib/libanilist.pyc +0 -0
  34. trackma/lib/libhb.py +248 -0
  35. trackma/lib/libhb.pyc +0 -0
  36. trackma/lib/libmal.py +705 -0
  37. trackma/lib/libmal.pyc +0 -0
  38. trackma/lib/libmelative.py +213 -0
  39. trackma/lib/libmelative.pyc +0 -0
  40. trackma/lib/libvndb.py +373 -0
  41. trackma/lib/libvndb.pyc +0 -0
  42. trackma/messenger.py +42 -0
  43. trackma/messenger.pyc +0 -0
  44. trackma/tracker.py +301 -0
  45. trackma/tracker.pyc +0 -0
  46. trackma/ui/__init__.py +0 -0
  47. trackma/ui/__init__.pyc +0 -0
  48. trackma/ui/cli.py +712 -0
  49. trackma/ui/cli.pyc +0 -0
  50. trackma/ui/curses.py +1049 -0
  51. trackma/ui/curses.pyc +0 -0
  52. trackma/ui/gtkui.py +2360 -0
  53. trackma/ui/gtkui.pyc +0 -0
  54. trackma/ui/qtui.py +2120 -0
  55. trackma/ui/qtui.pyc +0 -0
  56. trackma/utils.py +299 -0
  57. trackma/utils.pyc +0 -0
trackma/tracker.py ADDED
@@ -0,0 +1,301 @@
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 messenger
25
+ import utils
26
+ import extras.plex as plex
27
+ import extras.AnimeInfoExtractor
28
+
29
+ inotify_available = False
30
+
31
+ STATE_PLAYING = 0
32
+ STATE_NOVIDEO = 1
33
+ STATE_UNRECOGNIZED = 2
34
+ STATE_NOT_FOUND = 3
35
+
36
+ try:
37
+ import inotifyx
38
+ inotify_available = True
39
+ except ImportError:
40
+ pass # If we ignore this the tracker will just use lsof
41
+
42
+
43
+ class Tracker(object):
44
+ msg = None
45
+ active = True
46
+ list = None
47
+ last_show_tuple = None
48
+ last_filename = None
49
+ last_state = STATE_NOVIDEO
50
+ last_time = 0
51
+ last_updated = False
52
+ last_close_queue = False
53
+ plex_enabled = False
54
+ plex_log = [None, None]
55
+
56
+ name = 'Tracker'
57
+
58
+ signals = { 'playing' : None,
59
+ 'update': None, }
60
+
61
+ def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close):
62
+ self.msg = messenger
63
+ self.msg.info(self.name, 'Initializing...')
64
+
65
+ self.list = tracker_list
66
+ self.process_name = process_name
67
+ self.plex_enabled = plex.get_config()[0]
68
+
69
+ tracker_args = (watch_dir, interval)
70
+ self.wait_s = update_wait
71
+ self.wait_close = update_close
72
+ tracker_t = threading.Thread(target=self._tracker, args=tracker_args)
73
+ tracker_t.daemon = True
74
+ self.msg.debug(self.name, 'Enabling tracker...')
75
+ tracker_t.start()
76
+
77
+ def set_message_handler(self, message_handler):
78
+ """Changes the message handler function on the fly."""
79
+ self.msg = message_handler
80
+
81
+ def disable(self):
82
+ self.active = False
83
+
84
+ def enable(self):
85
+ self.active = True
86
+
87
+ def update_list(self, tracker_list):
88
+ self.list = tracker_list
89
+
90
+ def connect_signal(self, signal, callback):
91
+ try:
92
+ self.signals[signal] = callback
93
+ except KeyError:
94
+ raise utils.EngineFatal("Invalid signal.")
95
+
96
+ def _emit_signal(self, signal, *args):
97
+ try:
98
+ if self.signals[signal]:
99
+ self.signals[signal](*args)
100
+ except KeyError:
101
+ raise Exception("Call to undefined signal.")
102
+
103
+ def _get_playing_file(self, players):
104
+ try:
105
+ lsof = subprocess.Popen(['lsof', '-n', '-c', ''.join(['/', players, '/']), '-Fn'], stdout=subprocess.PIPE)
106
+ except OSError:
107
+ self.msg.warn(self.name, "Couldn't execute lsof. Disabling tracker.")
108
+ self.disable()
109
+ return False
110
+
111
+ output = lsof.communicate()[0].decode('utf-8')
112
+ fileregex = re.compile("n(.*(\.mkv|\.mp4|\.avi))")
113
+
114
+ for line in output.splitlines():
115
+ match = fileregex.match(line)
116
+ if match is not None:
117
+ return os.path.basename(match.group(1))
118
+
119
+ return False
120
+
121
+ def _get_plex_file(self):
122
+ playing_file = plex.playing_file()
123
+ return playing_file
124
+
125
+ def _inotify_watch_recursive(self, fd, watch_dir):
126
+ self.msg.debug(self.name, 'inotify: Watching %s' % watch_dir)
127
+ inotifyx.add_watch(fd, watch_dir.encode('utf-8'), inotifyx.IN_OPEN | inotifyx.IN_CLOSE)
128
+
129
+ for root, dirs, files in os.walk(watch_dir):
130
+ for dir_ in dirs:
131
+ self._inotify_watch_recursive(fd, os.path.join(root, dir_))
132
+
133
+ def _observe_inotify(self, watch_dir):
134
+ self.msg.info(self.name, 'Using inotify.')
135
+
136
+ timeout = -1
137
+ fd = inotifyx.init()
138
+ try:
139
+ self._inotify_watch_recursive(fd, watch_dir)
140
+ while True:
141
+ events = inotifyx.get_events(fd, timeout)
142
+ if events:
143
+ for event in events:
144
+ if not event.mask & inotifyx.IN_ISDIR:
145
+ (state, show_tuple) = self._get_playing_show()
146
+ self.update_show_if_needed(state, show_tuple)
147
+
148
+ if self.last_state == STATE_NOVIDEO:
149
+ # Make get_events block indifinitely
150
+ timeout = -1
151
+ else:
152
+ timeout = 1
153
+ else:
154
+ self.update_show_if_needed(self.last_state, self.last_show_tuple)
155
+ except IOError:
156
+ self.msg.warn(self.name, 'Watch directory not found! Tracker will stop.')
157
+ finally:
158
+ os.close(fd)
159
+
160
+ def _observe_polling(self, interval):
161
+ self.msg.warn(self.name, "inotifyx not available; using polling (slow).")
162
+ while True:
163
+ # This runs the tracker and update the playing show if necessary
164
+ (state, show_tuple) = self._get_playing_show()
165
+ self.update_show_if_needed(state, show_tuple)
166
+
167
+ # Wait for the interval before running check again
168
+ time.sleep(interval)
169
+
170
+ def _observe_plex(self, interval):
171
+ self.msg.info(self.name, "Tracking Plex.")
172
+
173
+ while True:
174
+ # This stores the last two states of the plex server and only
175
+ # updates if it's ACTIVE.
176
+ plex_status = plex.status()
177
+ self.plex_log.append(plex_status)
178
+
179
+ if self.plex_log[-1] == "ACTIVE" or self.plex_log[-1] == "IDLE":
180
+ self.wait_s = plex.timer_from_file()
181
+ (state, show_tuple) = self._get_playing_show()
182
+ self.update_show_if_needed(state, show_tuple)
183
+ elif (self.plex_log[-2] != "NOT_RUNNING" and self.plex_log[-1] == "NOT_RUNNING"):
184
+ self.msg.warn(self.name, "Plex Media Server is not running.")
185
+
186
+ del self.plex_log[0]
187
+ # Wait for the interval before running check again
188
+ time.sleep(30)
189
+
190
+ def _tracker(self, watch_dir, interval):
191
+ if self.plex_enabled:
192
+ self._observe_plex(interval)
193
+ else:
194
+ if inotify_available:
195
+ self._observe_inotify(watch_dir)
196
+ else:
197
+ self._observe_polling(interval)
198
+
199
+ def update_show_if_needed(self, state, show_tuple):
200
+ if show_tuple:
201
+ (show, episode) = show_tuple
202
+
203
+ if not self.last_show_tuple or show['id'] != self.last_show_tuple[0]['id'] or episode != self.last_show_tuple[1]:
204
+ # There's a new show detected, so
205
+ # let's save the show information and
206
+ # the time we detected it first
207
+
208
+ # But if we're watching a new show, let's make sure turn off
209
+ # the Playing flag on that one first
210
+ if self.last_show_tuple and self.last_show_tuple[0] != show:
211
+ self._emit_signal('playing', self.last_show_tuple[0]['id'], False, 0)
212
+
213
+ self.last_show_tuple = (show, episode)
214
+ self._emit_signal('playing', show['id'], True, episode)
215
+
216
+ self.last_time = time.time()
217
+ self.last_updated = False
218
+
219
+ if not self.last_updated:
220
+ # Check if we need to update the show yet
221
+ if episode == (show['my_progress'] + 1):
222
+ timedif = time.time() - self.last_time
223
+
224
+ if timedif > self.wait_s:
225
+ # Time has passed, let's update
226
+ if self.wait_close:
227
+ # Queue update for when the player closes
228
+ self.msg.info(self.name, 'Waiting for the player to close.')
229
+ self.last_close_queue = True
230
+ self.last_updated = True
231
+ else:
232
+ # Update now
233
+ self._emit_signal('update', show['id'], episode)
234
+ self.last_updated = True
235
+ else:
236
+ self.msg.info(self.name, 'Will update %s %d in %d seconds' % (show['title'], episode, self.wait_s-timedif+1))
237
+ else:
238
+ # We shouldn't update to this episode!
239
+ self.msg.warn(self.name, 'Player is not playing the next episode of %s. Ignoring.' % show['title'])
240
+ self.last_updated = True
241
+ else:
242
+ # The episode was updated already. do nothing
243
+ pass
244
+ elif self.last_state != state:
245
+ # React depending on state
246
+ # STATE_NOVIDEO : No video is playing anymroe
247
+ # STATE_UNRECOGNIZED : There's a new video playing but the regex didn't recognize the format
248
+ # STATE_NOT_FOUND : There's a new video playing but an associated show wasn't found
249
+ if state == STATE_NOVIDEO and self.last_show_tuple:
250
+ # Update now if there's an update queued
251
+ if self.last_close_queue:
252
+ self._emit_signal('update', self.last_show_tuple[0]['id'], self.last_show_tuple[1])
253
+ elif not self.last_updated:
254
+ self.msg.info(self.name, 'Player was closed before update.')
255
+ elif state == STATE_UNRECOGNIZED:
256
+ self.msg.warn(self.name, 'Found video but the file name format couldn\'t be recognized.')
257
+ elif state == STATE_NOT_FOUND:
258
+ self.msg.warn(self.name, 'Found player but show not in list.')
259
+
260
+ # Clear any show previously playing
261
+ if self.last_show_tuple:
262
+ self._emit_signal('playing', self.last_show_tuple[0]['id'], False, self.last_show_tuple[1])
263
+ self.last_updated = False
264
+ self.last_close_queue = False
265
+ self.last_time = 0
266
+ self.last_show_tuple = None
267
+
268
+ self.last_state = state
269
+
270
+ def _get_playing_show(self):
271
+ if not self.active:
272
+ # Don't do anything if the Tracker is disabled
273
+ return (STATE_NOVIDEO, None)
274
+
275
+ if self.plex_enabled:
276
+ filename = self._get_plex_file()
277
+ else:
278
+ filename = self._get_playing_file(self.process_name)
279
+
280
+ if filename:
281
+ if filename == self.last_filename:
282
+ # It's the exact same filename, there's no need to do the processing again
283
+ return (4, self.last_show_tuple)
284
+
285
+ self.last_filename = filename
286
+
287
+ # Do a regex to the filename to get
288
+ # the show title and episode number
289
+ aie = extras.AnimeInfoExtractor.AnimeInfoExtractor(filename)
290
+ (show_title, show_ep) = (aie.getName(), aie.getEpisode())
291
+ if not show_title:
292
+ return (STATE_UNRECOGNIZED, None) # Format not recognized
293
+
294
+ playing_show = utils.guess_show(show_title, self.list)
295
+ if playing_show:
296
+ return (STATE_PLAYING, (playing_show, show_ep))
297
+ else:
298
+ return (STATE_NOT_FOUND, None) # Show not in list
299
+ else:
300
+ self.last_filename = None
301
+ return (STATE_NOVIDEO, None) # Not playing
trackma/tracker.pyc ADDED
Binary file
trackma/ui/__init__.py ADDED
File without changes
Binary file