-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
36 lines (26 loc) · 1.15 KB
/
app.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
import argparse
from tgstat_scraper import TgStatScraper
def main():
try:
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--search", help="search channels by phrase", type=str)
parser.add_argument("-u", "--url", help="channel URL to get related channels", type=str)
parser.add_argument("-o", "--output", help="text output file", type=str)
args = parser.parse_args()
if ((args.search and args.url) or (not args.search and not args.url)):
raise ValueError("Bad arguments, call -h for help")
tgstat = TgStatScraper.from_settings()
if (args.search and not args.url):
channels = tgstat.search_channels(args.search)
elif (not args.search and args.url):
channels = tgstat.get_related_channels(args.url)
if (args.output):
with open(args.output, 'w') as fp:
fp.write("\n".join(channels))
except Exception as ex:
if '403' in str(ex):
print("Random proxy has been blocked. Try again.")
else:
print(f"{type(ex).__name__}: {ex}")
if __name__ == "__main__":
main()