Better formatting, 4-space indents, use http.get_soup instead of BeautifulSoup directly
This commit is contained in:
parent
0100f1d071
commit
e042aa1bbe
1 changed files with 30 additions and 18 deletions
|
@ -1,32 +1,44 @@
|
|||
import re
|
||||
from util import hook, http
|
||||
import json
|
||||
from BeautifulSoup import BeautifulSoup
|
||||
import urllib2
|
||||
|
||||
newgrounds_re = (r'(.*:)//(www.newgrounds.com|newgrounds.com)(:[0-9]+)?(.*)', re.I)
|
||||
valid = set('0123456789')
|
||||
|
||||
|
||||
def test(s):
|
||||
return set(s) <= valid
|
||||
return set(s) <= valid
|
||||
|
||||
|
||||
@hook.regex(*newgrounds_re)
|
||||
def newgrounds_url(match):
|
||||
location = match.group(4).split("/")[-1]
|
||||
if not test(location):
|
||||
return "Not a valid Newgrounds portal ID. Example: http://www.newgrounds.com/portal/view/593993"
|
||||
print "Not a valid Newgrounds portal ID. Example: http://www.newgrounds.com/portal/view/593993"
|
||||
return None
|
||||
soup = http.get_soup("http://www.newgrounds.com/portal/view/" + location)
|
||||
title = "\x02{}\x02".format(soup.find('title').text)
|
||||
try:
|
||||
urlobj = urllib2.urlopen("http://www.newgrounds.com/portal/view/" + location)
|
||||
except urllib2.HTTPError:
|
||||
return "\x034\x02Invalid response. Maybe Newgrounds is down for maintenance?"
|
||||
soup = BeautifulSoup(urlobj.read())
|
||||
author = " - \x02{}\x02".format(soup.find('ul', {'class': 'authorlinks'}).find('img')['alt'])
|
||||
except:
|
||||
author = ""
|
||||
|
||||
try:
|
||||
title = soup.find('title').text
|
||||
author = soup.find('ul', {'class': 'authorlinks'}).find('img')['alt']
|
||||
rating = u"\x02%s\x02/\x025.0\x02" % soup.find('dd', {'class': 'star-variable'})['title'].split("Stars –")[0].strip()
|
||||
numofratings = soup.find('dd', {'class': 'star-variable'})['title'].split("Stars –")[1].replace("Votes", "").strip()
|
||||
views = soup.find('dl', {'class': 'contentdata'}).findAll('dd')[1].find('strong').text
|
||||
date = soup.find('dl', {'class': 'sidestats'}).find('dd').text
|
||||
except Exception:
|
||||
return "\x034\x02Could not find item information."
|
||||
return u"\x02%s\x02 - rated %s (%s) - \x02%s\x02 views - \x02%s\x02 on \x02%s\x02" % (title, rating, numofratings, views, author, date)
|
||||
rating = u" - rated \x02%s\x02/\x025.0\x02" % soup.find('dd', {'class': 'star-variable'})['title'].split("Stars –")[0].strip()
|
||||
except:
|
||||
rating = ""
|
||||
|
||||
try:
|
||||
numofratings = " ({})".format(soup.find('dd', {'class': 'star-variable'})['title'].split("Stars –")[1].replace("Votes", "").strip())
|
||||
except:
|
||||
numofratings = ""
|
||||
|
||||
try:
|
||||
views = " - \x02{}\x02 views".format(soup.find('dl', {'class': 'contentdata'}).findAll('dd')[1].find('strong').text)
|
||||
except:
|
||||
views = ""
|
||||
|
||||
try:
|
||||
date = "on \x02{}\x02".format(soup.find('dl', {'class': 'sidestats'}).find('dd').text)
|
||||
except:
|
||||
date = ""
|
||||
return title + rating + numofratings + views + author + date
|
||||
|
|
Reference in a new issue