This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
110 lines (80 loc) · 2.91 KB
/
command.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
from telegram.error import Unauthorized
import parser
import storage
COMMANDS = ('/start', '/add', '/del', '/notify', '/links')
def start(update, context):
text = 'Available commands:\n' + '\n'.join(COMMANDS)
context.bot.send_message(update.effective_chat.id, text=text)
def add(update, context):
chat_id = update.effective_chat.id
try:
link = ''
link_name = ''
if len(context.args) != 2:
raise Exception(
'Wrong args, correct example: /add name=a4 uri=/lv/transport/cars/audi/a4')
for arg in context.args:
if arg.startswith('name='):
link_name = arg.split('=')[1]
continue
if arg.startswith('uri='):
link = arg.split('=')[1]
if not link.endswith('/'):
link += '/'
if not link.startswith('/'):
link = '/' + link
continue
raise Exception(
'Wrong args, correct example: /add name=a4 uri=/lv/transport/cars/audi/a4')
if not link:
raise Exception('link not passed')
if not link_name:
raise Exception('name not passed')
last_ad_id = parser.get_last_ad_id_by_link(link)
storage.save_link(chat_id, link, link_name, last_ad_id)
context.bot.send_message(chat_id, text='Saved')
except Exception as e:
context.bot.send_message(chat_id, text=str(e))
def delete(update, context):
chat_id = update.effective_chat.id
try:
if len(context.args) != 1:
raise Exception(
'You can pass only name argument, example: /del a4')
link_name = context.args[0]
if not link_name:
raise Exception('name not passed')
storage.del_link(chat_id, link_name)
context.bot.send_message(chat_id, text='Successfully deleted')
except Exception as e:
context.bot.send_message(chat_id, text=str(e))
def notify_job(context):
links = storage.get_all()
for link in links:
try:
parser.get_latest(
context.bot,
link['chat_id'],
link['link'],
link['last_ad_id'],
link['name'])
except Exception as e:
try:
context.bot.send_message(link['chat_id'], text=str(e))
except Unauthorized:
remove_all(link['chat_id'])
def remove_all(chat_id):
try:
storage.remove_all(chat_id)
except Exception as e:
print(e)
def get_user_links(update, context):
chat_id = update.effective_chat.id
try:
links = storage.get_links(chat_id)
text = ''
for link in links:
text += f"\n{link['name']}: {link['link']}"
context.bot.send_message(chat_id, text=text)
except Exception as e:
context.bot.send_message(chat_id, text=str(e))