Fixed formatting.

This commit is contained in:
Fletcher Boyd 2013-09-04 18:30:04 +08:00
parent 146ae3c279
commit 4069dd21a3
89 changed files with 615 additions and 496 deletions

View file

@ -9,6 +9,7 @@ control = {'bold': '\x02', 'color': '\x03', 'italic': '\x09',
'strikethrough': '\x13', 'reset': '\x0f', 'underline': '\x15',
'underline2': '\x1f', 'reverse': '\x16'}
def color(color):
return control['color'] + colors[color]

View file

@ -22,14 +22,14 @@ def _hook_add(func, add, name=''):
n_args -= 1
if n_args != 1:
err = '%ss must take 1 non-keyword argument (%s)' % (name,
func.__name__)
func.__name__)
raise ValueError(err)
args = []
if argspec.defaults:
end = bool(argspec.keywords) + bool(argspec.varargs)
args.extend(argspec.args[-len(argspec.defaults):
end if end else None])
end if end else None])
if argspec.keywords:
args.append(0) # means kwargs present
func._args = args
@ -41,7 +41,7 @@ def _hook_add(func, add, name=''):
def sieve(func):
if func.func_code.co_argcount != 5:
raise ValueError(
'sieves must take 5 arguments: (bot, input, func, type, args)')
'sieves must take 5 arguments: (bot, input, func, type, args)')
_hook_add(func, ['sieve', (func,)])
return func

View file

@ -22,7 +22,7 @@ ua_cloudbot = 'Cloudbot/DEV http://github.com/CloudDev/CloudBot'
ua_firefox = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0' \
' Firefox/17.0'
ua_old_firefox = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; ' \
'rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'
'rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6'
ua_internetexplorer = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
ua_chrome = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, ' \
'like Gecko) Chrome/22.0.1229.79 Safari/537.4'
@ -33,7 +33,7 @@ jar = cookielib.CookieJar()
class HTMLTextExtractor(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.result = [ ]
self.result = []
def handle_data(self, d):
self.result.append(d)
@ -76,7 +76,6 @@ def get_json(*args, **kwargs):
def open(url, query_params=None, user_agent=None, post_data=None,
referer=None, get_method=None, cookies=False, **kwargs):
if query_params is None:
query_params = {}
@ -112,7 +111,7 @@ def prepare_url(url, queries):
query = dict(urlparse.parse_qsl(query))
query.update(queries)
query = urllib.urlencode(dict((to_utf8(key), to_utf8(value))
for key, value in query.iteritems()))
for key, value in query.iteritems()))
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))

View file

@ -8,7 +8,7 @@ import re
def munge(text, munge_count=0):
"munges up text."
"""munges up text."""
reps = 0
for n in xrange(len(text)):
rep = character_replacements.get(text[n])
@ -96,7 +96,7 @@ def multiword_replace(text, wordDic):
def truncate_words(content, length=10, suffix='...'):
"Truncates a string after a certain number of words."
"""Truncates a string after a certain number of words."""
nmsg = content.split(" ")
out = None
x = 0
@ -106,7 +106,7 @@ def truncate_words(content, length=10, suffix='...'):
out = out + " " + nmsg[x]
else:
out = nmsg[x]
x = x + 1
x += 1
if x <= length:
return out
else:
@ -115,7 +115,7 @@ def truncate_words(content, length=10, suffix='...'):
# from <http://stackoverflow.com/questions/250357/smart-truncate-in-python>
def truncate_str(content, length=100, suffix='...'):
"Truncates a string after a certain number of chars."
"""Truncates a string after a certain number of chars."""
if len(content) <= length:
return content
else:
@ -197,5 +197,5 @@ def get_text_list(list_, last_word='or'):
return list_[0]
return '%s %s %s' % (
# Translators: This string is used as a separator between list elements
(', ').join([i for i in list_][:-1]),
', '.join([i for i in list_][:-1]),
last_word, list_[-1])

View file

