-
Notifications
You must be signed in to change notification settings - Fork 6
/
url_title.py
52 lines (41 loc) · 1.38 KB
/
url_title.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
# vim:set ts=8 sts=8 sw=8 tw=80 noet:
import re
import socket
import urllib.request
from bs4 import BeautifulSoup
from multiprocessing import Process
class Urltitle(object):
def __init__(self):
self.buffer = ['Keine URL gespeichert.']
def __call__(self, msg, nick, send_message):
if "http" in msg.lower():
result = self.save_url(msg, nick)
elif "!titel" == msg.strip():
result = self.output_url(msg, nick)
else:
return False
if result:
send_message(result)
return True
return False
def save_url(self, msg, nick):
self.buffer = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+[^http]', msg)
if ":" in self.buffer[0]:
self.buffer = "Keine URL gespeichert."
return False
def output_url(self, msg, nick):
url_array = []
try:
try:
url = urllib.request.urlopen(self.buffer[0],None, 5)
meta = url.info()
print(meta)
website = url.read()
bs = BeautifulSoup(website)
return bs.find('title').text
except socket.timeout:
return '** TIMEOUT **'
except Exception as e:
print(e)
return "Keine URL gespeichert."
return False