trackma 0.6.2__py3.4.egg → 0.7__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 (62) hide show
  1. EGG-INFO/PKG-INFO +1 -1
  2. EGG-INFO/SOURCES.txt +9 -6
  3. trackma/__pycache__/__init__.cpython-34.pyc +0 -0
  4. trackma/__pycache__/accounts.cpython-34.pyc +0 -0
  5. trackma/__pycache__/data.cpython-34.pyc +0 -0
  6. trackma/__pycache__/engine.cpython-34.pyc +0 -0
  7. trackma/__pycache__/messenger.cpython-34.pyc +0 -0
  8. trackma/__pycache__/utils.cpython-34.pyc +0 -0
  9. trackma/data/kitsu.png +0 -0
  10. trackma/data.py +26 -16
  11. trackma/engine.py +60 -16
  12. trackma/extras/AnimeInfoExtractor.py +1 -1
  13. trackma/extras/__pycache__/AnimeInfoExtractor.cpython-34.pyc +0 -0
  14. trackma/extras/__pycache__/__init__.cpython-34.pyc +0 -0
  15. trackma/lib/__pycache__/__init__.cpython-34.pyc +0 -0
  16. trackma/lib/__pycache__/lib.cpython-34.pyc +0 -0
  17. trackma/lib/__pycache__/libanilist.cpython-34.pyc +0 -0
  18. trackma/lib/__pycache__/libkitsu.cpython-34.pyc +0 -0
  19. trackma/lib/__pycache__/libmal.cpython-34.pyc +0 -0
  20. trackma/lib/__pycache__/libshikimori.cpython-34.pyc +0 -0
  21. trackma/lib/__pycache__/libvndb.cpython-34.pyc +0 -0
  22. trackma/lib/libanilist.py +3 -3
  23. trackma/lib/libkitsu.py +478 -0
  24. trackma/lib/libmal.py +6 -6
  25. trackma/lib/libshikimori.py +3 -3
  26. trackma/lib/libvndb.py +12 -9
  27. trackma/messenger.py +1 -1
  28. trackma/tracker/__init__.py +0 -0
  29. trackma/tracker/__pycache__/__init__.cpython-34.pyc +0 -0
  30. trackma/tracker/__pycache__/inotify.cpython-34.pyc +0 -0
  31. trackma/tracker/__pycache__/plex.cpython-34.pyc +0 -0
  32. trackma/tracker/__pycache__/polling.cpython-34.pyc +0 -0
  33. trackma/tracker/__pycache__/pyinotify.cpython-34.pyc +0 -0
  34. trackma/tracker/__pycache__/tracker.cpython-34.pyc +0 -0
  35. trackma/tracker/__pycache__/win32.cpython-34.pyc +0 -0
  36. trackma/tracker/inotify.py +123 -0
  37. trackma/tracker/plex.py +99 -0
  38. trackma/tracker/polling.py +55 -0
  39. trackma/tracker/pyinotify.py +143 -0
  40. trackma/tracker/tracker.py +213 -0
  41. trackma/tracker/win32.py +74 -0
  42. trackma/ui/__pycache__/__init__.cpython-34.pyc +0 -0
  43. trackma/ui/__pycache__/cli.cpython-34.pyc +0 -0
  44. trackma/ui/__pycache__/curses.cpython-34.pyc +0 -0
  45. trackma/ui/__pycache__/gtkui.cpython-34.pyc +0 -0
  46. trackma/ui/__pycache__/qt4ui.cpython-34.pyc +0 -0
  47. trackma/ui/__pycache__/qtui.cpython-34.pyc +0 -0
  48. trackma/ui/cli.py +109 -12
  49. trackma/ui/curses.py +87 -29
  50. trackma/ui/gtkui.py +81 -45
  51. trackma/ui/qtui.py +95 -34
  52. trackma/utils.py +15 -9
  53. trackma/__pycache__/tracker.cpython-34.pyc +0 -0
  54. trackma/data/hb.jpg +0 -0
  55. trackma/data/melative.jpg +0 -0
  56. trackma/extras/__pycache__/plex.cpython-34.pyc +0 -0
  57. trackma/extras/plex.py +0 -101
  58. trackma/lib/__pycache__/libhb.cpython-34.pyc +0 -0
  59. trackma/lib/__pycache__/libmelative.cpython-34.pyc +0 -0
  60. trackma/lib/libhb.py +0 -239
  61. trackma/lib/libmelative.py +0 -204
  62. trackma/tracker.py +0 -430
