-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
963 lines (726 loc) · 25.1 KB
/
main.py
File metadata and controls
963 lines (726 loc) · 25.1 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.core.clipboard import Clipboard
from kivy.metrics import dp
from kivymd.uix.snackbar import (
MDSnackbar,
MDSnackbarSupportingText,
MDSnackbarText,
)
from kivymd.uix.list import (
MDListItem,
MDListItemHeadlineText,
MDListItemLeadingIcon)
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import (
MDDialog,
MDDialogIcon,
MDDialogHeadlineText,
MDDialogSupportingText,
MDDialogButtonContainer,
MDDialogContentContainer,
)
from kivymd.uix.dialog import (
MDDialog,
MDDialogIcon,
MDDialogHeadlineText,
MDDialogSupportingText,
MDDialogButtonContainer,
MDDialogContentContainer,
)
from kivymd.uix.divider import MDDivider
from kivymd.uix.list import (
MDListItem,
MDListItemLeadingIcon,
MDListItemSupportingText,
)
from kivymd.uix.filemanager import MDFileManager
from kivymd.uix.button import MDIconButton
import webbrowser
import json
import os
import requests
## == Global variables == ##
method = "anon"
period = "1h"
last_screen = "catbox"
current_screen = "catbox"
list_to_show = "anon"
file_cb = r""
file_cb_name = ""
file_lb = r""
cba_items = []
cbb_items = []
lb_items = []
## == Load settings == ##
# Load settings
def load_settings():
with open('settings.json', 'r') as f:
settings = json.load(f)
return settings
# Save settings
def save_settings(settings):
with open('settings.json', 'w') as f:
json.dump(settings, f, indent=4)
# List LB
def list_lb():
with open('list_lb.json', 'r') as f:
list_lb = json.load(f)
return list_lb
# List CB-anon
def list_cb_a():
with open('list_cb_a.json', 'r') as f:
list_lb = json.load(f)
return list_lb
# List CB-auth
def list_cb_b():
with open('list_cb_b.json', 'r') as f:
list_lb = json.load(f)
return list_lb
# Internet connection
def ping():
# Show dialog when there is an connection problem
def dialog(message):
dialog = MDDialog(
MDDialogIcon(icon="alert",),
MDDialogHeadlineText(text="Attention!",),
MDDialogSupportingText(text=message,),
)
dialog.open()
# Check connection and availability of Catbox
def check(url, message):
try:
response = requests.get(url, timeout=5)
if response.ok:
print(f'{url} pong')
return True
else:
raise requests.exceptions.RequestException()
except requests.exceptions.RequestException:
print(f'No connection or responce issue with {url}')
dialog(message)
return False
# Check if user have internet connection (by google becasue it's available all time)
if check("https://www.google.com",
"It looks like you have connection issues! The main functions of the app will not work!"):
# Check if Catbox is available after knowing that the user is online
# Don't check Litterbox because it has the domain of Catbox
if check("https://catbox.moe",
"It looks like Catbox is not available now! The main functions of the app will not work!"):
print("All systems go")
## === App === ##
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.bind(on_keyboard=self.events)
# File Manager
self.manager_open = False
self.file_manager = MDFileManager(
exit_manager=self.exit_manager,
select_path=self.select_path,
preview=True,
#ext=[]
)
def build(self):
# App color palette
self.theme_cls.primary_palette = "Midnightblue"
# Load theme
settings = load_settings()
current_theme = settings['theme']
self.theme_cls.theme_style = current_theme
# Load UI file
return Builder.load_file('ui.kv')
def on_start(self):
self.title = "CatBox Client" # App title
self.add_lb()
self.add_cb_a()
ping()
### ===== File manager ===== ###
# Open file manager
def file_manager_open(self):
self.file_manager.show_disks()
self.file_manager.show(
os.path.expanduser("~"))
self.manager_open = True
def select_path(self, path: str):
self.exit_manager()
global file_cb
global file_cb_name
global file_lb
global file_lb_name
global current_screen
if os.path.isfile(path):
print(f"Chosen file: {path}")
snack_text = f"File: {path}"
else:
print("Choose file, not a folder!")
snack_text = "Choose file, not a folder!"
MDSnackbar(
MDSnackbarSupportingText(
text=snack_text,
),
y=dp(24),
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
).open()
if current_screen == "catbox":
file_cb = path
file_cb_name = os.path.basename(file_cb)
print("Chosen file for Catbox: ", file_cb)
print("Chosen name for Catbox: ", file_cb_name)
else:
file_lb = path
file_lb_name = os.path.basename(file_lb)
print("Chosen file for Litterbox: ", file_lb)
print("Chosen name for Litterbox:", file_lb_name)
def exit_manager(self, *args):
self.manager_open = False
self.file_manager.close()
def events(self, instance, keyboard, keycode, text, modifiers):
if keyboard in (1001, 27):
if self.manager_open:
self.file_manager.back()
return True
### ===== Buttons ===== ###
# Copy link
def link(self, option):
link = option
Clipboard.copy(link)
print("Link copied to the clipboard")
# Github
def github(self):
webbrowser.open("https://github.com/pythonCBK/catbox-client")
# Swicth app theme
def switch_theme(self):
settings = load_settings()
current_theme = settings['theme']
if current_theme == "Light":
self.theme_cls.theme_style = "Dark"
new_theme = "Dark"
settings['theme'] = new_theme
save_settings(settings)
else:
self.theme_cls.theme_style = "Light"
new_theme = "Light"
settings['theme'] = new_theme
save_settings(settings)
# Save hash
def save_hash(self):
settings = load_settings()
current_hash = settings['userhash']
new_hash = self.root.ids.entry.text.strip()
clear_hash = new_hash.replace(" ", "")
if clear_hash == current_hash:
self.show_hash("same")
return
if clear_hash == "":
self.show_hash("invalid")
return
settings['userhash'] = clear_hash
save_settings(settings)
self.show_hash("update")
# Copy userhash
def copy_hash(self):
settings = load_settings()
current_hash = settings['userhash']
Clipboard.copy(current_hash)
# Update method
def method_upd(self, option):
global method
method = option
print("Method updated:", method)
# Update period
def period_upd(self, option):
global period
period = option
print("Period updated:", period)
# Update list choise
def list_choice_upd(self, option):
global list_to_show
if option == list_to_show:
return
if option == "anon":
list_to_show = "anon"
self.add_cb_a()
else:
list_to_show = "auth"
self.add_cb_b()
print("List to show updated:", list_to_show)
### ===== UPLOADS ===== ###
# Save file Catbox - ANON
def add_list_cb_a(self, option):
global file_cb_name
link = option
filename = file_cb_name
datalist = list_cb_a()
# Object to add
new_filename = filename
new_link = {
"link": link
}
datalist[new_filename] = new_link
with open('list_cb_a.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
# Save file Catbox - AUTH
def add_list_cb_b(self, option):
global file_cb_name
link = option
filename = file_cb_name
datalist = list_cb_b()
# Object to add
new_filename = filename
new_link = {
"link": link
}
datalist[new_filename] = new_link
with open('list_cb_b.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
# Save file Litterbox
def add_list_lb(self, option):
global file_lb_name
link = option
filename = file_lb_name
datalist = list_lb()
# Object to add
new_filename = filename
new_link = {
"link": link
}
datalist[new_filename] = new_link
with open('list_lb.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
# Upload to Catbox - ANON
def upl_anon(self):
global file_cb
url = "https://catbox.moe/user/api.php"
with open(file_cb, 'rb') as file:
files = {'fileToUpload': file}
data = {'reqtype': 'fileupload'}
response = requests.post(url, files=files, data=data)
if response.status_code == 200:
return response.text.strip()
else:
return f"Upload error. Status code: {response.status_code}"
# Upload to Catbox - AUTH
def upl_auth(self):
global file_cb
url = "https://catbox.moe/user/api.php"
settings = load_settings()
userhash = settings['userhash']
with open(file_cb, 'rb') as file:
files = {'fileToUpload': file}
data = {
'reqtype': 'fileupload',
'userhash': userhash
}
response = requests.post(url, files=files, data=data)
if response.status_code == 200:
return response.text.strip()
else:
return f"Upload error. Status code: {response.status_code}"
# Upload to Litterbox
def upl_lb(self):
global file_lb
global period
url = "https://litterbox.catbox.moe/resources/internals/api.php"
try:
with open(file_lb, 'rb') as file:
response = requests.post(
url,
files={'fileToUpload': file},
data={
'reqtype': 'fileupload',
'time': period
}
)
return response.text if response.ok else f"Ошибка {response.status_code}"
except Exception as error:
return f"Errr: {str(error)}"
# Call Catbox uploads
def upload_cb(self):
global method
global file_cb
if file_cb == "":
return
else:
if method == "anon":
result = self.upl_anon()
print("Anon uploaded file:", result)
notification = MDSnackbar(
MDSnackbarSupportingText(
text = f"Catbox uploaded file (anon): {result}",
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
notification.open()
self.add_list_cb_a(result)
else:
result = self.upl_auth()
print("Auth uploaded file:", result)
notification = MDSnackbar(
MDSnackbarSupportingText(
text = f"Catbox uploaded file (auth): {result}",
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
notification.open()
self.add_list_cb_b(result)
# Call Litterbox uploads
def call_upload_lb(self):
global file_lb
if file_lb == "":
return
else:
result = self.upl_lb()
print("Litterbox uploaded file:", result)
notification = MDSnackbar(
MDSnackbarSupportingText(
text = f"Litterbox uploaded file: {result}",
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
notification.open()
self.add_list_lb(result)
self.add_lb()
# Remove LB
def remove_from_lb(self, option):
datalist = list_lb()
to_remove = option
if to_remove in datalist:
del datalist[to_remove]
else:
print(f"Name {to_remove} not found!")
with open('list_lb.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
self.add_lb()
# Remove CB-a
def remove_cb_a(self, option):
datalist = list_cb_a()
to_remove = option
if to_remove in datalist:
del datalist[to_remove]
else:
print(f"Name {to_remove} not found!")
return
with open('list_cb_a.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
self.add_cb_b()
self.add_cb_a()
# Remove CB-b (json)
def remove_cb_b_list(self, option):
datalist = list_cb_b()
to_remove = option
# Remove from .json
if to_remove in datalist:
del datalist[to_remove]
else:
print(f"Name {to_remove} not found!")
with open('list_cb_b.json', 'w', encoding='utf-8') as file:
json.dump(datalist, file, ensure_ascii=False, indent=4)
notification = MDSnackbar(
MDSnackbarText(text = "Success"),
MDSnackbarSupportingText(text = "File was deleted from Catbox"),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
notification.open()
self.add_cb_a()
self.add_cb_b()
# Remove CB-b
def remove_cb_b(self, option):
global cbb_items
datalist = list_cb_b()
to_remove = option
settings = load_settings()
userhash = settings['userhash']
url = "https://catbox.moe/user/api.php"
def show_error():
message = "Could not delete file from Catbox. Check your userhash or try later."
notification = MDSnackbar(
MDSnackbarText(text = "Error"),
MDSnackbarSupportingText(text=message),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
notification.open()
if to_remove in datalist:
link = datalist[to_remove]["link"]
print(link)
else:
print(f"File {to_remove} not found")
return
link = datalist[to_remove]["link"]
new_link = link.replace("https://files.catbox.moe/", "")
# Remove from Catbox
data = {
'reqtype': 'deletefiles',
'files': new_link,
'userhash': userhash
}
response = requests.post(url, data=data)
if response.status_code == 200:
if "success" in response.text.lower():
self.remove_cb_b_list(option=to_remove)
return "File was deleted from server"
else:
show_error()
return f"Error: {response.text}"
else:
show_error()
return f"Request error. Error code: {response.status_code}"
### ===== CB List UPDATE ===== ###
# Remove CB list
def remove_cb(self, option):
global cba_items
global cbb_items
if option == 'b':
for item_id in cba_items:
item = self.root.ids[item_id]
item.parent.remove_widget(item)
else:
for item_id in cbb_items:
item = self.root.ids[item_id]
item.parent.remove_widget(item)
# CB-a list
def add_cb_a(self):
wordlist = list_cb_a()
global cba_items
self.remove_cb('a')
cba_items.clear()
# Add to list
for name in wordlist:
list_container = self.root.ids.menu_cb
link = wordlist[name]["link"]
## === Item settings === ##
item = MDListItem()
# ID
item_id = name
item.my_custom_id = item_id
# Icon
icon = MDListItemLeadingIcon(
icon = 'folder-multiple-image')
item.add_widget(icon)
# Text
text = MDListItemHeadlineText(
text = name)
item.add_widget(text)
# Buttons
btn1 = MDIconButton(
icon = 'link',
on_release=
lambda x, link=link: self.link(link)
)
btn2 = MDIconButton(
icon = 'close-circle-outline',
on_release=
lambda x, name=name: self.remove_cb_a(name)
)
item.add_widget(btn1)
item.add_widget(btn2)
# Adding item
list_container.add_widget(item)
self.root.ids[item_id] = item
cba_items.append(item_id)
print("CB-a items:", cba_items)
# CB-b list
def add_cb_b(self):
wordlist = list_cb_b()
global cbb_items
self.remove_cb('b')
cbb_items.clear()
# Add to list
for name in wordlist:
list_container = self.root.ids.menu_cb
link = wordlist[name]["link"]
## === Item settings === ##
item = MDListItem()
# ID
item_id = name
item.my_custom_id = item_id
# Icon
icon = MDListItemLeadingIcon(
icon = 'folder-multiple-image')
item.add_widget(icon)
# Text
text = MDListItemHeadlineText(
text = name)
item.add_widget(text)
# Buttons
btn1 = MDIconButton(
icon = 'link',
on_release=
lambda x, link=link: self.link(link)
)
btn2 = MDIconButton(
icon = 'close-circle-outline',
on_release=
lambda x, name=name: self.remove_cb_b(name)
)
item.add_widget(btn1)
item.add_widget(btn2)
# Adding item
list_container.add_widget(item)
self.root.ids[item_id] = item
cbb_items.append(item_id)
print("CB-b items:", cbb_items)
### ===== LB List UPDATE ===== ###
# Remove LB list
def remove_lb(self):
global lb_items
for item_id in lb_items:
item = self.root.ids[item_id]
item.parent.remove_widget(item)
# Add LB list
def add_lb(self):
wordlist = list_lb()
global lb_items
self.remove_lb()
lb_items.clear()
# Add to list
for name in wordlist:
list_container = self.root.ids.menu_lb
link = wordlist[name]["link"]
## === Item settings === ##
item = MDListItem()
# ID
item_id = name
item.my_custom_id = item_id
# Icon
icon = MDListItemLeadingIcon(
icon = 'folder-multiple-image')
item.add_widget(icon)
# Text
text = MDListItemHeadlineText(
text = name)
item.add_widget(text)
# Buttons
btn1 = MDIconButton(
icon = 'link',
on_release=
lambda x, link=link: self.link(link)
)
btn2 = MDIconButton(
icon = 'close-circle-outline',
on_release=
lambda x, name=name: self.remove_from_lb(name)
)
item.add_widget(btn1)
item.add_widget(btn2)
# Adding item
list_container.add_widget(item)
self.root.ids[item_id] = item
lb_items.append(item_id)
print("LB items:", lb_items)
### ===== Settings ===== ###
# Current Screen upd
def current_screen_upd(self):
global current_screen
if current_screen == "catbox":
current_screen = "litterbox"
else:
current_screen = "catbox"
print("Current screen:", current_screen)
# Last Screen upd to cb
def last_screen(self):
global last_screen
global current_screen
if current_screen == "catbox":
last_screen = "catbox"
else:
last_screen = "litterbox"
# Go back
def back(self):
global last_screen
if last_screen == "litterbox":
self.root.current = "litterbox"
else:
self.root.current = "catbox"
### === Notifications === ###
# Show current userhash
def show_hash(self, option):
settings = load_settings()
current_hash = settings['userhash']
if option == "show":
if current_hash == "":
support_text = "No userhash provided"
else:
support_text = current_hash
show = MDSnackbar(
MDSnackbarText(
text = "Current userhash:",
),
MDSnackbarSupportingText(
text = support_text,
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
show.open()
if option == "update":
up_text = "New userhash"
support_text = current_hash
show = MDSnackbar(
MDSnackbarText(
text = up_text,
),
MDSnackbarSupportingText(
text = support_text,
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
show.open()
if option == "invalid":
up_text = "Userhash was not updated!"
support_text = "Provided userhash is invalid."
show = MDSnackbar(
MDSnackbarText(
text = up_text,
),
MDSnackbarSupportingText(
text = support_text,
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
show.open()
if option == "same":
up_text = "Userhash was not updated!"
support_text = "Provided userhash is the same."
show = MDSnackbar(
MDSnackbarText(
text = up_text,
),
MDSnackbarSupportingText(
text = support_text,
),
y=dp(10),
orientation="horizontal",
pos_hint={"center_x": 0.5},
size_hint_x=0.8,
)
show.open()
MainApp().run()