-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
81 lines (69 loc) · 2.41 KB
/
interface.py
File metadata and controls
81 lines (69 loc) · 2.41 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
import readline
import args
import pydoc
import pandas as pd
from dumper import Dumper
from tabulate import tabulate
class Interface:
def __init__(self):
self.cli_args = args.parse()
try:
self.d = Dumper(self.cli_args.api_id,
self.cli_args.api_hash,
self.cli_args.output,
self.cli_args.media)
except KeyboardInterrupt as _:
print("quit...")
self.commands = {
"list":self._list_channels,
"dump":self._dump
}
self.channels = []
def _list_channels(self,args:list, should_print: bool = True) -> None:
"""list available channels."""
self.channels = self.d.list_available_channels()
data = {
"name":[],
"telegram id":[]
}
counter = 0
for ch in self.channels:
data["name"].append(ch["title"])
data["telegram id"].append(ch["id"])
counter += 1
self.df = pd.DataFrame(data)
table = tabulate(self.df, headers='keys', tablefmt='psql')
if should_print: pydoc.pager(table)
def _get_channel_id(self, inner_id:int):
pass
def _dump(self, args:list) -> None:
"""dump channel using its id."""
if len(args) != 1:
print("dtc: error: dump requires exactly 1 argument that is channel id!")
return
inner_id = args[0]
if not inner_id.isdigit():
print("dtc: error: '{}' should be integer!".format(inner_id))
return
inner_id = int(inner_id)
try:
if not hasattr(self,"df"):self._list_channels([],False)#create df
row = self.df.iloc[inner_id]
ch_id = row["telegram id"]
self.d.dump_channel_by_id(ch_id)
except IndexError as _:
print("dtc: error: '{} doesn't exist!".format(inner_id))
return
def _run(self) -> None:
head,*tail = input(">>").split()
if head not in self.commands.keys():
print("dtc: error: no command '{}'!".format(head))
else:
self.commands[head](tail)
def run(self) -> None:
while True:
try:
self._run()
except KeyboardInterrupt as _:
print("quit...")
return