@@ -0,0 +1,123 @@
1
+ # This file is part of Trackma.
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 os
18
+ import re
19
+
20
+ import inotify.adapters
21
+ import inotify.constants
22
+
23
+ from trackma.tracker import tracker
24
+ from trackma import utils
25
+
26
+ class inotifyTracker(tracker.TrackerBase):
27
+ name = 'Tracker (inotify)'
28
+ open_pathname = None
29
+
30
+ def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt):
31
+ super().__init__(messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt)
32
+
33
+ self.re_players = re.compile(self.process_name.encode('utf-8'))
34
+
35
+ def _is_being_played(self, filename):
36
+ """
37
+ This function makes sure that the filename is being played
38
+ by the player specified in players.
39
+
40
+ It uses procfs so if we're using inotify that means we're using Linux
41
+ thus we should be safe.
42
+ """
43
+
44
+ for p in os.listdir("/proc/"):
45
+ if not p.isdigit(): continue
46
+ d = "/proc/%s/fd/" % p
47
+ try:
48
+ for fd in os.listdir(d):
49
+ f = os.readlink(d+fd)
50
+ if f == filename:
51
+ # Get process name
52
+ with open('/proc/%s/cmdline' % p, 'rb') as f:
53
+ pname = f.read()
54
+
55
+ # Check if it's our process
56
+ if self.re_players.match(pname):
57
+ return True
58
+ except OSError:
59
+ pass
60
+
61
+ return False
62
+
63
+ def _proc_open(self, path, filename):
64
+ pathname = "/".join([path, filename])
65
+
66
+ if self._is_being_played(pathname):
67
+ self.open_pathname = pathname
68
+
69
+ (state, show_tuple) = self._get_playing_show(filename)
70
+ self.update_show_if_needed(state, show_tuple)
71
+
72
+ def _proc_close(self, path, filename):
73
+ pathname = "/".join([path, filename])
74
+
75
+ if pathname == self.open_pathname:
76
+ self.open_pathname = None
77
+
78
+ (state, show_tuple) = self._get_playing_show(None)
79
+ self.update_show_if_needed(state, show_tuple)
80
+
81
+ def observe(self, watch_dir, interval):
82
+ # Note that this lib uses bytestrings for filenames and paths.
83
+ self.msg.info(self.name, 'Using inotify.')
84
+
85
+ watch_dir = watch_dir.encode('utf-8')
86
+ mask = (inotify.constants.IN_OPEN
87
+ | inotify.constants.IN_CLOSE
88
+ | inotify.constants.IN_CREATE
89
+ | inotify.constants.IN_MOVE
90
+ | inotify.constants.IN_DELETE)
91
+
92
+ i = inotify.adapters.InotifyTree(watch_dir, mask=mask)
93
+
94
+ try:
95
+ for event in i.event_gen():
96
+ if event is not None:
97
+ # With inotifyx impl., only the event type was used,
98
+ # such that it only served to poll lsof when an
99
+ # open or close event was received.
100
+ (header, types, path, filename) = event
101
+ if 'IN_ISDIR' not in types:
102
+ # If the file is gone, we remove from library
103
+ if ('IN_MOVED_FROM' in types
104
+ or 'IN_DELETE' in types):
105
+ self._emit_signal('removed', str(path, 'utf-8'), str(filename, 'utf-8'))
106
+ # Otherwise we attempt to add it to library
107
+ # Would check for IN_MOVED_TO or IN_CREATE but no need
108
+ else:
109
+ self._emit_signal('detected', str(path, 'utf-8'), str(filename, 'utf-8'))
110
+
111
+ if 'IN_OPEN' in types:
112
+ self._proc_open(str(path, 'utf-8'), str(filename, 'utf-8'))
113
+ elif ('IN_CLOSE_NOWRITE' in types
114
+ or 'IN_CLOSE_WRITE' in types):
115
+ self._proc_close(str(path, 'utf-8'), str(filename, 'utf-8'))
116
+ elif self.last_state != utils.TRACKER_NOVIDEO and not self.last_updated:
117
+ # Default blocking duration is 1 second
118
+ # This will count down like inotifyx impl. did
119
+ self.update_show_if_needed(self.last_state, self.last_show_tuple)
120
+ finally:
121
+ self.msg.info(self.name, 'Tracker has stopped.')
122
+ # inotify resource is cleaned-up automatically
123
+
@@ -0,0 +1,99 @@
1
+ # This file is part of Trackma.
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 ntpath
18
+ import time
19
+ import urllib.parse
20
+ import urllib.request
21
+ import xml.dom.minidom as xdmd
22
+
23
+ import trackma.utils as utils
24
+ from trackma.tracker import tracker
25
+
26
+ NOT_RUNNING = 0
27
+ ACTIVE = 1
28
+ IDLE = 2
29
+
30
+ class PlexTracker(tracker.TrackerBase):
31
+ name = 'Tracker (Plex)'
32
+
33
+ def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt):
34
+ self.config = utils.parse_config(utils.get_root_filename('config.json'), utils.config_defaults)
35
+ super().__init__(messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt)
36
+
37
+ def get_plex_status(self):
38
+ # returns the plex status of the first active session
39
+ try:
40
+ active = int(self._get_xml_info("MediaContainer", "size"))
41
+
42
+ if active:
43
+ return ACTIVE
44
+ else:
45
+ return IDLE
46
+ except urllib.request.URLError:
47
+ return NOT_RUNNING
48
+
49
+ def playing_file(self):
50
+ # returns the filename of the currently playing file
51
+ if self.get_plex_status() == IDLE:
52
+ return None
53
+
54
+ attr = self._get_xml_info("Part", "file")
55
+ name = urllib.parse.unquote(ntpath.basename(attr))
56
+
57
+ return name
58
+
59
+ def timer_from_file(self):
60
+ # returns 80% of video duration for the update timer,
61
+ # roughly the time of the video minus the OP and ED
62
+ if self.get_plex_status() == IDLE:
63
+ return None
64
+
65
+ duration = int(self._get_xml_info("Video", "duration"))
66
+
67
+ return round((duration*0.80)/60000)*60
68
+
69
+ def observe(self, watch_dir, interval):
70
+ self.msg.info(self.name, "Using Plex.")
71
+
72
+ while self.active:
73
+ status = self.get_plex_status()
74
+
75
+ if status == ACTIVE or status == IDLE:
76
+ if status == IDLE:
77
+ self.msg.info(self.name, "Using Plex.")
78
+
79
+ self.wait_s = self.timer_from_file()
80
+ filename = self.playing_file()
81
+ (state, show_tuple) = self._get_playing_show(filename)
82
+ self.update_show_if_needed(state, show_tuple)
83
+
84
+ elif status == NOT_RUNNING:
85
+ self.msg.warn(self.name, "Plex Media Server is not running.")
86
+
87
+ # Wait for the interval before running check again
88
+ time.sleep(interval)
89
+
90
+ def _get_xml_info(self, tag, attr):
91
+ # Get the required info from the /status/sessions url
92
+ host_port = self.config['plex_host']+":"+self.config['plex_port']
93
+
94
+ session_url = "http://"+host_port+"/status/sessions"
95
+ sdoc = xdmd.parse(urllib.request.urlopen(session_url))
96
+
97
+ res = sdoc.getElementsByTagName(tag)[0].getAttribute(attr)
98
+
99
+ return res
@@ -0,0 +1,55 @@
1
+ # This file is part of Trackma.
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 time
18
+ import re
19
+ import os
20
+ import subprocess
21
+
22
+ from trackma.tracker import tracker
23
+
24
+ class PollingTracker(tracker.TrackerBase):
25
+ name = 'Tracker (polling)'
26
+
27
+ def get_playing_file(self, watch_dir, players):
28
+ try:
29
+ lsof = subprocess.Popen(['lsof', '+w', '-n', '-c', ''.join(['/', players, '/']), '-Fn', watch_dir], stdout=subprocess.PIPE)
30
+ except OSError:
31
+ self.msg.warn(self.name, "Couldn't execute lsof. Disabling tracker.")
32
+ self.disable()
33
+ return None
34
+
35
+ output = lsof.communicate()[0].decode('utf-8')
36
+ fileregex = re.compile("n(.*(\.mkv|\.mp4|\.avi))")
37
+
38
+ for line in output.splitlines():
39
+ match = fileregex.match(line)
40
+ if match is not None:
41
+ return os.path.basename(match.group(1))
42
+
43
+ return None
44
+
45
+ def observe(self, watch_dir, interval):
46
+ self.msg.info(self.name, "pyinotify not available; using polling (slow).")
47
+ while self.active:
48
+ # This runs the tracker and update the playing show if necessary
49
+ filename = self.get_playing_file(watch_dir, self.process_name)
50
+ (state, show_tuple) = self._get_playing_show(filename)
51
+ self.update_show_if_needed(state, show_tuple)
52
+
53
+ # Wait for the interval before running check again
54
+ time.sleep(interval)
55
+
@@ -0,0 +1,143 @@
1
+ # This file is part of Trackma.
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 pyinotify
18
+
19
+ import os
20
+ import re
21
+
22
+ from trackma.tracker import tracker
23
+ from trackma import utils
24
+
25
+ class pyinotifyTracker(tracker.TrackerBase):
26
+ name = 'Tracker (pyinotify)'
27
+
28
+ open_pathname = None
29
+
30
+ def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt):
31
+ super().__init__(messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt)
32
+
33
+ self.re_players = re.compile(self.process_name.encode('utf-8'))
34
+
35
+ def _is_being_played(self, filename):
36
+ """
37
+ This function makes sure that the filename is being played
38
+ by the player specified in players.
39
+
40
+ It uses procfs so if we're using inotify that means we're using Linux
41
+ thus we should be safe.
42
+ """
43
+
44
+ for p in os.listdir("/proc/"):
45
+ if not p.isdigit(): continue
46
+ d = "/proc/%s/fd/" % p
47
+ try:
48
+ for fd in os.listdir(d):
49
+ f = os.readlink(d+fd)
50
+ if f == filename:
51
+ # Get process name
52
+ with open('/proc/%s/cmdline' % p, 'rb') as f:
53
+ pname = f.read()
54
+
55
+ # Check if it's our process
56
+ if self.re_players.match(pname):
57
+ return True
58
+ except OSError:
59
+ pass
60
+
61
+ return False
62
+
63
+ def _proc_open(self, pathname, filename):
64
+ if self._is_being_played(pathname):
65
+ self.open_pathname = pathname
66
+
67
+ (state, show_tuple) = self._get_playing_show(filename)
68
+ self.update_show_if_needed(state, show_tuple)
69
+
70
+ def _proc_close(self, pathname):
71
+ if pathname == self.open_pathname:
72
+ self.open_pathname = None
73
+
74
+ (state, show_tuple) = self._get_playing_show(None)
75
+ self.update_show_if_needed(state, show_tuple)
76
+
77
+ def observe(self, watch_dir, interval):
78
+ self.msg.info(self.name, 'Using pyinotify.')
79
+ wm = pyinotify.WatchManager() # Watch Manager
80
+ mask = (pyinotify.IN_OPEN
81
+ | pyinotify.IN_CLOSE_NOWRITE
82
+ | pyinotify.IN_CLOSE_WRITE
83
+ | pyinotify.IN_CREATE
84
+ | pyinotify.IN_MOVED_FROM
85
+ | pyinotify.IN_MOVED_TO
86
+ | pyinotify.IN_DELETE)
87
+
88
+ class EventHandler(pyinotify.ProcessEvent):
89
+ def my_init(self, parent=None):
90
+ self.parent = parent
91
+
92
+ def process_IN_OPEN(self, event):
93
+ if not event.mask & pyinotify.IN_ISDIR:
94
+ self.parent._emit_signal('detected', event.path, event.name)
95
+ self.parent._proc_open(event.pathname, event.name)
96
+
97
+ def process_IN_CLOSE_NOWRITE(self, event):
98
+ if not event.mask & pyinotify.IN_ISDIR:
99
+ self.parent._emit_signal('detected', event.path, event.name)
100
+ self.parent._proc_close(event.pathname)
101
+
102
+ def process_IN_CLOSE_WRITE(self, event):
103
+ if not event.mask & pyinotify.IN_ISDIR:
104
+ self.parent._emit_signal('detected', event.path, event.name)
105
+ self.parent._proc_close(event.pathname)
106
+
107
+ def process_IN_CREATE(self, event):
108
+ if not event.mask & pyinotify.IN_ISDIR:
109
+ self.parent._emit_signal('detected', event.path, event.name)
110
+
111
+ def process_IN_MOVED_TO(self, event):
112
+ if not event.mask & pyinotify.IN_ISDIR:
113
+ self.parent._emit_signal('detected', event.path, event.name)
114
+
115
+ def process_IN_MOVED_FROM(self, event):
116
+ if not event.mask & pyinotify.IN_ISDIR:
117
+ self.parent._emit_signal('removed', event.path, event.name)
118
+
119
+ def process_IN_DELETE(self, event):
120
+ if not event.mask & pyinotify.IN_ISDIR:
121
+ self.parent._emit_signal('removed', event.path, event.name)
122
+
123
+ handler = EventHandler(parent=self)
124
+ notifier = pyinotify.Notifier(wm, handler)
125
+ wdd = wm.add_watch(watch_dir, mask, rec=True, auto_add=True)
126
+
127
+ try:
128
+ #notifier.loop()
129
+ timeout = None
130
+ while self.active:
131
+ if notifier.check_events(timeout):
132
+ notifier.read_events()
133
+ notifier.process_events()
134
+ if self.last_state == utils.TRACKER_NOVIDEO or self.last_updated:
135
+ timeout = None # Block indefinitely
136
+ else:
137
+ timeout = 1000 # Check each second for counting
138
+ else:
139
+ self.update_show_if_needed(self.last_state, self.last_show_tuple)
140
+ finally:
141
+ notifier.stop()
142
+ self.msg.info(self.name, 'Tracker has stopped.')
143
+
@@ -0,0 +1,213 @@
1
+ # This file is part of Trackma.
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 threading
18
+ import re
19
+ import time
20
+ import os
21
+
22
+ import ctypes
23
+
24
+ from trackma import messenger
25
+ from trackma import utils
26
+ from trackma.extras import AnimeInfoExtractor
27
+
28
+ class TrackerBase(object):
29
+ msg = None
30
+ active = True
31
+ list = None
32
+ last_show_tuple = None
33
+ last_filename = None
34
+ last_state = utils.TRACKER_NOVIDEO
35
+ last_time = 0
36
+ last_updated = False
37
+ last_close_queue = None
38
+ timer = None
39
+
40
+ name = 'Tracker'
41
+
42
+ signals = {
43
+ 'state': None,
44
+ 'detected': None,
45
+ 'playing': None,
46
+ 'removed': None,
47
+ 'update': None,
48
+ 'unrecognised': None,
49
+ }
50
+
51
+ def __init__(self, messenger, tracker_list, process_name, watch_dir, interval, update_wait, update_close, not_found_prompt):
52
+ self.msg = messenger
53
+ self.msg.info(self.name, 'Initializing...')
54
+
55
+ self.list = tracker_list
56
+ self.process_name = process_name
57
+
58
+ tracker_args = (watch_dir, interval)
59
+ self.wait_s = update_wait
60
+ self.wait_close = update_close
61
+ self.not_found_prompt = not_found_prompt
62
+ tracker_t = threading.Thread(target=self.observe, args=tracker_args)
63
+ tracker_t.daemon = True
64
+ self.msg.debug(self.name, 'Enabling tracker...')
65
+ tracker_t.start()
66
+
67
+ def set_message_handler(self, message_handler):
68
+ """Changes the message handler function on the fly."""
69
+ self.msg = message_handler
70
+
71
+ def disable(self):
72
+ self.active = False
73
+
74
+ def enable(self):
75
+ self.active = True
76
+
77
+ def update_list(self, tracker_list):
78
+ self.list = tracker_list
79
+
80
+ def connect_signal(self, signal, callback):
81
+ try:
82
+ self.signals[signal] = callback
83
+ except KeyError:
84
+ raise utils.EngineFatal("Invalid signal.")
85
+
86
+ def observe(self, watch_dir, interval):
87
+ raise NotImplementedError
88
+
89
+ def get_status(self):
90
+ return {
91
+ 'state': self.last_state,
92
+ 'timer': self.timer,
93
+ 'show': self.last_show_tuple,
94
+ 'filename': self.last_filename,
95
+ }
96
+
97
+ def _emit_signal(self, signal, *args):
98
+ try:
99
+ if self.signals[signal]:
100
+ self.signals[signal](*args)
101
+ except KeyError:
102
+ raise Exception("Call to undefined signal.")
103
+
104
+ def _update_show(self, state, show_tuple):
105
+ (show, episode) = show_tuple
106
+ self.timer = int(1 + self.wait_s - (time.time() - self.last_time))
107
+ self._emit_signal('state', state, self.timer)
108
+
109
+ if self.timer <= 0:
110
+ # Perform show update
111
+ self.last_updated = True
112
+ action = None
113
+ if state == utils.TRACKER_PLAYING:
114
+ action = lambda: self._emit_signal('update', show['id'], episode)
115
+ elif state == utils.TRACKER_NOT_FOUND:
116
+ action = lambda: self._emit_signal('unrecognised', show, episode)
117
+
118
+ if self.wait_close:
119
+ self.msg.info(self.name, 'Waiting for the player to close.')
120
+ self.last_close_queue = action
121
+ elif action:
122
+ action()
123
+
124
+ def _update_state(self, state):
125
+ # Call when show or state is changed. Perform queued update if any, and clear playing flag.
126
+ if self.last_close_queue:
127
+ self.last_close_queue()
128
+ self.last_close_queue = None
129
+ self.last_time = time.time()
130
+ if self.last_show_tuple and self.last_state == utils.TRACKER_PLAYING:
131
+ (last_show, last_show_ep) = self.last_show_tuple
132
+ self._emit_signal('playing', last_show['id'], False, last_show_ep)
133
+
134
+ def update_show_if_needed(self, state, show_tuple):
135
+ # If the state and show are unchanged, skip to countdown
136
+ if show_tuple and state == self.last_state and show_tuple == self.last_show_tuple and not self.last_updated:
137
+ self._update_show(state, show_tuple)
138
+ return
139
+
140
+ if show_tuple and show_tuple != self.last_show_tuple:
141
+ (show, episode) = show_tuple
142
+ self._update_state(state)
143
+ # There's a new show/ep detected, so let's save the show information
144
+ self.last_show_tuple = show_tuple
145
+ if state == utils.TRACKER_PLAYING:
146
+ self._emit_signal('playing', show['id'], True, episode)
147
+ # Check if we shouldn't update the show
148
+ if episode != (show['my_progress'] + 1):
149
+ self.msg.warn(self.name, 'Player is not playing the next episode of %s. Ignoring.' % show['title'])
150
+ self.last_updated = True
151
+ self.last_state = utils.TRACKER_IGNORED
152
+ self._emit_signal('state', utils.TRACKER_IGNORED, None)
153
+ return
154
+
155
+ # Start our countdown
156
+ (show, episode) = show_tuple
157
+ if state == utils.TRACKER_PLAYING:
158
+ self.msg.info(self.name, 'Will update %s %d in %d seconds' % (show['title'], episode, self.wait_s))
159
+ elif state == utils.TRACKER_NOT_FOUND:
160
+ self.msg.info(self.name, 'Will add %s %d in %d seconds' % (show, episode, self.wait_s))
161
+ self._update_show(state, show_tuple)
162
+
163
+ elif self.last_state != state:
164
+ self._update_state(state)
165
+
166
+ # React depending on state
167
+ if state == utils.TRACKER_NOVIDEO: # No video is playing
168
+ # Video didn't get to update phase before it was closed
169
+ if self.last_state == utils.TRACKER_PLAYING and not self.last_updated:
170
+ self.msg.info(self.name, 'Player was closed before update.')
171
+ elif state == utils.TRACKER_UNRECOGNIZED: # There's a new video playing but the regex didn't recognize the format
172
+ self.msg.warn(self.name, 'Found video but the file name format couldn\'t be recognized.')
173
+ elif state == utils.TRACKER_NOT_FOUND: # There's a new video playing but an associated show wasn't found
174
+ self.msg.warn(self.name, 'Found player but show not in list.')
175
+
176
+ self.last_show_tuple = None
177
+ self.last_updated = False
178
+ self.timer = None
179
+ self._emit_signal('state', state, None)
180
+
181
+ self.last_state = state
182
+
183
+ def _get_playing_show(self, filename):
184
+ if not self.active:
185
+ # Don't do anything if the Tracker is disabled
186
+ return (utils.TRACKER_NOVIDEO, None)
187
+
188
+ if filename:
189
+ if filename == self.last_filename:
190
+ # It's the exact same filename, there's no need to do the processing again
191
+ return (self.last_state, self.last_show_tuple)
192
+
193
+ self.last_filename = filename
194
+
195
+ # Do a regex to the filename to get
196
+ # the show title and episode number
197
+ aie = AnimeInfoExtractor(filename)
198
+ (show_title, show_ep) = (aie.getName(), aie.getEpisode())
199
+ if not show_title:
200
+ return (utils.TRACKER_UNRECOGNIZED, None) # Format not recognized
201
+
202
+ playing_show = utils.guess_show(show_title, self.list)
203
+ if playing_show:
204
+ return (utils.TRACKER_PLAYING, (playing_show, show_ep))
205
+ else:
206
+ # Show not in list
207
+ if self.not_found_prompt:
208
+ return (utils.TRACKER_NOT_FOUND, (show_title, show_ep))
209
+ else:
210
+ return (utils.TRACKER_NOT_FOUND, None)
211
+ else:
212
+ self.last_filename = None
213
+ return (utils.TRACKER_NOVIDEO, None) # Not playing