-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmega-cli.py
55 lines (44 loc) · 1.67 KB
/
mega-cli.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
from megadebrid.libs.api import MegaDebridApi
from megadebrid.libs.ajax import MegaDebridAjax
from megadebrid.libs.flow import MegaDebridFlow
from megadebrid.parsers.argparser import MegaArgParser
from inspect import getfullargspec
import asyncio
import signal
class MegaCLI:
def __init__(self) -> None:
self.args = MegaArgParser.parse_args()
self.objects = {
"ajax": MegaDebridAjax,
"api": MegaDebridApi,
"flow": MegaDebridFlow,
}
self.SIGINT = False
def signal_handler(self, signal, frame) -> None:
print("\n MegaCLI has been interrupted!!")
self.SIGINT = True
async def run_until_interrupt(self, func, *args, **kwargs) -> None:
signal.signal(signal.SIGINT, self.signal_handler)
while not self.SIGINT:
await func(*args, **kwargs)
await asyncio.sleep(3)
async def async_run(self) -> None:
async with self.objects[self.args.lib]() as megadebrid:
megadebrid_method = getattr(
megadebrid, MegaArgParser.method_resolver(self.args.command)
)
method_args = set(getfullargspec(megadebrid_method).args[1:])
require_args = {
key: value
for key, value in vars(self.args).items()
if key in method_args
}
result = await megadebrid_method(**require_args)
print(result)
# TODO: need to be implemented
# await a.run_until_interrupt('none')
if __name__ == "__main__":
megacli = MegaCLI()
loop = asyncio.get_event_loop()
loop.run_until_complete(megacli.async_run())
loop.close()