This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (59 loc) · 2.32 KB
/
main.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
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
from gmusicapi import Mobileclient
api = Mobileclient()
with open('key.txt', 'r') as file:
key = file.read()
if not api.oauth_login(key): #valide device-id
api.perform_oauth()
if not api.is_authenticated():
print("Authentication failed somehow")
quit()
with open('static/header.html', 'r') as file:
header = file.read().encode("utf-8")
with open('static/footer.html', 'r') as file:
footer = file.read().encode("utf-8")
with open('playlist.txt', 'r') as file:
playlist = file.read()
searchform = b'<form method="POST"><input type="text" class="search" name="search" placeholder="Liedwunsch"><button type="submit">Suchen</button></form>'
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(header + searchform + footer)
def do_POST(self):
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
arrpost = body.split(b'=')
self.send_response(200)
self.end_headers()
response = BytesIO()
if arrpost[0] == b'search':
response.write(header)
response.write(b'<form method="POST"><table>')
hits = api.search(str(arrpost[1], "utf-8"))
songhits = hits["song_hits"]
for song in songhits:
response.write(b'<tr><td><b>')
response.write(song["track"]["title"].encode("utf-8"))
response.write(b'</b><hr>')
response.write(song["track"]["artist"].encode("utf-8"))
response.write(b'<hr>')
response.write(song["track"]["album"].encode("utf-8"))
response.write(b'</td><td><button type="submit" name="add" value="')
response.write(song["track"]["storeId"].encode("utf-8"))
response.write(b'"><img height="40px" width="40px" src="')
response.write(song["track"]["albumArtRef"][0]["url"].encode("utf-8"))
response.write(b'"></button></td></tr>')
response.write(b'</table></form>')
response.write(footer)
elif arrpost[0] == b'add':
song = str(arrpost[1], "utf-8")
api.add_songs_to_playlist(playlist, [song])
response.write(header)
response.write(b'<h2>Lied gespeichert!</h2><br>')
response.write(searchform)
response.write(footer)
self.wfile.write(response.getvalue())
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()