This repository has been archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardor.py
208 lines (178 loc) · 6.1 KB
/
ardor.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
from typer import Typer
from manager import Manager
from downloader import Downloader
from scraper import Scraper
from rich.console import Group
from rich.live import Live
from rich import print
from msvcrt import getch
import os
cli = Typer()
def selection_menu(options: list[str], transient=False):
def generate_text(index):
renderables = []
for i, option in enumerate(options, start=1):
if index == i:
renderables.append(f"[magenta bold]{option}[/magenta bold]")
else:
renderables.append(f"[blue]{option}[/blue]")
return Group(*renderables)
with Live(generate_text(1), auto_refresh=False, transient=transient) as live:
i = 1
selected = False
while True:
key = ord(getch())
changed = False
if key == 80: # Down Arrow
i += 1
if i > len(options):
i = 1
changed = True
elif key == 72: # Up Arrow
i -= 1
if i < 1:
i = len(options)
changed = True
elif key == 13: # Enter
selected = True
break
elif key == 27: # Escape
break
if changed:
live.update(generate_text(i))
live.refresh()
changed = False
if selected:
return i - 1, options[i - 1]
def selection_menu_mutiple(options: list[str], transient=False):
def generate_text(selected_indices: list[int], current_index):
renderables = []
for i, option in enumerate(options):
if i in selected_indices:
if i == current_index:
renderables.append(f"[magenta bold]>(●) {option}[/magenta bold]")
else:
renderables.append(f" [magenta bold](●) {option}[/magenta bold]")
elif i == current_index:
renderables.append(f"[cyan bold]>(○) {option}[/cyan bold]")
else:
renderables.append(f" [blue](○) {option}[/blue]")
return Group(*renderables)
selected_indices = []
selected_options = []
i = 0
with Live(
generate_text(selected_indices, i), auto_refresh=False, transient=transient
) as live:
while True:
key = ord(getch())
changed = False
if key == 80: # Down Arrow
i += 1
if i == len(options):
i = 0
changed = True
elif key == 72: # Up Arrow
i -= 1
if i == -1:
i = len(options) - 1
changed = True
elif key == 32: # Spacebar
if i not in selected_indices:
selected_indices.append(i)
selected_options.append(options[i])
else:
selected_indices.remove(i)
selected_options.remove(options[i])
changed = True
elif key == 13: # Enter
return selected_indices, selected_options
elif key == 27: # Escape
return
if changed:
live.update(generate_text(selected_indices, i))
live.refresh()
changed = False
@cli.command()
def shows():
manager = Manager()
manager.load_shows_watching()
print(manager.watching_shows())
@cli.command()
def play():
manager = Manager()
manager.load_unwatched_episodes()
if not manager.episodes_unwatched:
print("[red bold]No episodes in watchlist[/red bold]")
else:
i, _ = selection_menu(
list(map(lambda x: f"{x['show']} {x['ep']}", manager.episodes_unwatched))
)
downloader = Downloader()
episode_path = f"{downloader.base_directory}\{manager.episodes_unwatched[i]['show']}\{manager.episodes_unwatched[i]['title']}"
os.system(f'"{episode_path}"')
@cli.command()
def add(query: str):
scraper = Scraper()
_, selected_show = selection_menu(scraper.get_all_shows(query))
manager = Manager()
print(manager.add_show(selected_show))
@cli.command()
def remove(all: bool = False):
manager = Manager()
manager.load_shows_watching()
indices, _ = selection_menu_mutiple(manager.shows_watching)
for output in manager.remove_show(indices):
print(output)
@cli.command()
def download():
scraper = Scraper()
if newly_added := scraper.get_new_episodes():
print("[bold green]Episodes available for download![/bold green]")
if (
a := selection_menu_mutiple(
list(map(lambda x: f"{x['show']} {x['ep']}", newly_added)),
transient=True,
)
) is not None:
selection_indices, _ = a
downloader = Downloader()
for index in selection_indices:
print(downloader.start_torrent(newly_added[index]))
else:
print("[red bold]No episode selected[red bold]")
else:
print("[red bold]No new episodes![/red bold]")
@cli.command()
def complete():
manager = Manager()
manager.load_unwatched_episodes()
if manager.episodes_unwatched:
selected_indices, _ = selection_menu_mutiple(
list(map(lambda x: f"{x['show']} {x['ep']}", manager.episodes_unwatched)),
transient=True,
)
j = 0
for output in manager.complete(selected_indices):
print(output)
else:
print("[yellow bold]No unwatched episodes[/yellow bold]")
@cli.command()
def watchlist():
manager = Manager()
manager.load_unwatched_episodes()
print(manager.watchlist())
@cli.command()
def username(username: str):
downloader = Downloader()
downloader.set_username(username)
@cli.command()
def password(password: str):
downloader = Downloader()
downloader.set_password(password)
@cli.command()
def directory(base_directory: str):
downloader = Downloader()
downloader.set_base_directory(base_directory)
if __name__ == "__main__":
cli()