-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathback.py
190 lines (172 loc) · 6.53 KB
/
back.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
import os
import signal
import sys
import threading
import time
import urllib.parse
from http.server import BaseHTTPRequestHandler, HTTPServer
from playsound import playsound
# Variables
host = ("0.0.0.0", 8080)
versionNum = "40"
# Constants
soundPlayCount = -1
soundPlayStop = False
soundPlaying = False
soundForIdle = False
lastUrl = ""
visitCount = 0
errorCount = 0
noError = 0
serverRunning = True
def play(file):
if (file == None):
file = "se_ymd05.wav"
global soundPlayStop, soundPlaying, soundPlayCount
while (soundPlayStop == False):
if (soundPlayCount > 0):
soundPlayCount = soundPlayCount - 1
elif (soundPlayCount == 0):
break
log("sound play for one time")
playsound("res/" + file)
soundPlayStop = True
soundPlaying = False
def logToFile(str):
with open("cbsatar.log", "a") as logFile:
logFile.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ": " + str + "\n")
def logToConsole(str):
print("[College Board SAT Auto Registration] " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ": " + str)
def log(str):
logToConsole(str)
logToFile(str)
soundPlayThread = threading.Thread(target=play, args=("se_ymd05.wav",))
def idle():
global soundPlaying, soundPlayCount, soundPlayStop, soundForIdle, soundPlayThread
log("event: idle")
soundForIdle = True
if (soundPlaying == False):
soundPlayCount = -1
soundPlayStop = False
soundPlaying = True
soundPlayThread = threading.Thread(target=play, args=("se_ymd05.wav",))
soundPlayThread.start()
else:
soundPlayCount = -1
timerIdle = threading.Timer(60.0, idle)
class Request(BaseHTTPRequestHandler):
def do_GET(self):
global soundPlayStop, soundPlaying, soundPlayCount, lastUrl, visitCount, errorCount, noError, timerIdle, soundForIdle, soundPlayThread
self.send_response(200)
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Content-type", "application/json")
self.end_headers()
urlInfo = urllib.parse.urlparse(self.path)
path = urlInfo.path
query = urllib.parse.parse_qs(urlInfo.query)
if (path == "/startPlay") or (path == "/stopPlay") or (path == "/visit") or (path == "/errorHandler") or (path == "/log") or (path == "/test"):
self.send_response(200)
if (soundForIdle == True):
soundForIdle = False
soundPlayStop = True
soundPlaying = False
log("event: stop playing sound by idle")
timerIdle.cancel()
timerIdle = threading.Timer(60.0, idle)
timerIdle.start()
else:
self.send_response(404)
self.end_headers()
return
if path == "/startPlay":
log("event: start playing sound")
if (query.get("reason")):
log("reason: " + query.get("reason")[0])
if (query.get("count")):
log("count: " + query.get("count")[0])
if (query.get("file")):
file = query.get("file")[0]
else:
file = "se_ymd05.wav"
if (query.get("count")):
soundPlayCount = int(query.get("count")[0])
else:
soundPlayCount = -1
while (soundPlayThread.is_alive()):
soundPlayStop = True
soundPlaying = False
soundPlayStop = False
soundPlaying = True
soundPlayThread = threading.Thread(target=play, args=(file,))
soundPlayThread.start()
responseBody = "completed"
elif path == "/stopPlay":
soundPlayStop = True
soundPlaying = False
log("event: stop playing sound by request")
responseBody = "completed"
elif path == "/visit":
print()
log("event: site visited")
if (query.get("url")):
if (lastUrl == query.get("url")[0]):
visitCount = visitCount + 1
else:
lastUrl = query.get("url")[0]
errorCount = 0
visitCount = 1
noError = noError + 1
log("url: " + query.get("url")[0])
log("times visited this page: " + str(visitCount))
log("times visited without error: " + str(noError))
responseBody = "completed"
elif path == "/errorHandler":
log("event: error occured")
if (query.get("url")):
log("url: " + query.get("url")[0])
if (query.get("e")):
log("message: " + query.get("e")[0])
errorCount = errorCount + 1
noError = 0
log("times errors occured: " + str(errorCount))
responseBody = str(errorCount)
elif path == "/log":
log("event: console log")
if (query.get("url")):
log("url: " + query.get("url")[0])
if (query.get("content")):
log("content: " + query.get("content")[0])
responseBody = "completed"
elif path == "/test":
responseBody = "completed"
self.wfile.write(responseBody.encode("utf-8"))
log("respond to frontend")
def log_message(self, format, *args):
return
def getSlash():
if (os.name == "nt"):
return "\\"
else:
return "/"
server = HTTPServer(host, Request)
def runServer():
global serverRunning
while (serverRunning == True):
server.handle_request()
serverThread = threading.Thread(target=runServer, args=())
def interruptHandler(sig, frame):
global serverRunning
logToFile("Interrupted by user.")
print("Shutting down...")
serverRunning = False
sys.exit(0)
if (__name__ == "__main__"):
print("College Board SAT Auto Registration Backend Ver. " + versionNum +
"\n\nCopyright (C) 2020 TURX\nThis program comes with ABSOLUTELY NO WARRANTY with GNU GPL v3 license. This is free software, and you are welcome to redistribute it under certain conditions; go to https://www.gnu.org/licenses/gpl-3.0.html for details.\n")
open("cbsatar.log", "w").close()
print("Starting log, write to: " + os.getcwd() + getSlash() + "cbsatar.log")
print("Starting interrupt handler, you can press Ctrl+C or send SIGINT to exit")
signal.signal(signal.SIGINT, interruptHandler)
serverThread.start()
print("Starting server, listen at: %s:%s" % host)
logToFile("Service started.")