Telgram_Bot/lordchannel.py

70 lines
2.1 KiB
Python
Raw Normal View History

2020-06-06 13:53:34 +00:00
import telebot
2020-05-23 13:47:38 +00:00
import bs4, requests
2020-06-06 13:53:34 +00:00
import time
API_TOKEN = '1131828244:AAGdoGSg2wNnqpJxSOwTzeD94TYQKT1C-xM'
2020-05-23 13:47:38 +00:00
url = "https://lordchannel.com/"
2020-06-06 13:53:34 +00:00
bot = telebot.TeleBot(API_TOKEN)
2020-05-23 13:47:38 +00:00
def getLC(url):
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text,"html.parser")
links_with_text = []
2020-06-06 13:53:34 +00:00
# articoli = "\n"
elementi = soup.select('#post-123 > div > div > div.fl-row.fl-row-fixed-width.fl-row-bg-photo.fl-node-5bab8763f0554.fl-row-bg-overlay > div > div > div > div > div > div > div > div > div > ul')
elem = elementi[0].find_all('a',href=True)
for a in elem:
if a.text:
links_with_text.append(a['href'])
articolo_1 = links_with_text[0]
articolo_2 = links_with_text[1]
articolo_3 = links_with_text[2]
articolo_4 = links_with_text[3]
articolo_5 = links_with_text[4]
articolo_6 = links_with_text[5]
# articoli = articoli.join(links_with_text)
# articoli = articoli.join(elementi[0].text.strip())
return articolo_1,articolo_2,articolo_3,articolo_4,articolo_5,articolo_6
articolo_1,articolo_2,articolo_3,articolo_4,articolo_5,articolo_6 = getLC(url)
2020-05-23 13:47:38 +00:00
2020-06-06 13:53:34 +00:00
# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
name = message.from_user.first_name
bot.reply_to(message, f"""\
Ciao {name} , tramite questo bot verrai informato sugli ultimi articoli pubblicati su LORDCHANNEL.
Che cosa aspetti? Inizia subito!\
""")
2020-05-23 13:47:38 +00:00
2020-06-06 13:53:34 +00:00
# Handle '/articoli'
@bot.message_handler(commands=['articoli'])
def ultimi_articoli(message):
articoli = getLC(url)
name = message.from_user.first_name
bot.reply_to(message, f"""\
Ciao {name} , gli ultimi articoli caricati su LORDCHANNEL sono :\
""")
bot.reply_to(message,f"{articolo_1}")
time.sleep(2)
bot.reply_to(message,f"{articolo_2}")
time.sleep(2)
bot.reply_to(message,f"{articolo_3}")
time.sleep(2)
bot.reply_to(message,f"{articolo_4}")
time.sleep(2)
bot.reply_to(message,f"{articolo_5}")
time.sleep(2)
bot.reply_to(message,f"{articolo_6}")
time.sleep(2)
2020-05-23 13:47:38 +00:00
2020-06-06 13:53:34 +00:00
bot.polling()
2020-05-23 13:47:38 +00:00
2020-06-06 13:53:34 +00:00
# # print(f"{elemento}")
2020-05-23 13:47:38 +00:00