mirror of
https://github.com/codex-storage/deluge.git
synced 2025-02-17 13:56:47 +00:00
remove plugin preference page properly, handle downloads/timeouts better, fixed inf download loop
This commit is contained in:
parent
174ea3c35e
commit
87e7fd5869
@ -33,14 +33,12 @@
|
||||
# this exception statement from your version. If you delete this exception
|
||||
# statement from all source files in the program, then also delete it here.
|
||||
|
||||
import deluge.component
|
||||
from torrentblocklist import TorrentBlockList
|
||||
from deluge.log import LOG as log
|
||||
from deluge.plugins.corepluginbase import CorePluginBase
|
||||
|
||||
class Core(CorePluginBase):
|
||||
def enable(self):
|
||||
deluge.component.get("Core").session.set_max_connections(0)
|
||||
self.blocklist = TorrentBlockList(self.plugin)
|
||||
self.plugin.register_hook("post_session_load", self._post_session_load)
|
||||
log.debug('Blocklist: Plugin enabled..')
|
||||
|
@ -71,6 +71,7 @@ class GtkUI(ui.UI):
|
||||
def disable(self):
|
||||
deluge.component.get("StatusBar").remove_item(self.blocklist_status)
|
||||
self.plugin.deregister_hook("on_apply_prefs", self.apply_prefs)
|
||||
self.plugin.remove_preferences_page("Blocklist")
|
||||
|
||||
def get_pixmap(self, fname):
|
||||
"""Returns a pixmap file included with plugin"""
|
||||
|
@ -42,8 +42,9 @@
|
||||
import urllib2, httplib, socket, os, datetime, sys
|
||||
import deluge.common
|
||||
import deluge.component # for libtorrent session reference to change connection number
|
||||
import deluge.configmanager # used to retrieve max_global_connections
|
||||
from deluge.log import LOG as log
|
||||
import ui # added by Mark for pausing torrents
|
||||
#import ui # added by Mark for pausing torrents
|
||||
from peerguardian import PGReader, PGException
|
||||
from text import TextReader, GZMuleReader, PGZip
|
||||
|
||||
@ -76,8 +77,7 @@ class HTTPConnection(httplib.HTTPConnection):
|
||||
self.timeout = timeout
|
||||
|
||||
def connect(self):
|
||||
"""Override HTTPConnection.connect to connect to
|
||||
host/port specified in __init__."""
|
||||
"""Override HTTPConnection.connect to connect to host/port specified in __init__."""
|
||||
|
||||
msg = "getaddrinfo returns an empty list"
|
||||
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
|
||||
@ -114,32 +114,28 @@ class HTTPHandler(urllib2.HTTPHandler):
|
||||
return self.do_open(makeConnection, req)
|
||||
|
||||
|
||||
class TorrentBlockList:
|
||||
def __init__(self, coreplugin):
|
||||
class TorrentBlockList:
|
||||
def __init__(self, coreplugin):
|
||||
self.plugin = coreplugin # reference from plugin core
|
||||
log.info('Blocklist: TorrentBlockList instantiated')
|
||||
self.config = deluge.configmanager.ConfigManager("blocklist.conf", BLOCKLIST_PREFS)
|
||||
self.curr = 0
|
||||
self.load_options()
|
||||
|
||||
|
||||
# Make sure we have a current block list file locally
|
||||
self.fetch = False
|
||||
self.local_blocklist = deluge.common.get_config_dir("blocklist.cache")
|
||||
self.local_blocklist = deluge.common.get_config_dir("blocklist.cache")
|
||||
|
||||
# Check list for modifications from online version
|
||||
self.check_update()
|
||||
|
||||
# Initialize download attempt
|
||||
self.attempt = 0
|
||||
|
||||
|
||||
if self.fetch == True:
|
||||
self.download()
|
||||
|
||||
|
||||
log.debug('Blocklist: TorrentBlockList Constructor finished')
|
||||
|
||||
|
||||
|
||||
|
||||
def load_options(self):
|
||||
#self.config.load()
|
||||
# Fill variables with settings from block list configuration file
|
||||
self.url = self.config['url']
|
||||
self.listtype = self.config['listtype']
|
||||
@ -147,12 +143,12 @@ class TorrentBlockList:
|
||||
self.load_on_start = self.config['load_on_start']
|
||||
self.timeout = self.config['timeout']
|
||||
self.try_times = self.config['try_times']
|
||||
|
||||
|
||||
self.old_url = self.url
|
||||
self.old_listtype = self.listtype
|
||||
|
||||
|
||||
socket.setdefaulttimeout(self.timeout)
|
||||
|
||||
|
||||
def set_options(self, options):
|
||||
log.info('Blocklist: Options saved...')
|
||||
self.config.set('url', options['url'])
|
||||
@ -165,26 +161,26 @@ class TorrentBlockList:
|
||||
self.config.save()
|
||||
# Load newly set options to core plugin
|
||||
self.load_options()
|
||||
|
||||
|
||||
def check_update(self, force_check=False):
|
||||
log.info('Blocklist: Checking for updates')
|
||||
|
||||
|
||||
try:
|
||||
# Check current block lists time stamp and decide if it needs to be replaced
|
||||
list_stats = os.stat(self.local_blocklist)
|
||||
list_time = datetime.datetime.fromtimestamp(list_stats.st_mtime)
|
||||
list_size = list_stats.st_size
|
||||
current_time = datetime.datetime.today()
|
||||
|
||||
|
||||
except:
|
||||
self.fetch = True
|
||||
return
|
||||
|
||||
|
||||
# If local blocklist file exists but nothing is in it
|
||||
if list_size == 0:
|
||||
self.fetch = True
|
||||
return
|
||||
|
||||
return
|
||||
|
||||
if current_time >= (list_time + datetime.timedelta(self.check_after_days)):
|
||||
check_newer = True
|
||||
log.debug('Blocklist: List may need to be replaced')
|
||||
@ -194,29 +190,36 @@ class TorrentBlockList:
|
||||
# If the program decides it is time to get a new list
|
||||
if check_newer == True or force_check == True:
|
||||
log.debug('Blocklist: Attempting check')
|
||||
|
||||
|
||||
j = 0 # counter for loop
|
||||
|
||||
|
||||
while j < self.try_times:
|
||||
# Get current online block lists time stamp and compare it with current
|
||||
try:
|
||||
http_handler = HTTPHandler(timeout = 15)
|
||||
try:
|
||||
http_handler = HTTPHandler(timeout = self.timeout)
|
||||
opener = urllib2.build_opener(http_handler)
|
||||
request = urllib2.Request(self.url)
|
||||
|
||||
|
||||
try:
|
||||
new_listinfo = opener.open(request) # Can Raise URLError
|
||||
header = new_listinfo.info()
|
||||
remote_size = int(header['content-length'])
|
||||
remote_time = datetime.datetime.strptime(header['last-modified'],"%a, %d %b %Y %H:%M:%S GMT")
|
||||
remote_type = header['content-type']
|
||||
|
||||
|
||||
except URLError, e:
|
||||
log.warning("Blocklist: Requesnt online blocklist info failed")
|
||||
j += 1
|
||||
break
|
||||
|
||||
except Exception, e:
|
||||
log.warning(e)
|
||||
# HANDLE EXCEPTOIN
|
||||
|
||||
j = self.try_times
|
||||
break
|
||||
|
||||
log.debug(self.listtype)
|
||||
|
||||
|
||||
# check expected list type
|
||||
if self.listtype == "spzip":
|
||||
list_type = "application/zip"
|
||||
@ -224,7 +227,7 @@ class TorrentBlockList:
|
||||
list_type = "application/x-gzip"
|
||||
else:
|
||||
list_type = "text/html"
|
||||
|
||||
|
||||
# Print remote file information and local
|
||||
log.debug('Blocklist: remote')
|
||||
log.debug(remote_type)
|
||||
@ -234,7 +237,7 @@ class TorrentBlockList:
|
||||
log.debug(list_type)
|
||||
log.debug(list_time)
|
||||
log.debug(list_size)
|
||||
|
||||
|
||||
# Compare MIME types of local and remote list
|
||||
if list_type == remote_type:
|
||||
log.info('Blocklist: Remote and Local have the same list type')
|
||||
@ -242,70 +245,79 @@ class TorrentBlockList:
|
||||
if list_time < remote_time or list_size != remote_size:
|
||||
self.fetch = True
|
||||
log.info('Blocklist: Local blocklist list is out of date')
|
||||
|
||||
|
||||
else:
|
||||
self.fetch = False
|
||||
log.info('Blocklist: Local block list is up to date')
|
||||
|
||||
|
||||
return
|
||||
|
||||
|
||||
j+=1
|
||||
log.debug('Blocklist: 6 TRY AGAIN FOO')
|
||||
|
||||
log.debug('Blocklist: TRY AGAIN FOO')
|
||||
|
||||
# Connection can't be made to check remote time stamps
|
||||
except: # && urllib2.URLError
|
||||
log.debug('Blocklist: Connection to remote server timed out')
|
||||
self.fetch = False
|
||||
j+=1
|
||||
|
||||
|
||||
else:
|
||||
log.info('Blocklist: Not enough time has passed to check for a new list')
|
||||
return
|
||||
|
||||
|
||||
def download(self):
|
||||
log.info('Blocklist: Beginning download')
|
||||
self.attempt += 1
|
||||
|
||||
|
||||
i = 0
|
||||
while i < self.try_times:
|
||||
success = False
|
||||
|
||||
while i < self.try_times or success == False:
|
||||
# Download a new block list
|
||||
try:
|
||||
log.info('Blocklist: Downloading new list...')
|
||||
http_handler = HTTPHandler(timeout = 15)
|
||||
http_handler = HTTPHandler(timeout = self.timeout)
|
||||
opener = urllib2.build_opener(http_handler)
|
||||
request = urllib2.Request(self.url)
|
||||
new_list = opener.open(request)
|
||||
new_list = opener.open(request)
|
||||
|
||||
file = open(deluge.common.get_config_dir("blocklist.cache"), 'w')
|
||||
log.info('Blocklist: Writing blocklist to disk')
|
||||
|
||||
# Write new blocklist to disk
|
||||
while 1:
|
||||
data = new_list.read()
|
||||
if not len(data):
|
||||
break
|
||||
file.write(data)
|
||||
file.close()
|
||||
|
||||
file.close()
|
||||
|
||||
except OSError, e:
|
||||
log.debug('Blocklist: Unable to write blocklist file')
|
||||
return
|
||||
|
||||
|
||||
except:
|
||||
if self.attempt > self.try_times:
|
||||
if i > self.try_times:
|
||||
log.warning('Blocklist: All download attempts failed')
|
||||
return
|
||||
|
||||
|
||||
else:
|
||||
log.warning('Blocklist: Try list download again')
|
||||
i += 1
|
||||
|
||||
# CHECKSUM
|
||||
|
||||
log.info('Blocklist: List downloaded sucessfully')
|
||||
i += 1
|
||||
|
||||
# CHECKSUM
|
||||
success = True
|
||||
break
|
||||
|
||||
# end while loop
|
||||
|
||||
log.info('Blocklist: List downloaded sucessfully')
|
||||
# Download completed
|
||||
|
||||
|
||||
|
||||
def import_list(self):
|
||||
log.info('Blocklist: Importing list...')
|
||||
log.info('Blocklist: Importing list...')
|
||||
# During import, disable remote connections by setting max to 0
|
||||
deluge.component.get("Core").session.set_max_connections(0)
|
||||
|
||||
#TODO: Dont need try anymore
|
||||
try:
|
||||
self.plugin.reset_ip_filter()
|
||||
self.curr = 0
|
||||
@ -313,61 +325,61 @@ class TorrentBlockList:
|
||||
except:
|
||||
log.debug('Blocklist: Reset filter failed')
|
||||
pass
|
||||
|
||||
|
||||
# Instantiate format class that will read the lists file format
|
||||
try:
|
||||
log.info('Blocklist: ' + str(self.listtype))
|
||||
read_list = FORMATS[self.listtype][1](self.local_blocklist)
|
||||
|
||||
|
||||
except:
|
||||
log.warning('Blocklist: Error: Format read error')
|
||||
self.reset_critical_settings()
|
||||
|
||||
|
||||
try:
|
||||
ips = read_list.next()
|
||||
print ips
|
||||
|
||||
|
||||
while ips:
|
||||
self.plugin.block_ip_range(ips)
|
||||
ips = read_list.next()
|
||||
self.curr += 1
|
||||
# Progress measurement here
|
||||
|
||||
|
||||
log.info(self.curr)
|
||||
|
||||
|
||||
except IOError, e:
|
||||
log.debug('Blocklist: Problem with list, re-download')
|
||||
return
|
||||
|
||||
# Throw exception if curr = 0 reset critical settings
|
||||
|
||||
# If experiencing an unrecoverable error, reset critical settings (url, listtype)
|
||||
if self.curr == 0:
|
||||
log.warning("Blocklist: Improper list read")
|
||||
self.reset_critical_settings()
|
||||
else:
|
||||
|
||||
else: # Sucessful import
|
||||
deluge.component.get("Core").session.set_max_connections(deluge.configmanager.ConfigManager("core.conf")["max_connections_global"])
|
||||
log.info('Blocklist: Import completed sucessfully')
|
||||
|
||||
|
||||
def reset_critical_settings(self):
|
||||
log.info('Blocklist: URL and List type reset')
|
||||
reset_url = BACKUP_PREFS["url"]
|
||||
reset_listtype = BACKUP_PREFS["listtype"]
|
||||
|
||||
|
||||
log.info(reset_url)
|
||||
log.info(reset_listtype)
|
||||
|
||||
|
||||
self.config.set('url', reset_url)
|
||||
self.config.set('listtype', reset_listtype)
|
||||
self.config.save()
|
||||
|
||||
|
||||
self.load_options()
|
||||
log.info(self.url)
|
||||
log.info(self.listtype)
|
||||
self.download()
|
||||
self.import_list()
|
||||
|
||||
|
||||
def return_count(self):
|
||||
return self.curr
|
||||
|
||||
|
||||
def get_config_value(self, key): # url, check_after_days, listtype
|
||||
val = self.config[key]
|
||||
log.debug('Blocklist: Get_config_val')
|
||||
|
Loading…
x
Reference in New Issue
Block a user