-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (67 loc) · 2.34 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
78
79
80
81
82
83
84
85
86
87
88
89
90
import eel
import spotifyApi
import youtubeApi
from Playlist import Playlist
from Song import Song
import json
import os.path
from Encoder import Encoder
# Set web files folder and optionally specify which file types to check for eel.expose()
# *Default allowed_extensions are: ['.js', '.html', '.txt', '.htm', '.xhtml']
eel.init('web', allowed_extensions=['.js', '.html'])
playlists = []
current_playlist = None
def initialize():
# Load existing saved playlists if there are any
global playlists
if os.path.exists('playlists.json'):
settings = open('playlists.json')
temp = json.load(settings)
for pl in temp:
playlist = Playlist('')
playlist.deserialize(pl)
playlists.append(playlist)
settings.close()
youtubeApi.setup()
spotifyApi.authorize_spotify()
@eel.expose
def search(query: str):
results = {'spotify': spotifyApi.get_tracks(query), 'youtube': youtubeApi.get_videos(query)}
return json.dumps(results)
@eel.expose
def create_playlist(name: str) -> str:
"""
Create a new playlist with a default name.
:return: JSON list of all the current playlists
"""
playlist = Playlist(name)
playlists.append(playlist)
print(playlists)
with open('playlists.json', 'w+') as f:
json.dump([pl.reprJSON() for pl in playlists], f, cls=Encoder, indent=3)
return json.dumps([pl.reprJSON() for pl in playlists], cls=Encoder, indent=3)
@eel.expose
def get_playlists() -> str:
return json.dumps([pl.reprJSON() for pl in playlists], cls=Encoder, indent=3)
@eel.expose
def set_current_playlist(playlist) -> None:
global current_playlist
current_playlist = Playlist('')
current_playlist.deserialize(playlist)
print(current_playlist)
@eel.expose
def get_current_playlist() -> str:
return json.dumps(current_playlist.reprJSON(), cls=Encoder, indent=3)
@eel.expose
def add_song_to_playlist(playlist, song) -> None:
print(song)
pl = Playlist('')
pl.deserialize(playlist)
sn = Song('', '', 0, '', '', '', 1)
sn.deserialize(song)
print(sn)
playlists[playlists.index(pl)].add_song(sn)
with open('playlists.json', 'w+') as f:
json.dump([plist.reprJSON() for plist in playlists], f, cls=Encoder, indent=3)
initialize()
eel.start('templates/index.html', size=(1400, 700), jinja_templates='templates')