nemubot/modules/ycc/Tinyfier.py

38 lines
1.0 KiB
Python
Raw Normal View History

2012-06-30 02:06:59 +00:00
# coding=utf-8
2012-06-30 14:37:02 +00:00
import http.client
2012-06-30 02:06:59 +00:00
import threading
import re
2012-06-30 02:06:59 +00:00
class Tinyfier(threading.Thread):
def __init__(self, url, msg):
self.url = url
self.msg = msg
threading.Thread.__init__(self)
def run(self):
(status, page) = getPage("ycc.fr", "/redirection/create/" + self.url)
if status == http.client.OK and len(page) < 100:
srv = re.match(".*((ht|f)tps?://|www.)([^/ ]+).*", self.url)
if srv is None:
self.msg.send_chn("Mauvaise URL : %s" % (self.url))
else:
self.msg.send_chn("URL pour %s : %s" % (srv.group(3), page.decode()))
else:
print ("ERROR: ycc.fr seem down?")
self.msg.send_chn("La situation est embarassante, il semblerait que YCC soit down :(")
def getPage(s, p):
2012-07-27 16:18:13 +00:00
conn = http.client.HTTPConnection(s, timeout=10)
2012-06-30 02:06:59 +00:00
try:
conn.request("GET", p)
except socket.gaierror:
print ("[%s] impossible de récupérer la page %s."%(s, p))
return None
res = conn.getresponse()
data = res.read()
conn.close()
return (res.status, data)