forked from 05sonicblue/Gamez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGamez.py
217 lines (204 loc) · 10.1 KB
/
Gamez.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
#!/usr/bin/env python
import cherrypy
import os
from lib.WebRoot import WebRoot
import sys
import sched
import time
import threading
import thread
import datetime
import lib.GameTasks
import ConfigParser
import cherrypy.process.plugins
from cherrypy.process.plugins import Daemonizer
from lib.ConfigFunctions import CheckConfigForAllKeys
from lib.DBFunctions import ValidateDB,AddWiiGamesIfMissing,AddXbox360GamesIfMissing,AddComingSoonGames
from lib.Logger import LogEvent
import cherrypy.lib.auth_basic
from lib.FolderFunctions import *
app_path = os.path.dirname(os.path.abspath("__FILE__"))
config_path = os.path.join(app_path,'Gamez.ini')
class RunApp():
def RunWebServer(self,isToDaemonize):
LogEvent("Generating CherryPy configuration")
cherrypy.config.update(config_path)
css_path = os.path.join(app_path,'css')
images_path = os.path.join(app_path,'images')
navigation_images_path = os.path.join(css_path,'navigation_images')
datatables_images_path = os.path.join(css_path,'datatables_images')
js_path = os.path.join(app_path,'js')
theme_path = os.path.join(css_path,'redmond')
theme_images_path = os.path.join(theme_path,'images')
config = ConfigParser.RawConfigParser()
config.read('Gamez.ini')
username = config.get('global','user_name').replace('"','')
password = config.get('global','password').replace('"','')
useAuth = False
if(username <> "" or password <> ""):
useAuth = True
userPassDict = {username:password}
checkpassword = cherrypy.lib.auth_basic.checkpassword_dict(userPassDict)
conf = {
'/':{'tools.auth_basic.on':useAuth,'tools.auth_basic.realm':'Gamez','tools.auth_basic.checkpassword':checkpassword},
'/css': {'tools.staticdir.on':True,'tools.staticdir.dir':css_path},
'/js':{'tools.staticdir.on':True,'tools.staticdir.dir':js_path},
'/css/redmond':{'tools.staticdir.on':True,'tools.staticdir.dir':theme_path},
'/css/redmond/images':{'tools.staticdir.on':True,'tools.staticdir.dir':theme_images_path},
'/css/navigation_images':{'tools.staticdir.on':True,'tools.staticdir.dir':navigation_images_path},
'/css/datatables_images':{'tools.staticdir.on':True,'tools.staticdir.dir':datatables_images_path},
}
if(isToDaemonize == 1):
LogEvent("Preparing to run in daemon mode")
daemon = Daemonizer(cherrypy.engine)
daemon.subscribe()
isSabEnabled = config.get('SystemGenerated','sabnzbd_enabled').replace('"','')
if(isSabEnabled == "1"):
LogEvent("Generating Post Process Script")
GenerateSabPostProcessScript()
RunGameTask()
LogEvent("Getting download interval from config file and invoking scheduler")
config = ConfigParser.RawConfigParser()
config.read('Gamez.ini')
interval = config.get('Scheduler','download_interval').replace('"','')
updateGameListInterval = config.get('Scheduler','game_list_update_interval').replace('"','')
fInterval = float(interval)
fUpdateGameListInterval = float(updateGameListInterval)
try:
LogEvent("Setting up download scheduler")
gameTasksScheduler = cherrypy.process.plugins.Monitor(cherrypy.engine,RunGameTask,fInterval)
gameTasksScheduler.subscribe()
LogEvent("Setting up game list update scheduler")
gameListUpdaterScheduler = cherrypy.process.plugins.Monitor(cherrypy.engine,RunGameListUpdaterTask,fUpdateGameListInterval)
gameListUpdaterScheduler.subscribe()
LogEvent("Setting up folder processing scheduler")
folderProcessingScheduler = cherrypy.process.plugins.Monitor(cherrypy.engine,RunFolderProcessingTask,float(900))
folderProcessingScheduler.subscribe()
LogEvent("Starting the Gamez web server")
cherrypy.quickstart(WebRoot(app_path),'/',config=conf)
except KeyboardInterrupt:
LogEvent("Shutting down Gamez")
if(isToDaemonize == 1):
daemon.unsubscribe()
sys.exit()
def GenerateSabPostProcessScript():
config = ConfigParser.RawConfigParser()
config.read(os.path.join(app_path,'Gamez.ini'))
gamezWebHost = config.get('global','server.socket_host').replace('"','')
gamezWebport = config.get('global','server.socket_port').replace('"','')
gamezBaseUrl = "http://" + gamezWebHost + ":" + gamezWebport + "/"
postProcessPath = os.path.join(app_path,'postprocess')
postProcessScript = os.path.join(postProcessPath,'gamezPostProcess.py')
file = open(postProcessScript,'w')
file.write('#!/usr/bin/env python')
file.write("\n")
file.write('import sys')
file.write("\n")
file.write('import urllib')
file.write("\n")
file.write("filePath = str(sys.argv[1])")
file.write("\n")
file.write('fields = str(sys.argv[3]).split("-")')
file.write("\n")
file.write('gamezID = fields[0].replace("[","").replace("]","").replace(" ","")')
file.write("\n")
file.write("status = str(sys.argv[7])")
file.write("\n")
file.write("downloadStatus = 'Wanted'")
file.write("\n")
file.write("if(status == '0'):")
file.write("\n")
file.write(" downloadStatus = 'Downloaded'")
file.write("\n")
file.write('url = "' + gamezBaseUrl + 'updatestatus?game_id=" + gamezID + "&filePath=" + urllib.quote(filePath) + "&status=" + downloadStatus')
file.write("\n")
file.write('responseObject = urllib.FancyURLopener({}).open(url)')
file.write("\n")
file.write('responseObject.read()')
file.write("\n")
file.write('responseObject.close()')
file.write("\n")
file.write('print("Processing Completed Successfully")')
file.close
LogEvent("Setting permissions on post process script")
cmd = "chmod +x '" + postProcessScript + "'"
os.system(cmd)
def RunGameTask():
try:
config = ConfigParser.RawConfigParser()
config.read('Gamez.ini')
nzbMatrixUser = config.get('NZBMatrix','username').replace('"','')
nzbMatrixApi = config.get('NZBMatrix','api_key').replace('"','')
sabnzbdHost = config.get('Sabnzbd','host').replace('"','')
sabnzbdPort = config.get('Sabnzbd','port').replace('"','')
sabnzbdApi = config.get('Sabnzbd','api_key').replace('"','')
sabnzbdCategory = config.get('Sabnzbd','category').replace('"','')
newznabWiiCat = config.get('Newznab','wii_category_id').replace('"','')
newznabXbox360Cat = config.get('Newznab','xbox360_category_id').replace('"','')
newznabApi = config.get('Newznab','api_key').replace('"','')
newznabHost = config.get('Newznab','host').replace('"','')
newznabPort = config.get('Newznab','port').replace('"','')
isSabEnabled = config.get('SystemGenerated','sabnzbd_enabled').replace('"','')
isNzbMatrixEnabled = config.get('SystemGenerated','nzbmatrix_enabled').replace('"','')
isNewznabEnabled = config.get('SystemGenerated','newznab_enabled').replace('"','')
isNzbBlackholeEnabled = config.get('SystemGenerated','blackhole_nzb_enabled').replace('"','')
nzbBlackholePath = config.get('Blackhole','nzb_blackhole_path').replace('"','')
isTorrentBlackholeEnabled = config.get('SystemGenerated','blackhole_torrent_enabled').replace('"','')
isTorrentKATEnabled = config.get('SystemGenerated','torrent_kat_enabled').replace('"','')
torrentBlackholePath = config.get('Blackhole','torrent_blackhole_path').replace('"','')
manualSearchGame = ''
LogEvent("Searching for games")
lib.GameTasks.GameTasks().FindGames(manualSearchGame,nzbMatrixUser,nzbMatrixApi,sabnzbdApi,sabnzbdHost,sabnzbdPort,newznabWiiCat,newznabApi,newznabHost,newznabPort,newznabXbox360Cat,sabnzbdCategory,isSabEnabled,isNzbMatrixEnabled,isNewznabEnabled,isNzbBlackholeEnabled,nzbBlackholePath,isTorrentBlackholeEnabled,isTorrentKATEnabled,torrentBlackholePath)
except:
errorMessage = "Major error occured when running scheduled tasks"
for message in sys.exc_info():
errorMessage = errorMessage + " - " + str(message)
LogEvent(errorMessage)
def RunGameListUpdaterTask():
try:
LogEvent("Updating Game Lists")
AddWiiGamesIfMissing()
LogEvent("Wii Game List Updated")
AddXbox360GamesIfMissing()
LogEvent("XBOX 360 Game List Updated")
AddComingSoonGames
LogEvent("Coming Soon Game List Updated")
except:
errorMessage = "Major error occured when running Update Game List scheduled tasks"
for message in sys.exc_info():
errorMessage = errorMessage + " - " + str(message)
LogEvent(errorMessage)
def RunFolderProcessingTask():
try:
ScanFoldersToProcess()
except:
errorMessage = "Error occurred while processing folders"
for message in sys.exc_info():
errorMessage = errorMessage + " - " + str(message)
LogEvent(errorMessage)
if __name__ == '__main__':
app_path = sys.path[0]
LogEvent("Checking DB")
ValidateDB()
LogEvent("Checking config file for completeness")
CheckConfigForAllKeys(app_path)
config = ConfigParser.RawConfigParser()
configFilePath = os.path.join(app_path,'Gamez.ini')
config.read(configFilePath)
sabnzbdHost = config.get('Sabnzbd','host').replace('"','')
sabnzbdPort = config.get('Sabnzbd','port').replace('"','')
sabnzbdApi = config.get('Sabnzbd','api_key').replace('"','')
LogEvent("Attempting to get download completed directory from Sabnzbd")
sabCompleted = lib.GameTasks.GameTasks().CheckSabDownloadPath(sabnzbdApi,sabnzbdHost,sabnzbdPort)
if(sabCompleted <> ""):
LogEvent("Setting Value")
config.set('Folders','sabnzbd_completed','"' + sabCompleted + '"')
LogEvent("Trying to save")
with open(configFilePath,'wb') as configFile:
config.write(configFile)
isToDaemonize = 0
params = sys.argv
for param in params:
if(param == "-d"):
isToDaemonize = 1
RunApp().RunWebServer(isToDaemonize)