mirror of
https://github.com/progval/Limnoria.git
synced 2025-04-29 06:51:08 -05:00
All tinyurl related commands now query the local database to see if a tinyurl
already exists for the url that is being snarfed/passed as an argument. If not, tinyurl.com is used to generate the tinyurl
This commit is contained in:
parent
2909c4a8c6
commit
f6e848511b
100
plugins/URL.py
100
plugins/URL.py
@ -151,48 +151,60 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
|
|||||||
url = match.group(0)
|
url = match.group(0)
|
||||||
if self.configurables.get('tinyurl-snarfer', channel):
|
if self.configurables.get('tinyurl-snarfer', channel):
|
||||||
minlen = self.configurables.get('tinyurl-minimum-length', channel)
|
minlen = self.configurables.get('tinyurl-minimum-length', channel)
|
||||||
if len(url) > minlen:
|
if len(url) >= minlen:
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute("""SELECT tinyurls.tinyurl FROM urls, tinyurls
|
(tinyurl, updateDb) = self._getTinyUrl(url, channel)
|
||||||
WHERE urls.url=%s AND
|
if tinyurl is None:
|
||||||
urls.id=tinyurls.url_id""", url)
|
debug.msg('tinyurl was None for url %r' % url)
|
||||||
if cursor.rowcount == 0:
|
return
|
||||||
|
elif updateDb:
|
||||||
#debug.printf(url)
|
#debug.printf(url)
|
||||||
tinyurl = self._getTinyUrl(url)
|
self._updateTinyDb(url, tinyurl, channel)
|
||||||
cursor.execute("""INSERT INTO tinyurls
|
s = '%s (was <%s>)' % (ircutils.bold(tinyurl), url)
|
||||||
VALUES (NULL, 0, %s)""", tinyurl)
|
irc.reply(msg, s, prefixName=False)
|
||||||
cursor.execute("""SELECT id FROM urls WHERE url=%s""", url)
|
|
||||||
id = cursor.fetchone()[0]
|
|
||||||
cursor.execute("""UPDATE tinyurls SET url_id=%s
|
|
||||||
WHERE tinyurl=%s""", id,tinyurl)
|
|
||||||
db.commit()
|
|
||||||
else:
|
|
||||||
tinyurl = cursor.fetchone()[0]
|
|
||||||
if tinyurl is not None:
|
|
||||||
s = '%s (was <%s>)' % (ircutils.bold(tinyurl), url)
|
|
||||||
irc.reply(msg, s, prefixName=False)
|
|
||||||
else:
|
|
||||||
debug.msg('tinyurl was none for url %r' % url)
|
|
||||||
tinyurlSnarfer = privmsgs.urlSnarfer(tinyurlSnarfer)
|
tinyurlSnarfer = privmsgs.urlSnarfer(tinyurlSnarfer)
|
||||||
|
|
||||||
|
def _updateTinyDb(self, url, tinyurl, channel):
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""INSERT INTO tinyurls
|
||||||
|
VALUES (NULL, 0, %s)""", tinyurl)
|
||||||
|
cursor.execute("""SELECT id FROM urls WHERE url=%s""", url)
|
||||||
|
id = cursor.fetchone()[0]
|
||||||
|
cursor.execute("""UPDATE tinyurls SET url_id=%s
|
||||||
|
WHERE tinyurl=%s""", id, tinyurl)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
_tinyRe = re.compile(r'(http://tinyurl\.com/\w+)</blockquote>')
|
_tinyRe = re.compile(r'(http://tinyurl\.com/\w+)</blockquote>')
|
||||||
def _getTinyUrl(self, url, cmd=False):
|
def _getTinyUrl(self, url, channel, cmd=False):
|
||||||
try:
|
db = self.getDb(channel)
|
||||||
#debug.printf('Trying to get tinyurl for %r' % url)
|
cursor = db.cursor()
|
||||||
fd = urllib2.urlopen('http://tinyurl.com/create.php?url=%s' % url)
|
cursor.execute("""SELECT tinyurls.tinyurl FROM urls, tinyurls
|
||||||
s = fd.read()
|
WHERE urls.url=%s AND
|
||||||
fd.close()
|
tinyurls.url_id=urls.id""", url)
|
||||||
m = self._tinyRe.search(s)
|
if cursor.rowcount == 0:
|
||||||
if m is None:
|
updateDb = True
|
||||||
return None
|
try:
|
||||||
return m.group(1)
|
#debug.printf('Trying to get tinyurl for %r' % url)
|
||||||
except urllib2.HTTPError, e:
|
fd = urllib2.urlopen('http://tinyurl.com/create.php?url=%s' %
|
||||||
if cmd:
|
url)
|
||||||
raise callbacks.Error, e.msg()
|
s = fd.read()
|
||||||
else:
|
fd.close()
|
||||||
debug.msg(e.msg())
|
m = self._tinyRe.search(s)
|
||||||
|
if m is None:
|
||||||
|
tinyurl = None
|
||||||
|
else:
|
||||||
|
tinyurl = m.group(1)
|
||||||
|
except urllib2.HTTPError, e:
|
||||||
|
if cmd:
|
||||||
|
raise callbacks.Error, e.msg()
|
||||||
|
else:
|
||||||
|
debug.msg(e.msg())
|
||||||
|
else:
|
||||||
|
updateDb = False
|
||||||
|
tinyurl = cursor.fetchone()[0]
|
||||||
|
return (tinyurl, updateDb)
|
||||||
|
|
||||||
def _formatUrl(self, url, added, addedBy):
|
def _formatUrl(self, url, added, addedBy):
|
||||||
#debug.printf((url, added, addedBy))
|
#debug.printf((url, added, addedBy))
|
||||||
@ -224,11 +236,21 @@ class URL(callbacks.PrivmsgCommandAndRegexp,
|
|||||||
Returns a TinyURL.com version of <url>
|
Returns a TinyURL.com version of <url>
|
||||||
"""
|
"""
|
||||||
url = privmsgs.getArgs(args)
|
url = privmsgs.getArgs(args)
|
||||||
if self.configurables.get('tinyurl-snarfer', channel=msg.args[0]):
|
if len(url) < 23:
|
||||||
|
irc.error(msg,
|
||||||
|
'Stop being a lazy-biotch and type the URL yourself.')
|
||||||
return
|
return
|
||||||
url = self._getTinyUrl(url, cmd=True)
|
channel = msg.args[0]
|
||||||
if url:
|
snarf = self.configurables.get('tinyurl-snarfer', channel=msg.args[0])
|
||||||
irc.reply(msg, url)
|
minlen = self.configurables.get('tinyurl-minimum-length',
|
||||||
|
channel=channel)
|
||||||
|
if snarf and len(url) >= minlen:
|
||||||
|
return
|
||||||
|
(tinyurl, updateDb) = self._getTinyUrl(url, channel, cmd=True)
|
||||||
|
if tinyurl:
|
||||||
|
if updateDb:
|
||||||
|
self._updateTinyDb(url, tinyurl, channel)
|
||||||
|
irc.reply(msg, tinyurl)
|
||||||
else:
|
else:
|
||||||
s = 'Could not parse the TinyURL.com results page. (%s)' % \
|
s = 'Could not parse the TinyURL.com results page. (%s)' % \
|
||||||
conf.replyPossibleBug
|
conf.replyPossibleBug
|
||||||
|
Loading…
x
Reference in New Issue
Block a user