-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
157 lines (94 loc) · 4.02 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
# main.py
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, CardTransition
from kivymd.uix.screen import MDScreen
# from kivy.lang import Builder
from kivymd.uix.list import OneLineAvatarIconListItem, ThreeLineAvatarIconListItem, ImageLeftWidget
import shelve
# from kivy.clock import Clock
from scrap import download_webpage
import os
# from kivy.resources import resource_add_path, resource_find
from threading import Thread
# path = os.getcwd().a
#----------------Custom widgets------------------------#
class CustomListItem(OneLineAvatarIconListItem):
def delete_item(self, text):
"""Delete list item and remove item from the shelve file"""
with shelve.open('./save_files/mydata')as shelf_file:
url_list = shelf_file['url_list']
url_list.remove(str(text))
shelf_file['url_list'] = url_list
self.parent.remove_widget(self)
#------------------Screens-------------------------------#
class ScreenManagement(ScreenManager):
pass
class MainWindow(MDScreen):
def refresh_callback(self, *args):
print("Refreshing...")
def refresh_callback(interval):
self.ids.box.clear_widgets()
self.get_anime_info()
self.ids.refresh_layout.refresh_done()
self.tick = 0
# Clock.schedule_once(refresh_callback, 1)
anime_thread = Thread(target=refresh_callback, args=(1,))
anime_thread.start()
def get_anime_info(self):
with shelve.open('./save_files/mydata') as shelf_file:
url_list = shelf_file['url_list']
for url in url_list:
download_webpage(url)
with shelve.open('./save_files/mydata') as shelf_file:
for key in shelf_file.keys():
if key != 'url_list':
print(key)
anime = shelf_file[key]
episodes = anime['episodes']
completed = anime['completed']
# ongoing = anime['ongoing']
image = anime['image']
anime_complete = 'completed' if completed else 'ongoing'
list_item = ThreeLineAvatarIconListItem(text=key, secondary_text=f"[b]Status:[/b] {anime_complete}", tertiary_text=f"[b]Episodes:[/b] {episodes}")
list_item.add_widget(ImageLeftWidget(source=f"./images/{image}"))
self.ids.box.add_widget(list_item)
class AddUrlScreen(MDScreen):
def add_url(self, text):
"""Add the url to list and save the url in shelve file"""
self.ids.linklist.add_widget(CustomListItem(text=text))
self.ids.linkinput.focus = True
self.ids.linkinput.text = ''
# saving to shelve file
with shelve.open('./save_files/mydata') as shelf_file:
url_list = shelf_file['url_list']
url_list.append(str(text))
shelf_file['url_list'] = url_list
def on_pre_enter(self):
try:
with shelve.open('./save_files/mydata') as shelf_file:
self.ids.linklist.clear_widgets()
for item in shelf_file['url_list']:
self.ids.linklist.add_widget(CustomListItem(text=item))
except KeyError:
with shelve.open('./save_files/mydata') as shelf_file:
shelf_file['url_list'] = []
#-----------------Main App-------------------#
class MainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "DeepPurple"
self.root.transition= CardTransition()
def on_start(self):
try:
os.mkdir('images')
os.mkdir('save_files')
except:
pass
def open_settings_screen(self):
"""open setting window"""
self.root.current = 'addurl'
self.root.transition.direction = 'down'
def return_to_main_window(self):
self.root.current = 'mainscreen'
self.root.transition.direction = 'up'
if __name__ == '__main__':
MainApp().run()