Migrate modules using urllib2 to use phenny's web module

master
mutantmonkey 2011-09-21 20:43:05 -04:00
parent c8fe22190c
commit f34695717d
4 changed files with 44 additions and 51 deletions

View File

@ -129,14 +129,7 @@ def gettitle(uri):
try: try:
redirects = 0 redirects = 0
while True: while True:
headers = { info = web.head(uri)
'Accept': 'text/html',
'User-Agent': 'Mozilla/5.0 (Phenny)'
}
req = urllib2.Request(uri, headers=headers)
u = urllib2.urlopen(req)
info = u.info()
u.close()
if not isinstance(info, list): if not isinstance(info, list):
status = '200' status = '200'
@ -157,9 +150,9 @@ def gettitle(uri):
if not (('/html' in mtype) or ('/xhtml' in mtype)): if not (('/html' in mtype) or ('/xhtml' in mtype)):
return None return None
u = urllib2.urlopen(req) bytes = web.get(uri)
bytes = u.read(262144) #bytes = u.read(262144)
u.close() #u.close()
except IOError: except IOError:
return return

View File

@ -8,32 +8,33 @@ author: mutantmonkey <mutantmonkey@mutantmonkey.in>
import random import random
from urllib import quote as urlquote from urllib import quote as urlquote
from urllib2 import urlopen, HTTPError from urllib2 import HTTPError
import web
import lxml.html import lxml.html
def fml(phenny, input): def fml(phenny, input):
""".fml""" """.fml"""
try: try:
req = urlopen("http://www.fmylife.com/random") req = web.get("http://www.fmylife.com/random")
except HTTPError: except HTTPError:
phenny.say("I tried to use .fml, but it was broken. FML") phenny.say("I tried to use .fml, but it was broken. FML")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('article')[0][0].text_content() quote = doc.find_class('article')[0][0].text_content()
phenny.say(quote) phenny.say(quote)
fml.commands = ['fml'] fml.commands = ['fml']
def mlia(phenny, input): def mlia(phenny, input):
""".mlia - My life is average.""" """.mlia - My life is average."""
try: try:
req = urlopen("http://mylifeisaverage.com/") req = web.get("http://mylifeisaverage.com/")
except HTTPError: except HTTPError:
phenny.say("I tried to use .mlia, but it wasn't loading. MLIA") phenny.say("I tried to use .mlia, but it wasn't loading. MLIA")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('story')[0][0].text_content() quote = doc.find_class('story')[0][0].text_content()
quote = quote.strip() quote = quote.strip()
phenny.say(quote) phenny.say(quote)
mlia.commands = ['mlia'] mlia.commands = ['mlia']
@ -41,14 +42,14 @@ mlia.commands = ['mlia']
def mliarab(phenny, input): def mliarab(phenny, input):
""".mliarab - My life is Arabic.""" """.mliarab - My life is Arabic."""
try: try:
req = urlopen("http://mylifeisarabic.com/random/") req = web.get("http://mylifeisarabic.com/random/")
except HTTPError: except HTTPError:
phenny.say("The site you requested, mylifeisarabic.com, has been banned \ phenny.say("The site you requested, mylifeisarabic.com, has been banned \
in the UAE. You will be reported to appropriate authorities") in the UAE. You will be reported to appropriate authorities")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quotes = doc.getroot().find_class('entry') quotes = doc.find_class('entry')
quote = random.choice(quotes)[0].text_content() quote = random.choice(quotes)[0].text_content()
quote = quote.strip() quote = quote.strip()
phenny.say(quote) phenny.say(quote)
@ -57,26 +58,26 @@ mliarab.commands = ['mliar', 'mliarab']
def mlib(phenny, input): def mlib(phenny, input):
""".mlib - My life is bro.""" """.mlib - My life is bro."""
try: try:
req = urlopen("http://mylifeisbro.com/random") req = web.get("http://mylifeisbro.com/random")
except HTTPError: except HTTPError:
phenny.say("MLIB is out getting a case of Natty. It's chill.") phenny.say("MLIB is out getting a case of Natty. It's chill.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('storycontent')[0][0].text_content() quote = doc.find_class('storycontent')[0][0].text_content()
phenny.say(quote) phenny.say(quote)
mlib.commands = ['mlib'] mlib.commands = ['mlib']
def mlic(phenny, input): def mlic(phenny, input):
""".mlic - My life is creepy.""" """.mlic - My life is creepy."""
try: try:
req = urlopen("http://mylifeiscreepy.com/random") req = web.get("http://mylifeiscreepy.com/random")
except HTTPError: except HTTPError:
phenny.say("Error: Have you checked behind you?") phenny.say("Error: Have you checked behind you?")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('oldlink')[0].text_content() quote = doc.find_class('oldlink')[0].text_content()
quote = quote.strip() quote = quote.strip()
phenny.say(quote) phenny.say(quote)
mlic.commands = ['mlic'] mlic.commands = ['mlic']
@ -84,65 +85,65 @@ mlic.commands = ['mlic']
def mlid(phenny, input): def mlid(phenny, input):
""".mlib - My life is Desi.""" """.mlib - My life is Desi."""
try: try:
req = urlopen("http://www.mylifeisdesi.com/random") req = web.get("http://www.mylifeisdesi.com/random")
except HTTPError: except HTTPError:
phenny.say("MLID is busy at the hookah lounge, be back soon.") phenny.say("MLID is busy at the hookah lounge, be back soon.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('oldlink')[0].text_content() quote = doc.find_class('oldlink')[0].text_content()
phenny.say(quote) phenny.say(quote)
mlid.commands = ['mlid'] mlid.commands = ['mlid']
def mlig(phenny, input): def mlig(phenny, input):
""".mlig - My life is ginger.""" """.mlig - My life is ginger."""
try: try:
req = urlopen("http://www.mylifeisginger.org/random") req = web.get("http://www.mylifeisginger.org/random")
except HTTPError: except HTTPError:
phenny.say("Busy eating your soul. Be back soon.") phenny.say("Busy eating your soul. Be back soon.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('oldlink')[0].text_content() quote = doc.find_class('oldlink')[0].text_content()
phenny.say(quote) phenny.say(quote)
mlig.commands = ['mlig'] mlig.commands = ['mlig']
def mlih(phenny, input): def mlih(phenny, input):
""".mlih - My life is ho.""" """.mlih - My life is ho."""
try: try:
req = urlopen("http://mylifeisho.com/random") req = web.get("http://mylifeisho.com/random")
except HTTPError: except HTTPError:
phenny.say("MLIH is giving some dome to some lax bros.") phenny.say("MLIH is giving some dome to some lax bros.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('storycontent')[0][0].text_content() quote = doc.find_class('storycontent')[0][0].text_content()
phenny.say(quote) phenny.say(quote)
mlih.commands = ['mlih'] mlih.commands = ['mlih']
def mlihp(phenny, input): def mlihp(phenny, input):
""".mlihp - My life is Harry Potter.""" """.mlihp - My life is Harry Potter."""
try: try:
req = urlopen("http://www.mylifeishp.com/random") req = web.get("http://www.mylifeishp.com/random")
except HTTPError: except HTTPError:
phenny.say("This service is not available to Muggles.") phenny.say("This service is not available to Muggles.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('oldlink')[0].text_content() quote = doc.find_class('oldlink')[0].text_content()
phenny.say(quote) phenny.say(quote)
mlihp.commands = ['mlihp'] mlihp.commands = ['mlihp']
def mlit(phenny, input): def mlit(phenny, input):
""".mlit - My life is Twilight.""" """.mlit - My life is Twilight."""
try: try:
req = urlopen("http://mylifeistwilight.com/random") req = web.get("http://mylifeistwilight.com/random")
except HTTPError: except HTTPError:
phenny.say("Error: Your life is too Twilight. Go outside.") phenny.say("Error: Your life is too Twilight. Go outside.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
quote = doc.getroot().find_class('fmllink')[0].text_content() quote = doc.find_class('fmllink')[0].text_content()
phenny.say(quote) phenny.say(quote)
mlit.commands = ['mlit'] mlit.commands = ['mlit']

View File

@ -5,7 +5,8 @@ author: mutantmonkey <mutantmonkey@gmail.com>
""" """
from urllib import quote as urlquote from urllib import quote as urlquote
from urllib2 import urlopen, HTTPError from urllib2 import HTTPError
import web
import lxml.html import lxml.html
def tfw(phenny, input, fahrenheit=False, celsius=False): def tfw(phenny, input, fahrenheit=False, celsius=False):
@ -22,17 +23,17 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
celsius_param = "&CELSIUS=yes" celsius_param = "&CELSIUS=yes"
try: try:
req = urlopen("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param)) req = web.get("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param))
except HTTPError: except HTTPError:
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.") phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return return
doc = lxml.html.parse(req) doc = lxml.html.fromstring(req)
location = doc.getroot().find_class('small')[0].text_content() location = doc.find_class('small')[0].text_content()
try: try:
weather = doc.getroot().get_element_by_id('content') weather = doc.get_element_by_id('content')
except KeyError: except KeyError:
phenny.say("Unknown location") phenny.say("Unknown location")
return return

View File

@ -17,10 +17,8 @@ def location(name):
name = urllib.quote(name.encode('utf-8')) name = urllib.quote(name.encode('utf-8'))
uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name
for i in xrange(10): for i in xrange(10):
u = urllib.urlopen(uri) bytes = web.get(uri)
if u is not None: break if bytes is not None: break
bytes = u.read()
u.close()
results = web.json(bytes) results = web.json(bytes)
try: name = results['geonames'][0]['name'] try: name = results['geonames'][0]['name']