Add links parsing in message (without need to type commands)

This commit is contained in:
g1n 2021-11-27 20:13:27 +00:00
parent f14f148901
commit d9d6ef0a70
3 changed files with 37 additions and 10 deletions

View File

@ -1,3 +1,4 @@
import re
import irc
from config import pygbot as c
from config import changeobj
@ -46,6 +47,18 @@ def bot_command_parser(config, bot, user, channel, message):
yt.gettitle(bot, channel, message)
elif message.split()[0] == (c.prefix + "t") or message.split()[0] == (c.prefix + "title"):
web.gettitle(bot, channel, message)
else:
parseiflink(bot, channel, message)
def parseiflink(bot, channel, message):
for i in message.split():
messagehaslink = re.search("http.*://", i)
if messagehaslink != None:
isyoutubelink = re.search("youtu.*be.*", i)
if isyoutubelink != None:
yt.sendyttitle(bot, channel, i)
else:
web.sendtitle(bot, channel, i)
def configure(config, bot, channel, message):
try:

17
web.py
View File

@ -5,13 +5,20 @@ from bs4 import BeautifulSoup
def gettitle(bot, channel, message):
try:
link = ' '.join(message.split()[1:])
iflinkok = re.search("^http", link)
iflinkok = re.search("^http.*://", link)
if iflinkok != None:
page = requests.get(link)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.title.string
bot.send(channel, title)
sendtitle(bot, channel, link)
else:
bot.send(channel, "Failed to get title")
except:
bot.send(channel, "Failed to get title")
def sendtitle(bot, channel, link):
try:
page = requests.get(link)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.title.string
bot.send(channel, title)
except:
bot.send(channel, "Failed to get title")

View File

@ -5,13 +5,20 @@ from bs4 import BeautifulSoup
def gettitle(bot, channel, message):
try:
link = ' '.join(message.split()[1:])
iflinkok = re.search("^http", link)
iflinkok = re.search("^http.*://", link)
if iflinkok != None:
page = requests.get(link)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.find("meta", itemprop="name")["content"]
bot.send(channel, title)
sendyttitle(bot, channel, link)
else:
bot.send(channel, "Failed to get title")
except:
bot.send(channel, "Failed to get title")
def sendyttitle(bot, channel, link):
try:
page = requests.get(link)
soup = BeautifulSoup(page.text, 'html.parser')
title = soup.find("meta", itemprop="name")["content"]
bot.send(channel, title)
except:
bot.send(channel, "Failed to get title")