-
Notifications
You must be signed in to change notification settings - Fork 4
/
urltitle.py
67 lines (60 loc) · 1.94 KB
/
urltitle.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
#urltitle.py by x0rnn, prints the title of a website/youtube link, etc. posted in chat
#sudo apt-get install python3-lxml
#sudo python3.5 -m pip install lxml
#sudo python3.5 -m pip install tld
import minqlx
import threading
import requests
import re
import lxml.html
import socket
from tld import get_tld
class urltitle(minqlx.Plugin):
def __init__(self):
self.add_hook("chat", self.handle_chat)
def handle_chat(self, player, msg, channel):
if channel != "chat":
return
if msg[0] == "!":
return
url = re.findall(r'http[s]?://[^\s<>"]+|www\.[^\s<>"]+', msg)[:1]
if not url:
return
self.getTitle(url)
@minqlx.thread
def getTitle(self, url):
@minqlx.next_frame
def printTitle(status_code):
if status_code == 200:
title = lxml.html.fromstring(www.content)
try:
self.msg("^5URL title: ^7" + title.find(".//title").text)
except:
return
else:
self.msg("^5Invalid URL, error code " + str(status_code))
if not url[0].lower().startswith("http"):
url[0] = ''.join(('http://', url[0]))
try:
tld = get_tld(url[0])
try:
socket.gethostbyname(tld)
except:
return
except:
return
try:
headers = requests.head(url[0]).headers.get('content-type')
if "text/html" not in headers:
return
except:
return
try:
www = requests.get(url[0], headers = {"Connection": "close"})
except requests.exceptions.RequestException:
return
www.connection.close() #not sure if this is necessary but just in case
if www.status_code != requests.codes.ok:
printTitle(www.status_code)
return
printTitle(200)