@ -43,12 +43,12 @@ def timesince(d, now=None):
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
"""
chunks = (
(60 * 60 * 24 * 365, ('year', 'years')),
(60 * 60 * 24 * 30, ('month', 'months')),
(60 * 60 * 24 * 7, ('week', 'weeks')),
(60 * 60 * 24, ('day', 'days')),
(60 * 60, ('hour', 'hours')),
(60, ('minute', 'minutes'))
(60 * 60 * 24 * 365, ('year', 'years')),
(60 * 60 * 24 * 30, ('month', 'months')),
(60 * 60 * 24 * 7, ('week', 'weeks')),
(60 * 60 * 24, ('day', 'days')),
(60 * 60, ('hour', 'hours')),
(60, ('minute', 'minutes'))
)
# Convert int or float (unix epoch) to datetime.datetime for comparison

View file

@ -38,13 +38,15 @@ class Normalizer(object):
self.regex = regex
self.normalize = normalize_func
normalizers = ( Normalizer( re.compile(r'(?:https?://)?(?:[a-zA-Z0-9\-]+\.)?(?:amazon|amzn){1}\.(?P<tld>[a-zA-Z\.]{2,})\/(gp/(?:product|offer-listing|customer-media/product-gallery)/|exec/obidos/tg/detail/-/|o/ASIN/|dp/|(?:[A-Za-z0-9\-]+)/dp/)?(?P<ASIN>[0-9A-Za-z]{10})'),
lambda m: r'http://amazon.%s/dp/%s' % (m.group('tld'), m.group('ASIN'))),
Normalizer( re.compile(r'.*waffleimages\.com.*/([0-9a-fA-F]{40})'),
lambda m: r'http://img.waffleimages.com/%s' % m.group(1) ),
Normalizer( re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-zA-Z0-9]+)'),
lambda m: r'http://youtube.com/watch?v=%s' % m.group(1) ),
)
normalizers = (Normalizer(re.compile(
r'(?:https?://)?(?:[a-zA-Z0-9\-]+\.)?(?:amazon|amzn){1}\.(?P<tld>[a-zA-Z\.]{2,})\/(gp/(?:product|offer-listing|customer-media/product-gallery)/|exec/obidos/tg/detail/-/|o/ASIN/|dp/|(?:[A-Za-z0-9\-]+)/dp/)?(?P<ASIN>[0-9A-Za-z]{10})'),
lambda m: r'http://amazon.%s/dp/%s' % (m.group('tld'), m.group('ASIN'))),
Normalizer(re.compile(r'.*waffleimages\.com.*/([0-9a-fA-F]{40})'),
lambda m: r'http://img.waffleimages.com/%s' % m.group(1)),
Normalizer(re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-zA-Z0-9]+)'),
lambda m: r'http://youtube.com/watch?v=%s' % m.group(1)),
)
def normalize(url, assume_scheme=False):
@ -78,12 +80,13 @@ def normalize(url, assume_scheme=False):
def clean(string):
string = unicode(unquote(string), 'utf-8', 'replace')
return unicodedata.normalize('NFC', string).encode('utf-8')
path = quote(clean(path), "~:/?#[]@!$&'()*+,;=")
fragment = quote(clean(fragment), "~")
# note care must be taken to only encode & and = characters as values
query = "&".join(["=".join([quote(clean(t), "~:/?#[]@!$'()*+,;=")
for t in q.split("=", 1)]) for q in query.split("&")])
for t in q.split("=", 1)]) for q in query.split("&")])
# Prevent dot-segments appearing in non-relative URI paths.
if scheme in ["", "http", "https", "ftp", "file"]:
@ -128,7 +131,7 @@ def normalize(url, assume_scheme=False):
if url.endswith("#") and query == "" and fragment == "":
path += "#"
normal_url = urlparse.urlunsplit((scheme, auth, path, query,
fragment)).replace("http:///", "http://")
fragment)).replace("http:///", "http://")
for norm in normalizers:
m = norm.regex.match(normal_url)
if m:

View file

@ -1,7 +1,9 @@
""" web.py - handy functions for web services """
import http, urlnorm
import json, urllib
import http
import urlnorm
import json
import urllib
import yql
short_url = "http://is.gd/create.php"
@ -39,11 +41,12 @@ def try_isgd(url):
out = url
return out
def haste(text, ext='txt'):
""" pastes text to a hastebin server """
page = http.get(paste_url + "/documents", post_data=text)
data = json.loads(page)
return("%s/%s.%s" % (paste_url, data['key'], ext))
return ("%s/%s.%s" % (paste_url, data['key'], ext))
def query(query, params={}):