This repository was archived by the owner on Dec 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplex-scrobble.py
More file actions
executable file
·146 lines (119 loc) · 4.86 KB
/
Copy pathplex-scrobble.py
File metadata and controls
executable file
·146 lines (119 loc) · 4.86 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
#!/usr/local/bin/python
import os
import sys
import platform
import logging
import time
import threading
import ConfigParser
import signal
from optparse import OptionParser
from plex_scrobble.lastfm import LastFm
from plex_scrobble.plex_monitor import monitor_log
from plex_scrobble.scrobble_cache import ScrobbleCache
from plex_scrobble.pre_check import PLSSanity
def platform_log_directory():
"""
Retrieves the default platform specific default log location.
This is called if the user does not specify a log location in
the configuration file.
github issue https://github.com/jesseward/plex-lastfm-scrobbler/issues/5
"""
LOG_DEFAULTS = {
'Darwin': os.path.expanduser('~/Library/Logs/Plex Media Server.log'),
'Linux': '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Logs/Plex Media Server.log',
'Windows': os.path.join(os.environ.get('LOCALAPPDATA', 'c:'), 'Plex Media Server/Logs/Plex Media Server.log'),
'FreeBSD': '/usr/local/plexdata/Plex Media Server/Logs/Plex Media Server.log',
}
return LOG_DEFAULTS[platform.system()]
def cache_retry(config):
"""
Thread timer for the cache retry logic.
:param config: config (ConfigParser obj)
"""
while True:
logger.info('starting cache_retry thread.')
cache = ScrobbleCache(config)
# do not retry if cache is empty.
if cache.length() > 0:
cache.retry_queue()
cache.close()
time.sleep(3600)
def main(config):
"""
The main thread loop
:param config: config (ConfigParser obj)
"""
logger.info('starting log monitor thread.')
log_watch = threading.Thread(target=monitor_log, args=(config,))
log_watch.daemon = True
log_watch.start()
# retry cache every hour.
cache_thread = threading.Thread(target=cache_retry, args=(config,))
cache_thread.daemon = True
cache_thread.start()
# main thread ended/crashed. exit.
#log_watch.join()
#cache_thread.join()
try:
while True:
signal.pause()
except KeyboardInterrupt:
logger.info('KeyboardInterrupt.')
sys.exit(1)
if __name__ == '__main__':
p = OptionParser()
p.add_option('-c', '--config', action='store', dest='config_file',
help='The location to the configuration file.')
p.add_option('-p', '--precheck', action='store_true', dest='precheck',
default=False, help='Run a pre-check to ensure a correctly configured system.')
p.add_option('-a', '--authenticate', action='store_true', dest='authenticate',
default=False, help='Generate a new last.fm session key.')
p.set_defaults(config_file=os.path.expanduser(
'~/.config/plex-lastfm-scrobbler/plex_scrobble.conf'))
(options, args) = p.parse_args()
if not os.path.exists(options.config_file):
print 'Exiting, unable to locate config file {0}. use -c to specify config target'.format(
options.config_file)
sys.exit(1)
# apply defaults to *required* configuration values.
config = ConfigParser.ConfigParser(defaults = {
'config file location': options.config_file,
'session': os.path.expanduser('~/.config/plex-lastfm-scrobbler/session_key'),
'mediaserver_url': 'http://localhost:32400',
'mediaserver_log_location': platform_log_directory(),
'log_file': '/tmp/plex_scrobble.log'
})
# ISSUE https://github.com/jesseward/plex-lastfm-scrobbler/issues/34
try:
config.read(options.config_file)
except ConfigParser.Error:
print 'ERROR: unable to parse config file "{file}". Syntax error?'.format(
file=options.config_file)
sys.exit(1)
FORMAT = '%(asctime)-15s [%(process)d] [%(name)s %(funcName)s] [%(levelname)s] %(message)s'
logging.basicConfig(filename=config.get('plex-scrobble',
'log_file'), format=FORMAT, level=logging.DEBUG)
logger = logging.getLogger('main')
# dump our configuration values to the logfile
for key in config.items('plex-scrobble'):
logger.debug('config : {0} -> {1}'.format(key[0], key[1]))
if options.precheck:
pc = PLSSanity(config)
pc.run()
logger.warn('Precheck completed. Exiting.')
sys.exit(0)
# if a valid session object does not exist, prompt user
# to authenticate.
if (not os.path.exists(config.get('plex-scrobble','session')) or
options.authenticate):
logger.info('Prompting to authenticate to Last.fm.')
last_fm = LastFm(config)
last_fm.last_fm_auth()
print 'Please relaunch plex-scrobble service.'
logger.warn('Exiting application.')
sys.exit(0)
logger.debug('using last.fm session key={key} , st_mtime={mtime}'.format(
key=config.get('plex-scrobble','session'),
mtime=time.ctime(os.path.getmtime(config.get('plex-scrobble','session'))) ))
m = main(config)