Youtube: more flexible url matching

Youtube: support for youtu.be and yooouuutuuube urls
This commit is contained in:
melonhead 2010-03-05 03:04:25 +00:00
parent c396be96d2
commit ea70af8ef1
2 changed files with 16 additions and 4 deletions

View File

@ -8,6 +8,9 @@ url_re = re.compile(r'([a-zA-Z]+://|www\.)[^ ]*')
expiration_period = 60 * 60 * 24 # 1 day expiration_period = 60 * 60 * 24 # 1 day
rate_limit_period = 60 * 3 # 3 minutes
rate_limit_count = 3
ignored_urls = [urlnorm.normalize("http://google.com")] ignored_urls = [urlnorm.normalize("http://google.com")]
@ -27,13 +30,21 @@ def insert_history(db, chan, url, nick):
db.commit() db.commit()
def get_history(db, chan, url): def get_history(db, chan, url, duration):
db.execute("delete from urlhistory where time < ?", db.execute("delete from urlhistory where time < ?",
(time.time() - expiration_period,)) (time.time() - duration,))
return db.execute("select nick, time from urlhistory where " return db.execute("select nick, time from urlhistory where "
"chan=? and url=? order by time desc", (chan, url)).fetchall() "chan=? and url=? order by time desc", (chan, url)).fetchall()
def get_history(db, chan, url):
return get_history(db, chan, url, expiration_period)
def get_recent_links_count(db, chan, url):
return len(get_history(db, chan, url, rate_limit_period))
def nicklist(nicks): def nicklist(nicks):
nicks = sorted(dict(nicks), key=unicode.lower) nicks = sorted(dict(nicks), key=unicode.lower)
if len(nicks) <= 2: if len(nicks) <= 2:
@ -78,6 +89,7 @@ def urlinput(inp, nick='', chan='', server='', reply=None, bot=None):
url = urlnorm.normalize(m.group(0)) url = urlnorm.normalize(m.group(0))
if url not in ignored_urls: if url not in ignored_urls:
history = get_history(db, chan, url) history = get_history(db, chan, url)
recent_history = get_recent_links_count(db, chan, url)
insert_history(db, chan, url, nick) insert_history(db, chan, url, nick)
if nick not in dict(history): if nick not in dict(history) and recent_history < rate_limit_count:
return format_reply(history) return format_reply(history)

View File

@ -10,7 +10,7 @@ from urllib import quote_plus
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
youtube_re = re.compile(r'youtube.*?v=([-_a-z0-9]+)', flags=re.I) youtube_re = re.compile(r'(?:youtube.*?(?:v=|/v/)|youtu\.be/|yooouuutuuube.*?id=)([-_a-z0-9]+)', flags=re.I)
base_url = 'http://gdata.youtube.com/feeds/api/' base_url = 'http://gdata.youtube.com/feeds/api/'
url = base_url + 'videos/%s?v=2&alt=jsonc' url = base_url + 'videos/%s?v=2&alt=jsonc'