-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
565 lines (469 loc) · 17.4 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
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
import os
import threading
import json
import multiprocessing
from pandas import read_csv
from pebble import concurrent
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.image import AsyncImage
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.button import Button
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.uix.scrollview import ScrollView
from kivy.config import Config
from kivy.core.window import Window
from steal_db_api import StealApi
# Disable multitouch
Config.set("input", "mouse", "mouse,multitouch_on_demand")
_fixed_size = Window.size = (1200, 700)
def reSize(*args):
Window.size = _fixed_size
Window.bind(on_resize=reSize)
############ CUSTOM ELEMENTS ###################
class UpdateBtn(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.background_color = (0.67, 0.70, 0.75, 1)
self.text = "Update"
def Update(self):
app.lib = read_csv(f"{app.steal_path}/library.csv")
app.library.layout.clear_widgets()
print(app.lib)
for i in range(len(app.lib)):
app.library.layout.add_widget(
Card(
cover_url=app.lib["cover"][i],
g_name=app.lib["name"][i],
start_script=app.lib["script"][i],
size_hint_y=None,
height=40,
)
)
def on_press(self):
self.Update()
class DownloadBtn(Button):
def __init__(self, name, magnet, cover, pltfrm, **kwargs):
super().__init__(**kwargs)
self.g_magnet = magnet
self.g_name = name
self.g_cover = cover
self.g_pltfrm = pltfrm
self.background_color = (0.67, 0.70, 0.75, 1)
self.text = "Download"
def download(self):
command = f"webtorrent download '{self.g_magnet}' -o '{app.conf['lib_path']}'"
os.system(command)
self.add_to_lib()
def add_to_lib(self):
arc_file, arc_type = self.get_arc_type()
start_script = ""
if arc_type == "zpaq":
## extract zpaq and get folder
game_dir = arc_file[:-5:]
command = f"cd {app.conf['lib_path']} && zpaq x {arc_file}"
multiprocessing.Process(target=os.system, args=(f"{command}",)).start()
start_script = f"{self.g_pltfrm[0]}start.sh"
elif arc_type == "zst":
## extract zst and get folder name
game_dir = arc_file[:-8:]
command = f"cd {app.conf['lib_path']} && tar -xvf {arc_file}"
multiprocessing.Process(target=os.system, args=(f"{command}",)).start()
start_script = "start.sh"
else:
## dwarfs
game_dir = self.get_game_dir_dwarfs()
start_script = f"start.{self.g_pltfrm[0]}.sh"
params = f"{self.g_name},{self.g_cover},{app.conf['lib_path']}/{game_dir}/{start_script}\n"
## add to library csv
with open(f"{app.steal_path}/library.csv", "a", encoding="utf-8") as file:
file.write(params)
app.lib = read_csv(f"{app.steal_path}/library.csv")
def get_game_dir_dwarfs(self):
game_dir = ""
for root, dirs, files in os.walk(app.conf["lib_path"]):
for dirname in dirs:
if (
str(self.g_name[1:].split(" ", 1)[0].replace(":", "")).title()
in dirname
):
game_dir = dirname
break
break
return game_dir
def get_arc_type(self):
for root, dirs, files in os.walk(app.conf["lib_path"]):
for filename in files:
if self.g_name[1:].split(" ", 1)[0] in filename:
if ".zpaq" in filename:
return filename, "zpaq"
if ".zst" in filename:
return filename, "zst"
return None, "dwarfs"
def on_press(self):
self.disabled = True
self.size_hint_min_x = 200
self.right = 1000
self.text = "Download in progress..."
download_task = multiprocessing.Process(target=self.download)
download_task.start()
class BackToBrowseBtn(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.background_color = (0.67, 0.70, 0.75, 1)
def on_release(self):
app.Sm.transition = SlideTransition(direction="right", duration=0.25)
app.Sm.current = "main_screen"
class BtnAsyncImage(ButtonBehavior, AsyncImage):
def __init__(
self,
g_cover_url,
g_name,
g_size,
g_magnet,
g_pltfrm,
start_script,
g_summary,
**kwargs,
):
super().__init__(**kwargs)
self.g_cover_url = g_cover_url
self.g_name = g_name
self.g_size = g_size
self.g_magnet = g_magnet
self.g_pltfrm = g_pltfrm
self.start_script = start_script
self.g_summary = g_summary
self.launch_game_proc = multiprocessing.Process(
target=self.launch_game, name="launch_game"
)
def on_press(self):
if self.g_magnet:
app.Sm.gamescr.clear_widgets()
app.Sm.gamescr.layout = GameDetailsLayout(
g_cover_url=self.g_cover_url,
g_name=self.g_name,
g_size=self.g_size,
g_magnet=self.g_magnet,
g_pltfrm=self.g_pltfrm,
g_summary=self.g_summary,
)
app.Sm.gamescr.add_widget(app.Sm.gamescr.layout)
def on_release(self):
if self.g_magnet:
app.Sm.transition = SlideTransition(direction="left", duration=0.25)
app.Sm.current = "game_screen"
else:
try:
if not self.launch_game_proc.is_alive():
self.launch_game_proc.start()
else:
print(
"WARNING: game process is still alive, cannot launch game twice"
)
except AssertionError:
self.launch_game_proc.kill()
self.launch_game_proc.join()
self.launch_game_proc.close()
self.launch_game_proc = multiprocessing.Process(
target=self.launch_game, name="launch_game"
)
self.launch_game_proc.start()
def launch_game(self):
os.system(f"sh \"{self.start_script}\"")
class Card(BoxLayout):
def __init__(
self,
cover_url=None,
g_name=None,
g_size=None,
g_magnet=None,
g_pltfrm=None,
start_script=None,
g_summary=None,
**kwargs,
):
super().__init__(**kwargs)
self.orientation = "vertical"
self.size_hint = (None, None)
self.size = (170, 270)
self.padding = (7, 7)
self.cover = BtnAsyncImage(
source=cover_url
if cover_url is not None
else "https://i.imgur.com/tbTWk2n.jpg",
size_hint=(1, 1),
g_cover_url=cover_url
if cover_url is not None
else "https://i.imgur.com/tbTWk2n.jpg",
g_name=g_name,
g_size=g_size,
g_magnet=g_magnet,
g_pltfrm=g_pltfrm,
start_script=start_script,
g_summary=g_summary,
)
self.name = Label(
text=g_name if len(g_name) <= 13 else g_name[:11] + "....",
size_hint=(0.7, 0.1),
padding=(0.2, 0),
)
self.add_widget(self.cover)
self.add_widget(self.name)
class SearchBar(TextInput):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.multiline = False
self.background_color = (0.12, 0.13, 0.16, 1)
self.foreground_color = (1, 1, 1, 1)
self.hint_text = "Search..."
self.hint_text_color = (1, 1, 1, 1)
self.padding = (10, 11)
self.api = StealApi()
@concurrent.process
def get_search(self, g_name):
return self.api.search(g_name)
def on_text_validate(self):
task = self.get_search(self.text)
response = task.result()
app.browse.layout.clear_widgets()
num_cards = int(app.conf["num_of_cards"]) if self.text == "" else len(response)
for i in range(num_cards):
game = response[i]
app.browse.layout.add_widget(
Card(
cover_url=game["cover"],
g_name=game["name"],
g_size=game["size"],
g_magnet=game["magnet"],
g_pltfrm=game["pltfrm"],
g_summary=game["summary"],
size_hint_y=None,
height=40,
)
)
app.lib = read_csv(f"{app.steal_path}/library.csv")
########## SCREENS #################
class MainScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.name = "main_screen"
self.add_widget(MainLayout())
class GameScreen(Screen):
def __init__(self, layout=None, **kwargs):
super().__init__(**kwargs)
self.name = "game_screen"
self.layout = None
if layout:
self.add_widget(layout)
class ScreensManager(ScreenManager):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.mainscr = MainScreen()
self.gamescr = GameScreen()
self.add_widget(self.mainscr)
self.add_widget(self.gamescr)
######### LAYOUTS ###########
class MainLayout(TabbedPanel):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.tab_width = None
self.background_color = (0.12, 0.13, 0.16, 1)
self.browser = TabbedPanelHeader(text="Browse")
self.browser.background_color = (.24, .27, .32, 1)
self.browser.background_down = '.12, .13, .16, 1'
self.browser.content = app.browse
self.add_widget(self.browser)
self.browser.size_hint_x = None
self.browser.width = Window.width / 5
self.default_tab = self.browser
self.library = TabbedPanelHeader(text="Library")
self.library.size_hint_x = None
self.library.width = Window.width / 5
self.library.background_color = (.24, .27, .32, 1)
self.library.background_down = '.12, .13, .16, 1'
self.library.content = app.library
self.add_widget(self.library)
self.label = Label()
self.label.size_hint_x = None
self.label.width = 390
self._tab_strip.add_widget(self.label)
self.update_btn = UpdateBtn()
self.update_btn.size_hint_x = None
self.update_btn.width = 100
self._tab_strip.add_widget(self.update_btn)
self.label2 = Label()
self.label2.size_hint_x = None
self.label2.width = 20
self._tab_strip.add_widget(self.label2)
self.searchbar = SearchBar()
self._tab_strip.add_widget(self.searchbar)
self.searchbar.size_hint_x = None
self.searchbar.width = 200
class BrowseTabLayout(ScrollView):
def __init__(self):
super().__init__()
self.layout = StackLayout(size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter("height"))
self.size_hint = (1, None)
self.size = (Window.width, Window.height + 120)
self.add_widget(self.layout)
self.response = api.search("")
self.cards = []
def add_cards(self):
if self.response:
threading.Thread(target=self.create_cards, name="create_cards").run()
for card in self.cards:
self.layout.add_widget(card)
def create_cards(self):
for i in range(int(app.conf["num_of_cards"])):
game = self.response[i]
self.cards.append(
Card(
cover_url=game["cover"],
g_name=game["name"],
g_size=game["size"],
g_magnet=game["magnet"],
g_pltfrm=game["pltfrm"],
g_summary=game["summary"],
size_hint_y=None,
height=40,
)
)
class LibraryTabLayout(ScrollView):
def __init__(self):
super().__init__()
self.layout = StackLayout(size_hint_y=None)
self.layout.bind(minimum_height=self.layout.setter("height"))
self.cards = []
threading.Thread(target=self.create_cards, name="create_cards_lib").run()
for card in self.cards:
self.layout.add_widget(card)
self.size_hint = (1, None)
self.size = (Window.width, Window.height + 120)
self.add_widget(self.layout)
def create_cards(self):
for i in range(len(app.lib)):
self.cards.append(
Card(
cover_url=app.lib["cover"][i],
g_name=app.lib["name"][i],
start_script=app.lib["script"][i],
size_hint_y=None,
height=40,
)
)
class GameDetailsLayout(FloatLayout):
def __init__(
self, g_cover_url, g_name, g_size, g_magnet, g_pltfrm, g_summary, **kwargs
):
super().__init__(**kwargs)
self.g_name = g_name
self.g_size = g_size
self.g_magnet = g_magnet
self.g_pltfrm = g_pltfrm
self.g_summary = str(g_summary).splitlines()[0]
self.game_info = BoxLayout(orientation="vertical", size_hint=(0.2, 0.2))
self.game_info.pos_hint = {"center_x": 0.5, "top": 0.9}
self.cover = AsyncImage(
source=g_cover_url,
size_hint=(0.58, 0.58),
pos=(-180, 140),
)
self.back_to_browse_btn = BackToBrowseBtn(
text="back", size_hint=(0.08, 0.05), pos=(15, 650)
)
self.add_widget(self.cover)
self.add_widget(self.back_to_browse_btn)
self.labelg_name = Label(
text=self.g_name,
font_size=30,
size_hint=(1, 0.01),
pos_hint={"x": -0.077, "y": 0.85},
text_size=(1000, None),
)
self.add_widget(self.labelg_name)
self.g_summary_label = Label(
text=f"{self.g_summary}",
font_size=19,
size_hint=(1, 0.01),
pos_hint={"x": 0.12, "y": 0.65},
text_size=(800, None),
)
self.add_widget(self.g_summary_label)
self.g_size_pltfrm_label = Label(
text=f"{self.g_size} | {self.g_pltfrm}",
font_size=17,
size_hint=(1, 0.01),
pos_hint={"x": 0.33, "y": 0.28},
)
self.add_widget(self.g_size_pltfrm_label)
self.add_widget(self.game_info)
self.download_btn = DownloadBtn(
name=self.g_name,
magnet=self.g_magnet,
cover=g_cover_url,
pltfrm=g_pltfrm,
size_hint=(0.08, 0.05),
pos=(950, 130),
)
self.add_widget(self.download_btn)
for i in range(len(app.lib)):
if self.g_name == app.lib["name"][i]:
self.download_btn.disabled = True
self.download_btn.size_hint_min_x = 200
self.download_btn.right = 1000
self.download_btn.text = "game already in library!"
break
class StealApp(App):
def build(self):
threading.Thread(target=self.init_steal, name="init_steal").run()
self.browse = BrowseTabLayout()
self.library = LibraryTabLayout()
self.Sm = ScreensManager()
return self.Sm
def on_start(self):
self.browse.add_cards()
def init_steal(self):
# get linux username
self.usr = os.getlogin()
# define config path
self.steal_path = f"/home/{self.usr}/.config/steal"
# if conf.json doesnt exist make it
self.check_conf()
# load config as json obj
self.conf = json.load(open(f"{self.steal_path}/conf.json", encoding="utf-8"))
self.check_lib()
self.lib = read_csv(f"{self.steal_path}/library.csv")
def check_conf(self): # function to check if conf path and file exit
if os.path.exists(
f"{self.steal_path}/conf.json"
): # if path and config file exist exit the function
return
if not os.path.isdir(self.steal_path): # if path doesn't exit
os.makedirs(self.steal_path) # make it
# config params
params = {"lib_path": f"/home/{self.usr}/Games", "num_of_cards": "50"}
# dump the config file
with open(f"{self.steal_path}/conf.json", "w", encoding="utf-8") as file:
json.dump(params, file, indent=4)
def check_lib(self):
if not os.path.isfile(f"{self.steal_path}/library.csv"):
params = "name,cover,script\n"
with open(f"{self.steal_path}/library.csv", "a", encoding="utf-8") as file:
file.write(params)
if __name__ == "__main__":
api = StealApi()
app = StealApp()
app.run()
children = multiprocessing.active_children()
for child in children:
child.terminate()
os.system("clear")
print("see you space cowboy...")
print("CTRL + c to exit")