Add links parsing in message (without need to type commands)
This commit is contained in:
parent
f14f148901
commit
d9d6ef0a70
13
commands.py
13
commands.py
|
@ -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
17
web.py
|
@ -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")
|
||||
|
||||
|
|
17
youtube.py
17
youtube.py
|
@ -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")
|
||||
|
||||
|
|
Loading…
Reference in New Issue