From e042aa1bbef29b7dc00cb44b7864d15ba2c9e732 Mon Sep 17 00:00:00 2001 From: Steven Smith Date: Wed, 31 Jul 2013 22:42:59 +0800 Subject: [PATCH] Better formatting, 4-space indents, use http.get_soup instead of BeautifulSoup directly --- plugins/newgrounds.py | 48 +++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/plugins/newgrounds.py b/plugins/newgrounds.py index 37557b3..62c5d31 100644 --- a/plugins/newgrounds.py +++ b/plugins/newgrounds.py @@ -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