mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-23 02:24:12 +01:00
Fixed bugz0rs in URL, added vacuum (called on stats).
This commit is contained in:
parent
7f112672de
commit
565c6b7347
@ -43,9 +43,11 @@ import os
|
|||||||
import re
|
import re
|
||||||
import sets
|
import sets
|
||||||
import time
|
import time
|
||||||
|
import shutil
|
||||||
import getopt
|
import getopt
|
||||||
import urllib2
|
import urllib2
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import itertools
|
||||||
|
|
||||||
import supybot.conf as conf
|
import supybot.conf as conf
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
@ -94,44 +96,81 @@ class URLDB(object):
|
|||||||
dataDir = conf.supybot.directories.data()
|
dataDir = conf.supybot.directories.data()
|
||||||
self.filename = os.path.join(dataDir, '%s-URL.db' % channel)
|
self.filename = os.path.join(dataDir, '%s-URL.db' % channel)
|
||||||
|
|
||||||
def addUrl(self, url, nick):
|
def _getFile(self):
|
||||||
fd = file(self.filename, 'a')
|
|
||||||
fd.write('%s %s\n' % (url, nick))
|
|
||||||
fd.close()
|
|
||||||
|
|
||||||
def numUrls(self):
|
|
||||||
try:
|
try:
|
||||||
fd = file(self.filename)
|
fd = file(self.filename)
|
||||||
|
return fd
|
||||||
except EnvironmentError, e:
|
except EnvironmentError, e:
|
||||||
self.log.warning('Couldn\'t open %s: %s',
|
self.log.warning('Couldn\'t open %s: %s',
|
||||||
self.filename, utils.exnToString(e))
|
self.filename, utils.exnToString(e))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _formatRecord(self, url, nick):
|
||||||
|
return '%s %s\n' % (url, nick)
|
||||||
|
|
||||||
|
def addUrl(self, url, nick):
|
||||||
|
fd = file(self.filename, 'a')
|
||||||
|
fd.write(self._formatRecord(url, nick))
|
||||||
|
fd.close()
|
||||||
|
|
||||||
|
def numUrls(self):
|
||||||
|
fd = self._getFile()
|
||||||
|
if fd is None:
|
||||||
return 0
|
return 0
|
||||||
try:
|
try:
|
||||||
return itertools.ilen(fd)
|
return itertools.ilen(fd)
|
||||||
finally:
|
finally:
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
def getUrls(self, p):
|
def getUrlsAndNicks(self, p=None):
|
||||||
L = []
|
L = []
|
||||||
try:
|
fd = self._getFile()
|
||||||
fd = file(self.filename)
|
if fd is None:
|
||||||
except EnvironmentError, e:
|
|
||||||
self.log.warning('Couldn\'t open %s: %s',
|
|
||||||
self.filename, utils.exnToString(e))
|
|
||||||
return []
|
return []
|
||||||
try:
|
try:
|
||||||
urls = sets.Set()
|
|
||||||
for line in fd:
|
for line in fd:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
(url, nick) = line.split()
|
(url, nick) = line.split()
|
||||||
if url not in urls and p(url, nick):
|
if p(url, nick):
|
||||||
urls.add(url)
|
L.append((url, nick))
|
||||||
L.append(url)
|
seen = sets.Set()
|
||||||
L.reverse()
|
L.reverse()
|
||||||
|
for (i, (url, nick)) in enumerate(L):
|
||||||
|
if url in seen:
|
||||||
|
L[i] = None
|
||||||
|
else:
|
||||||
|
seen.add(url)
|
||||||
|
L = filter(None, L)
|
||||||
return L
|
return L
|
||||||
finally:
|
finally:
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
|
def getUrls(self, p):
|
||||||
|
return [url for (url, nick) in self.getUrlsAndNicks(p)]
|
||||||
|
|
||||||
|
def vacuum(self):
|
||||||
|
filename = utils.mktemp()
|
||||||
|
out = file(filename, 'w')
|
||||||
|
notAdded = 0
|
||||||
|
urls = self.getUrlsAndNicks(lambda *args: True)
|
||||||
|
urls.reverse()
|
||||||
|
seen = sets.Set()
|
||||||
|
for (i, (url, nick)) in enumerate(urls):
|
||||||
|
if url not in seen:
|
||||||
|
seen.add(url)
|
||||||
|
else:
|
||||||
|
urls[i] = None
|
||||||
|
notAdded += 1
|
||||||
|
for urlNick in urls:
|
||||||
|
if urlNick is not None:
|
||||||
|
out.write(self._formatRecord(*urlNick))
|
||||||
|
out.close()
|
||||||
|
shutil.move(filename, self.filename)
|
||||||
|
self.log.info('Vacuumed %s, removed %s records.',
|
||||||
|
self.filename, notAdded)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class URL(callbacks.PrivmsgCommandAndRegexp):
|
class URL(callbacks.PrivmsgCommandAndRegexp):
|
||||||
regexps = ['tinyurlSnarfer', 'titleSnarfer']
|
regexps = ['tinyurlSnarfer', 'titleSnarfer']
|
||||||
_titleRe = re.compile('<title>(.*?)</title>', re.I | re.S)
|
_titleRe = re.compile('<title>(.*?)</title>', re.I | re.S)
|
||||||
@ -254,6 +293,7 @@ class URL(callbacks.PrivmsgCommandAndRegexp):
|
|||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
|
db.vacuum()
|
||||||
count = db.numUrls()
|
count = db.numUrls()
|
||||||
irc.reply('I have %s in my database.' % utils.nItems('URL', count))
|
irc.reply('I have %s in my database.' % utils.nItems('URL', count))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user