-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvk_bot.py
94 lines (70 loc) · 2.89 KB
/
vk_bot.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
import bs4 as bs4
import requests
class VkBot:
def __init__(self, user_id):
print("\nСоздан объект бота!")
self._USER_ID = user_id
self._USERNAME = self._get_user_name_from_vk_id(user_id)
self._COMMANDS = ["ПРИВЕТ", "ПОГОДА", "ВРЕМЯ", "ПОКА"]
def _get_user_name_from_vk_id(self, user_id):
request = requests.get("https://vk.com/id"+str(user_id))
bs = bs4.BeautifulSoup(request.text, "html.parser")
user_name = self._clean_all_tag_from_str(bs.findAll("title")[0])
return user_name.split()[0]
def new_message(self, message):
# Привет
if message.upper() == self._COMMANDS[0]:
return f"Привет-привет, {self._USERNAME}!"
# Погода
elif message.upper() == self._COMMANDS[1]:
return self._get_weather()
# Время
elif message.upper() == self._COMMANDS[2]:
return self._get_time()
# Пока
elif message.upper() == self._COMMANDS[3]:
return f"Пока-пока, {self._USERNAME}!"
else:
return "Не понимаю о чем вы..."
def _get_time(self):
request = requests.get("https://my-calend.ru/date-and-time-today")
b = bs4.BeautifulSoup(request.text, "html.parser")
return self._clean_all_tag_from_str(str(b.select(".page")[0].findAll("h2")[1])).split()[1]
@staticmethod
def _clean_all_tag_from_str(string_line):
"""
Очистка строки stringLine от тэгов и их содержимых
:param string_line: Очищаемая строка
:return: очищенная строка
"""
result = ""
not_skip = True
for i in list(string_line):
if not_skip:
if i == "<":
not_skip = False
else:
result += i
else:
if i == ">":
not_skip = True
return result
@staticmethod
def _get_weather(city: str = "санкт-петербург") -> list:
request = requests.get("https://sinoptik.com.ru/погода-" + city)
b = bs4.BeautifulSoup(request.text, "html.parser")
p3 = b.select('.temperature .p3')
weather1 = p3[0].getText()
p4 = b.select('.temperature .p4')
weather2 = p4[0].getText()
p5 = b.select('.temperature .p5')
weather3 = p5[0].getText()
p6 = b.select('.temperature .p6')
weather4 = p6[0].getText()
result = ''
result = result + ('Утром :' + weather1 + ' ' + weather2) + '\n'
result = result + ('Днём :' + weather3 + ' ' + weather4) + '\n'
temp = b.select('.rSide .description')
weather = temp[0].getText()
result = result + weather.strip()
return result