-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
303 lines (231 loc) · 8.7 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import json
import os
import threading
import audios_main
from kivy import Config
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.scrollview import ScrollView
from kivymd.app import MDApp
from kivymd.uix.button import MDFillRoundFlatIconButton, MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import MDList, ImageLeftWidget, OneLineRightIconListItem, ThreeLineAvatarListItem
from kivymd.uix.navigationdrawer import MDNavigationDrawer
from kivymd.uix.toolbar import MDToolbar
Config.set('graphics', 'width', '360')
Config.set('graphics', 'height', '760')
Config.write()
Window.size = (360, 760)
def delete_photos():
try:
with open("filenames.json", "r") as read_file:
filenames = json.load(read_file)
for filename in filenames:
os.remove(filename)
except Exception:
raise
kv = """
MainScreenManager:
MainScreen:
id: mscr
LoginScreen:
<MainScreen>:
id: mscr
name: 'main'
NavigationLayout:
id: navlay
ScreenManager:
id: scrman
Screen:
id: scr
name: 'nav_screen'
MainScreenLayout:
id: mainscrlayout
orientation: 'vertical'
NavDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
padding: "8dp"
spacing: "8dp"
NavList:
id: nav_list
OneLineIconListItem:
text: "Login vk"
on_release: root.manager.current = 'login_screen'
IconLeftWidget:
icon: "login"
OneLineIconListItem:
text: 'Sync music'
on_release: mainscrlayout.show_dialog_login()
IconLeftWidget:
icon: 'sync'
Widget:
<LoginScreen>:
name: 'login_screen'
AnchorLayout:
BoxLayout:
orientation: 'vertical'
size_hint: 0.5, 0.5
spacing: 10
Image:
source: 'spr/music_logov2.png'
size: self.texture_size
MDTextField:
hint_text: "Login"
mode: "rectangle"
MDTextField:
hint_text: "Password"
icon_right: "eye"
password: True
mode: "rectangle"
MDFillRoundFlatButton
icon: "android"
text: 'Log in'
pos_hint: {"center_x": .5, "center_y": .5}
font_size: '20sp'
<Content>
orientation: "vertical"
Widget:
MDSpinner:
size_hint: None, None
size: dp(45), dp(45)
pos_hint: {'center_x': .5, 'center_y': 1}
active: True
Widget:
"""
class MyApp(MDApp):
dialog = None
def __init__(self, **kw):
super().__init__(**kw)
def build(self):
screen = Builder.load_string(kv)
return screen
def open_nav_drawer(self):
self.root.ids.mscr.ids.nav_drawer.set_state('open')
def on_stop(self):
delete_photos()
def on_resume(self):
delete_photos()
def on_pause(self):
delete_photos()
def sync(*args):
vk_session = audios_main.get_session()
if vk_session is None:
print('Session is None!')
audios_main.main()
audios_main.auth_vk()
vk_session = audios_main.get_session()
audios_main.update_audios(vk_session=vk_session)
class MainScreenLayout(BoxLayout):
dialog = None
dialog_spin = None
global my
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.toolbar = MDToolbar(title='My music')
self.toolbar.elevation = 10
self.toolbar.type = 'top'
self.toolbar.left_action_items = [['menu', lambda x: my.root.ids.mscr.ids.nav_drawer.set_state('open')]]
self.add_widget(self.toolbar)
self.add_scroll_widget()
def add_scroll_widget(self):
sc = ScrollOne()
self.scroll_self = sc.get_self()
self.add_widget(self.scroll_self)
def reload_scroll(self):
try:
self.remove_widget(self.scroll_self)
self.add_scroll_widget()
except Exception as ex:
print(ex)
def dialog_close(self, instance):
self.dialog.dismiss()
def dialog_spin_func(self):
self.dialog_spin = MDDialog(title='Loading...',
size_hint=(.7, None),
auto_dismiss=False,
type="custom",
content_cls=Content())
self.dialog_spin.open()
def check_thr(self, *args):
if not self.thr.is_alive():
self.dialog_spin.dismiss()
self.reload_scroll()
self.schedule.cancel()
def sync_music(self, instance):
self.dialog.dismiss()
self.dialog_spin_func()
self.thr = threading.Thread(target=sync, daemon=True)
self.thr.start()
self.schedule = Clock.schedule_interval(self.check_thr, 1)
self.schedule()
def show_dialog_login(self):
self.dialog = MDDialog(type="custom",
size_hint=(.7, None),
auto_dismiss=False,
text="Do you want to sync your music?",
buttons=[MDFlatButton(text="CANCEL",
on_press=self.dialog_close),
MDFlatButton(text="SYNC",
on_press=self.sync_music)])
self.dialog.open()
class Content(BoxLayout):
pass
class MainScreenManager(ScreenManager):
pass
class MainScreen(Screen):
pass
class LoginScreen(Screen):
pass
class NavList(MDList):
pass
class NavDrawer(MDNavigationDrawer):
pass
class ScrollOne(ScrollView):
def __init__(self, **kwargs):
super().__init__(**kwargs)
import wget
self.layout = MDList()
self.layout.spacing = 10
filenames = []
try:
with open("data_audios.json", "r") as rf:
data = json.load(rf)
if len(data) == 0:
sync_but = MDFillRoundFlatIconButton(text='Sync music', icon='sync', on_press=audios_main.main())
self.add_widget(sync_but)
else:
for i in data[:10]:
try:
filename = wget.download(i['track_covers'][0])
filenames.append(filename)
image = ImageLeftWidget(source=filename)
except Exception as ex:
image = ImageLeftWidget(source='spr/music_logo.png')
print(ex)
self.test = OneLineRightIconListItem
self.item = ThreeLineAvatarListItem(text=i['title'],
secondary_text=i['artist'],
tertiary_text=str(i['duration'][0]) + ':' + str(
i['duration'][1]))
self.item.add_widget(image)
self.layout.add_widget(self.item)
try:
with open('filenames.json', 'w') as write_file:
json.dump(filenames, write_file, sort_keys=True, indent=4)
except Exception as ex:
print(ex)
self.add_widget(self.layout)
except Exception as ex:
print(ex)
def get_self(self):
return self
if __name__ == '__main__':
my = MyApp()
my.run()