-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathServer.py
75 lines (64 loc) · 2.72 KB
/
Server.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
from http.server import BaseHTTPRequestHandler, HTTPServer
import socket
import json
from requests import post
hostName = socket.gethostbyname(socket.gethostname())
serverPort = 8080
def grabDownloadUrl(url): # Grab download link from lovetik api
apiUrl = "https://lovetik.com/api/ajax/search"
req = post(apiUrl,
data = {
"query": url
},
headers = {
"Origin": 'https://lovetik.com/',
"Referer": 'https://lovetik.com/',
})
return req.json()["links"][0]["a"]
class MyServer(BaseHTTPRequestHandler):
def do_GET(self): # Serve web page with instructions
self.send_response(400)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><head><title>Error</title></head>", "utf-8"))
self.wfile.write(bytes("<h1>GET Request: %s</h1>" % self.path, "utf-8"))
self.wfile.write(bytes("<body>", "utf-8"))
self.wfile.write(bytes("<p>Please use POST to request a download.</p>", "utf-8"))
self.wfile.write("<p>The POST request should include TikTok URL using JSON with the key being \"url\".<br>Returned information is returned in JSON format...</p>".encode('utf-8'))
self.wfile.write(bytes("</body></html>", "utf-8"))
return
def do_POST(self): # Send link back to webclient.
content_length = int(self.headers.get('content-length', 0))
post_data = json.loads(self.rfile.read(content_length))
response = dict()
if "url" not in post_data:
self.send_response(400)
self.send_header("Content-type", "application/json")
self.end_headers()
response["error"] = "No URL.\nURL expected to download. Are you fucking stupid?"
else:
tiktok_url = post_data["url"]
download_url = grabDownloadUrl(tiktok_url)
if download_url is None:
self.send_response(400)
self.send_header("Content-type", "application/json")
self.end_headers()
response["error"] = "Problem with grabbing download link. Please check TikTok Link or try again."
else:
response["url"] = download_url
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(response).encode('utf-8'))
return
def main():
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
if __name__ == "__main__":
main()