forked from styxit/HTPC-Manager
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Htpc.py
executable file
·268 lines (226 loc) · 9.21 KB
/
Htpc.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is the main executable of HTPC Manager. It parses the
command line arguments, sets globals variables and calls the
start function to start the server.
"""
import os
import sys
import htpc
import webbrowser
import locale
import logging
def parse_arguments():
""" Get variables from commandline """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--datadir', default=None,
help='Set the datadirectory')
parser.add_argument('--db', default=None,
help='Use a custom database')
parser.add_argument('--host', default=None,
help='Use a specific host/IP')
parser.add_argument('--port', type=int,
help='Use a specific port')
parser.add_argument('--shell', action='store_true', default=False,
help='This argument has has been deprecated')
parser.add_argument('--daemon', action='store_true', default=False,
help='Daemonize process')
parser.add_argument('--pid', default=False,
help='Generate PID file at location')
parser.add_argument('--debug', action='store_true', default=False,
help='This parameter has been deprecated')
parser.add_argument('--dev', action='store_true', default=False,
help='Used while developing, prints debug messages uncensored, autoreload etc')
parser.add_argument('--openbrowser', action='store_true', default=False,
help='Open the browser on server start')
parser.add_argument('--webdir', default=None,
help='Use a custom webdir')
parser.add_argument('--resetauth', action='store_true', default=False,
help='Resets the username and password to HTPC Manager')
parser.add_argument('--loglevel',
help='Set a loglevel. Allowed values: debug, info, warning, error, critical')
parser.add_argument('--nocolor', action='store_true', default=False,
help='Disable colored terminal text')
return parser.parse_args()
def load_modules():
""" Import the system modules """
from htpc.root import Root
htpc.ROOT = Root()
from htpc.settings import Settings
htpc.ROOT.settings = Settings()
from htpc.log import Log
htpc.ROOT.log = Log()
from htpc.updater import Updater
htpc.ROOT.update = Updater()
# Import all modules.
from modules.kodi import Kodi
htpc.ROOT.kodi = Kodi()
from modules.sabnzbd import Sabnzbd
htpc.ROOT.sabnzbd = Sabnzbd()
from modules.couchpotato import Couchpotato
htpc.ROOT.couchpotato = Couchpotato()
from modules.sickbeard import Sickbeard
htpc.ROOT.sickbeard = Sickbeard()
from modules.transmission import Transmission
htpc.ROOT.transmission = Transmission()
from modules.deluge import Deluge
htpc.ROOT.deluge = Deluge()
from modules.squeezebox import Squeezebox
htpc.ROOT.squeezebox = Squeezebox()
from modules.newznab import Newznab
htpc.ROOT.newznab = Newznab()
from modules.utorrent import UTorrent
htpc.ROOT.utorrent = UTorrent()
from modules.nzbget import NZBGet
htpc.ROOT.nzbget = NZBGet()
from modules.qbittorrent import Qbittorrent
htpc.ROOT.qbittorrent = Qbittorrent()
from modules.stats import Stats
htpc.ROOT.stats = Stats()
from modules.tvheadend import TVHeadend
htpc.ROOT.tvheadend = TVHeadend()
from modules.torrentsearch import Torrentsearch
htpc.ROOT.torrentsearch = Torrentsearch()
from modules.plex import Plex
htpc.ROOT.plex = Plex()
from modules.users import Users
htpc.ROOT.users = Users()
from modules.sonarr import Sonarr
htpc.ROOT.sonarr = Sonarr()
from modules.radarr import Radarr
htpc.ROOT.radarr = Radarr()
from modules.sickrage import Sickrage
htpc.ROOT.sickrage = Sickrage()
from modules.samsungtv import Samsungtv
htpc.ROOT.samsungtv = Samsungtv()
from modules.vnstat import Vnstat
htpc.ROOT.vnstat = Vnstat()
from modules.headphones import Headphones
htpc.ROOT.headphones = Headphones()
from modules.mylar import Mylar
htpc.ROOT.mylar = Mylar()
from modules.rtorrent import RTorrent
htpc.ROOT.rtorrent = RTorrent()
from modules.plexpy import Plexpy
htpc.ROOT.plexpy = Plexpy()
from modules.ombi import Ombi
htpc.ROOT.ombi = Ombi()
def init_sched():
from apscheduler.schedulers.background import BackgroundScheduler
htpc.SCHED = BackgroundScheduler()
htpc.SCHED.start()
def main():
"""
Main function is called at startup.
"""
# Parse runtime arguments
args = parse_arguments()
# Set root and insert bundled libraies into path
htpc.RUNDIR = os.path.dirname(os.path.abspath(sys.argv[0]))
sys.path.insert(0, os.path.join(htpc.RUNDIR, 'libs'))
try:
locale.setlocale(locale.LC_ALL, "")
htpc.SYS_ENCODING = locale.getpreferredencoding()
except (locale.Error, IOError):
pass
# for OSes that are poorly configured I'll just force UTF-8
if not htpc.SYS_ENCODING or htpc.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
htpc.SYS_ENCODING = 'UTF-8'
if not hasattr(sys, "setdefaultencoding"):
reload(sys)
# python 2.7.9 verifies certs by default. This disables it
if sys.version_info >= (2, 7, 9):
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Set datadir, create if it doesn't exist and exit if it isn't writable.
htpc.DATADIR = os.path.join(htpc.RUNDIR, 'userdata')
if args.datadir:
htpc.DATADIR = args.datadir
if not os.path.isdir(htpc.DATADIR):
os.makedirs(htpc.DATADIR)
if not os.access(htpc.DATADIR, os.W_OK):
sys.exit("No write access to userdata folder")
htpc.SCRIPTDIR = os.path.join(htpc.DATADIR, 'scripts')
if not os.path.isdir(htpc.SCRIPTDIR):
os.makedirs(htpc.SCRIPTDIR)
from mako.lookup import TemplateLookup
# Enable dev mode if needed
htpc.DEV = args.dev
# Set default database and overwrite if supplied through commandline
htpc.DB = os.path.join(htpc.DATADIR, 'database.db')
if args.db:
htpc.DB = args.db
# Load settings from database
from htpc.settings import Settings
htpc.settings = Settings()
# Set default loglevel
htpc.LOGLEVEL = htpc.settings.get('app_loglevel', 'info')
if args.loglevel:
htpc.LOGLEVEL = args.loglevel.lower()
htpc.settings.set('app_loglevel', args.loglevel.lower())
# Check for SSL
htpc.USE_SSL = htpc.settings.get('app_use_ssl')
htpc.SSLCERT = htpc.settings.get('app_ssl_cert')
htpc.SSLKEY = htpc.settings.get('app_ssl_key')
htpc.WEBDIR = htpc.settings.get('app_webdir', '/')
if args.webdir:
htpc.WEBDIR = args.webdir
if not htpc.WEBDIR.startswith('/'):
htpc.WEBDIR = '/' + htpc.WEBDIR
if not htpc.WEBDIR.endswith('/'):
htpc.WEBDIR += '/'
htpc.TEMPLATE = os.path.join(htpc.RUNDIR, 'interfaces/',
htpc.settings.get('app_template', 'default'))
htpc.LOOKUP = TemplateLookup(directories=[os.path.join(htpc.TEMPLATE, 'html/')])
# Overwrite host setting if supplied through commandline
htpc.HOST = htpc.settings.get('app_host', '0.0.0.0')
if args.host:
htpc.HOST = args.host
# Overwrite port setting if supplied through commandline
htpc.PORT = int(htpc.settings.get('app_port', 8085))
if args.port:
htpc.PORT = args.port
htpc.USERNAME = htpc.settings.get('app_username')
htpc.PASSWORD = htpc.settings.get('app_password')
# Is used for hiding logout in the menu
if htpc.USERNAME and htpc.PASSWORD:
htpc.AUTH = True
else:
htpc.AUTH = False
# Resets the htpc manager password and username
if args.resetauth:
htpc.USERNAME = htpc.settings.set('app_username', '')
htpc.PASSWORD = htpc.settings.set('app_password', '')
htpc.NOCOLOR = args.nocolor
# Open webbrowser
if args.openbrowser or htpc.settings.get('openbrowser') and not htpc.DEV:
browser_ssl = 's' if htpc.SSLCERT and htpc.SSLKEY and htpc.settings.get('app_use_ssl') else ''
if htpc.settings.get('app_host') == '0.0.0.0':
browser_host = 'localhost'
else:
browser_host = htpc.settings.get('app_host', 'localhost')
openbrowser = 'http%s://%s:%s%s' % (browser_ssl, str(browser_host), htpc.PORT, htpc.WEBDIR[:-1])
webbrowser.open(openbrowser, new=2, autoraise=True)
# Select if you want to control processes and open from HTPC Manager
htpc.SHELL = args.shell
# Select whether to run as daemon
htpc.DAEMON = args.daemon
# Set Application PID
htpc.PID = args.pid
# Initialize Scheduler
init_sched()
# Inititialize root and settings page
load_modules()
logger = logging.getLogger('root')
if args.debug:
logger.warning('Commandline parameter --debug has been deprecated')
if args.shell:
logger.warning('Shell parameter --shell has been deprecated')
htpc.ARGS = sys.argv
# Start the server
from htpc.server import start
start()
if __name__ == '__main__':
main()