-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_d.py
executable file
·227 lines (181 loc) · 6.69 KB
/
bot_d.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-
#
# bot_d
# =====
# ver.: 2.1.0
#
# «Я видел некоторое дерьмо»
# Букля.
#
# @SpongeBot
# ~~~~~~~~~~
# Демон для работы с API бота для уютного чатика «/dev/null»
# Умеет много всякого. И продолжает учиться. В отличии от тебя.
#
# (с) @egregors 2015
from __future__ import unicode_literals
import logging
from time import sleep
import click
import requests
import telegram
from config import CHAT_ID, INTERVAL, TOKEN, DOCKER
from grab import Grab
try:
from urllib.error import URLError
except ImportError:
from urllib2 import URLError # python 2
VERSION = 'ver.: 2.1.0'
if DOCKER:
logging.basicConfig(level=logging.WARNING, filename='/var/log/sponge/bot.log')
else:
logging.basicConfig(level=logging.WARNING)
@click.group()
def bot_d():
""" Демон Бота
:return:
"""
pass
@click.group()
def bot_cli():
""" CLI Бота
:return:
"""
pass
@bot_d.command()
def start():
""" Start bot_d daemon """
update_id = None
bot = telegram.Bot(token=TOKEN)
def send(msg: str, ch_id: int):
bot.sendMessage(chat_id=ch_id, text=msg)
def check_updates(b: telegram.Bot, update_id: int) -> int:
for update in b.getUpdates(
offset=update_id,
timeout=INTERVAL,
):
message = update.message.text
upd_chat_id = update.message.chat_id
update_id = update.update_id + 1
cmd = message.lower()
if upd_chat_id in CHAT_ID:
# commands list
if '/ping' in cmd:
send('Pong!', ch_id=upd_chat_id)
elif 'сиськ' in cmd or '/boobs' in cmd:
bot.sendPhoto(chat_id=upd_chat_id, photo=get_boobs_url())
elif 'жоп' in cmd or '/ass' in cmd:
bot.sendPhoto(chat_id=upd_chat_id, photo=get_butts_url())
elif 'курс' in cmd or 'currency' in cmd:
send(msg=get_currency(), ch_id=upd_chat_id)
elif '/ver' in cmd:
send(msg=VERSION, ch_id=upd_chat_id)
else:
pass
return update_id
while True:
try:
update_id = check_updates(bot, update_id)
except telegram.TelegramError as e:
# These are network problems with Telegram.
if e.message in ("Bad Gateway", "Timed out", "Unauthorized"):
sleep(1)
else:
raise e
except URLError as e:
# These are network problems on our end.
sleep(1)
@bot_cli.command()
@click.option('-m', '--msg', help='Message to send', type=str)
@click.option('-c', '--chat_id', help='Chat ID', type=int)
def send(msg: str, chat_id: int):
""" Send the message into Chat """
bot = telegram.Bot(token=TOKEN)
bot.sendMessage(chat_id=chat_id, text=msg)
# ================================================================= #
# AUX functions #
# ================================================================= #
BOOBS_API = 'http://api.oboobs.ru/noise/1'
BOOBS_URL = 'http://media.oboobs.ru/'
BUTTS_API = 'http://api.obutts.ru/noise/1'
BUTTS_URL = 'http://media.obutts.ru/'
def get_boobs_url():
"""
:return: ссылка на картинку с сиськами
"""
try:
request = requests.get(url=BOOBS_API)
except:
return False
if not request.status_code == 200: return False
url = request.json()[0]['preview'].replace('_preview', '')
return BOOBS_URL + url
def get_butts_url():
"""
:return: ссылка на картинку с жопой
"""
try:
request = requests.get(url=BUTTS_API)
except:
return False
if not request.status_code == 200: return False
url = request.json()[0]['preview'].replace('_preview', '')
return BUTTS_URL + url
def get_currency():
"""
Парсим главную одного известного поисковика
:return: текущие биржевые курсы доллара и евро, относительно рубля
"""
g_usd = Grab()
g_eur = Grab()
g_cny = Grab()
g_fuel = Grab()
USD_URL = 'https://news.yandex.ru/quotes/2002.html'
EUR_URL = 'https://news.yandex.ru/quotes/2000.html'
CNY_URL = 'https://news.yandex.ru/quotes/10018.html'
FUEL_URL = 'https://news.yandex.ru/quotes/1006.html'
try:
g_usd.go(USD_URL)
g_eur.go(EUR_URL)
g_cny.go(CNY_URL)
g_fuel.go(FUEL_URL)
except:
return 'FAIL'
resp = g_usd.doc.select('//tr[@class="quote__head"]')
USD = {
'date': resp.select('//td[@class="quote__date"]').text(),
'value': resp.select('//td[@class="quote__value"]').text(),
'change': resp.select('//td[@class="quote__change"]').text(),
}
resp = g_eur.doc.select('//tr[@class="quote__head"]')
EURO = {
'date': resp.select('//td[@class="quote__date"]').text(),
'value': resp.select('//td[@class="quote__value"]').text(),
'change': resp.select('//td[@class="quote__change"]').text(),
}
resp = g_cny.doc.select('//tr[@class="quote__head"]')
CNY = {
'date': resp.select('//td[@class="quote__date"]').text(),
'value': resp.select('//td[@class="quote__value"]').text(),
'change': resp.select('//td[@class="quote__change"]').text(),
}
resp = g_fuel.doc.select('//tr[@class="quote__head"]')
FUEL = {
'date': resp.select('//td[@class="quote__date"]').text(),
'value': resp.select('//td[@class="quote__value"]').text(),
'change': resp.select('//td[@class="quote__change"]').text(),
}
return 'USD: {usd_val} ({usd_change})\n' \
'EUR: {eur_val} ({eur_change})\n' \
'CNY: {cny_val} ({cny_change})\n' \
'=================\n' \
'Brent: {fuel_val} ({fuel_change})'.format(usd_val=USD['value'], usd_change=USD['change'],
eur_val=EURO['value'], eur_change=EURO['change'],
cny_val=CNY['value'], cny_change=CNY['change'],
fuel_val=FUEL['value'], fuel_change=FUEL['change'])
# ================================================================= #
# Main() #
# ================================================================= #
cli = click.CommandCollection(sources=[bot_d, bot_cli])
if __name__ == '__main__':
cli()