Blocklist Importer - Check For Updates

Suggest, post, or discuss plugins for Deluge
Post Reply
johnnyg
Top Bloke
Top Bloke
Posts: 1522
Joined: Sun Oct 28, 2007 4:00 am
Location: Sydney, Australia

Blocklist Importer - Check For Updates

Post by johnnyg »

I use the blocklist importer plugin a lot and unfortunately the bluetack site (which hosts the emule & safepeer lists) tends to go down & redirects you to an error page. Currently it will still import the error page but it'll detect that it's the wrong format (& won't crash).

However this annoyed me seeing my perfectly good blocklist would get overwritten with an invalid one.

So I've written a patch for the blocklist importer which I thought I would share which only downloads the blocklist if the url is correct (i.e. offering the right type of file) and the remote blocklist is actually newer than the one you have (based on last-modified times and lengths) :D

I should mention that I'm quite new to python (but not to programming) and I've only really tested it out with bluetack's nipfitler.dat.gz & splist.zip URLs.

EDIT: updated for deluge 0.5.9.3

__init__.py patch

Code: Select all

37c37,38
< import os.path, os, time
---
> from socket import setdefaulttimeout, getdefaulttimeout
> import os, datetime, re
78a80,133
>     def validurl(self, header):
>         valid = False
>         listtype = self.config.get('listtype')
>         if listtype == "spzip":
>             type = "application/zip"
>         elif listtype == "gzmule" or listtype == "p2bgz":
>             type = "application/x-gzip"
>         if header['content-type']  == type:
>             valid = True
>         return valid
> 
>     def outdated(self):
>         # only perform check if it's a url
>         if re.match("^http\:\/\/",self.config.get('url'),re.I):
>             print "Checking for updated blocklist..."
>             # initialise variables
>             outdated = False
>             local = {}
>             remote = {}
>             timeout = getdefaulttimeout()
>             # allow 5s to connect to server
>             setdefaulttimeout(5)
>             try:
>                 # collect remote blocklist info
>                 header = urllib.urlopen(self.config.get('url')).info()
>                 remote['size'] = int(header['content-length'])
>                 remote['time'] = datetime.datetime.strptime(header['last-modified'],"%a, %d %b %Y %H:%M:%S GMT")
>             except IOError:
>                 print "...Remote blocklist timed out"
>             else:
>                 if self.validurl(header):
>                     try:
>                         # collect local blocklist info
>                         local['stat'] = os.stat(self.blockfile)
>                         local['size'] = local['stat'].st_size
>                         local['time'] = datetime.datetime.fromtimestamp(local['stat'].st_mtime)
>                     except OSError:
>                         print "...Could not open local blocklist"
>                         outdated = True
>                     else:
>                         if local['size'] != remote['size'] or local['time'] < remote['time']:
>                             print "...Blocklist is NOT up to date"
>                             outdated = True
>                         else:
>                             print "...Blocklist is up to date"
>                 else:
>                     print "...Remote blocklist invalid/unavailable"
>                     raise IOError, "remote blocklist invalid or unavailable"
>             finally:
>                 setdefaulttimeout(timeout)
>         else:
>             outdated = True
>         return outdated
> 
92,93c147,149
<             list_timestamp = liststats.st_mtime
<             now_timestamp = time.time()
---
>             list_time = datetime.datetime.fromtimestamp(liststats.st_mtime)
>             list_size = liststats.st_size
>             now_time = datetime.datetime.today()
98d153
<             # Seconds in a day = 86400
101c156
<             if now_timestamp >= (list_timestamp + (86400 * days_update)):
---
>             if now_time >= (list_time + datetime.timedelta(days=days_update)):
110c165
<         if fetch == -1:
---
>         if fetch == -1 and self.outdated():
ui.py patch (optional)

Code: Select all

52c52
<         label2.set_markup('<b>' + _("Download new blocklist every") + '</b>')
---
>         label2.set_markup('<b>' + _("Check for new blocklist every") + '</b>')
markybob
Compulsive Poster
Compulsive Poster
Posts: 1230
Joined: Thu May 24, 2007 11:27 pm
Location: Chicago, IL, USA
Contact:

Re: Blocklist Importer - Check For Updates

Post by markybob »

johnnyg wrote:I use the blocklist importer plugin a lot and unfortunately the bluetack site (which hosts the emule & safepeer lists) tends to go down & redirects you to an error page. Currently it will still import the error page but it'll detect that it's the wrong format (& won't crash).

However this annoyed me seeing my perfectly good blocklist would get overwritten with an invalid one.

So I've written a patch for the blocklist importer which I thought I would share which only downloads the blocklist if the url is correct (i.e. offering the right type of file) and the remote blocklist is actually newer than the one you have (based on last-modified times and lengths) :D

I should mention that I'm quite new to python (but not to programming) and I've only really tested it out with bluetack's nipfitler.dat.gz & splist.zip URLs.

__init__.py patch

Code: Select all

33a34
> from socket import setdefaulttimeout, getdefaulttimeout
37c38
< import os.path
---
> import os.path, time, re
73a75,127
>     def validurl(self, header):
>         valid = False
>         listtype = self.config.get('listtype')
>         if listtype == "spzip":
>             type = "application/zip"
>         elif listtype == "gzmule" or listtype == "p2bgz":
>             type = "application/x-gzip"
>         if header['content-type']  == type:
>             valid = True
>         return valid
> 
>     def outdated(self):
>         # only perform check if it's a url
>         if re.match("^http\:\/\/",self.config.get('url'),re.I):
>             print "Checking for updated blocklist..."
>             # initialise variables
>             outdated = False
>             local = {}
>             remote = {}
>             timeout = getdefaulttimeout()
>             # allow 5s to connect to server
>             setdefaulttimeout(5)
>             try:
>                 # collect remote blocklist info
>                 header = urllib.urlopen(self.config.get('url')).info()
>                 remote['size'] = int(header['content-length'])
>                 remote['time'] = time.strptime(header['last-modified'],"%a, %d %b %Y %H:%M:%S GMT")
>             except:
>                 print "Remote blocklist timed out or unavailable"
>             else:
>                 if self.validurl(header):
>                     try:
>                         # collect local blocklist info
>                         local['size'] = int(os.path.getsize(self.blockfile))
>                         local['time'] = time.gmtime(os.path.getmtime(self.blockfile))
>                     except OSError:
>                         print "Could not open local blocklist"
>                         outdated = True
>                     else:
>                         if local['size'] < remote['size'] or local['time'] < remote['time']:
>                             print "Blocklist is NOT up to date"
>                             outdated = True
>                         else:
>                             print "Blocklist is up to date"
>                 else:
>                     print "...Remote blocklist invalid/unavailable"
>                     raise IOError, "remote blocklist invalid or unavailable"
>             finally:
>                 setdefaulttimeout(timeout)
>         else:
>             outdated = True
>         return outdated
> 
86,87d139
<             print "Fetching",self.config.get('url')
<             self.gtkprog.start_download()
89c141,144
<                 filename, headers = urllib.urlretrieve(self.config.get('url'),
---
>                 if self.outdated():
>                     print "Fetching",self.config.get('url')
>                     self.gtkprog.start_download()
>                     filename, headers = urllib.urlretrieve(self.config.get('url'),
94,95c149,150
<                 self.gtkprog.stop()
<                 return
---
>                 #self.gtkprog.stop()
>                 #return
ui.py patch (optional)

Code: Select all

50c50
<         self.load_on_start = gtk.CheckButton(_("Download on start"))
---
>         self.load_on_start = gtk.CheckButton(_("Check for updates on startup (URLs only)"))
nice, thanks :)
Renfro
Member
Member
Posts: 11
Joined: Fri Jan 25, 2008 10:51 pm

Re: Blocklist Importer - Check For Updates

Post by Renfro »

However this annoyed me seeing my perfectly good blocklist would get overwritten with an invalid one.
Arrgghh .... that keeps happening to me to! and I agree with you, it really is annoying.

Thanks for creating a patch to resolve this.
johnnyg
Top Bloke
Top Bloke
Posts: 1522
Joined: Sun Oct 28, 2007 4:00 am
Location: Sydney, Australia

Re: Blocklist Importer - Check For Updates

Post by johnnyg »

Renfro wrote:
However this annoyed me seeing my perfectly good blocklist would get overwritten with an invalid one.
Arrgghh .... that keeps happening to me to! and I agree with you, it really is annoying.

Thanks for creating a patch to resolve this.
no worries, glad someone else is finding it useful. :D
I've updated it so that it now works with latest deluge (0.5.9.3)
Post Reply