-
Notifications
You must be signed in to change notification settings - Fork 0
/
habr.py
69 lines (51 loc) · 1.81 KB
/
habr.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
import requests
import telebot
import random
import time
from telebot import types
from bs4 import BeautifulSoup
url = "https://habr.com/ru/news/"
API_KEY = "TOKEN"
headers = {
"Accept": "*/*",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
proxies = {"https": f"http://login:password@ip:port"}
def parser(url):
req = requests.get(url, headers=headers, proxies=proxies)
src = req.text
soup = BeautifulSoup(src, "lxml")
all_news_hrefs = soup.find_all(class_="tm-title__link")
return all_news_hrefs
list_news = []
for item in parser(url):
item_text = item.text
item_href = "https://habr.com" + item.get("href")
all_news = f"Свежак: {item_href}"
list_news.append(all_news)
random.shuffle(list_news)
# print(list_news)
bot = telebot.TeleBot(API_KEY)
@bot.message_handler(commands=["start"])
def hello(message):
bot.send_message(
message.chat.id,
"Привет, чтобы узнать свежие и популярные новости с сайта Harb.com. Введите: /news или нажмите на кнопку",
reply_markup=keyboard(),
)
@bot.message_handler(content_types=["text"])
def popular_news(message):
if message.text == "NEWS":
for i in list_news:
bot.send_message(message.chat.id, list_news[0])
del list_news[0]
time.sleep(7000)
elif message.text == "Кинуть кубик":
bot.send_dice(message.chat.id, reply_markup=keyboard())
def keyboard():
markup = types.ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)
btn1 = types.KeyboardButton("NEWS")
btn2 = types.KeyboardButton("Кинуть кубик")
markup.add(btn1,btn2)
return markup
bot.polling()