ETags for reading lists
This commit is contained in:
parent
c610c93720
commit
88bdbe930e
@ -39,12 +39,12 @@ def setTimeout(timeout):
|
|||||||
try:
|
try:
|
||||||
from planet import timeoutsocket
|
from planet import timeoutsocket
|
||||||
timeoutsocket.setDefaultSocketTimeout(timeout)
|
timeoutsocket.setDefaultSocketTimeout(timeout)
|
||||||
logger.debug("Socket timeout set to %d seconds", timeout)
|
logger.info("Socket timeout set to %d seconds", timeout)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import socket
|
import socket
|
||||||
if hasattr(socket, 'setdefaulttimeout'):
|
if hasattr(socket, 'setdefaulttimeout'):
|
||||||
logger.debug("timeoutsocket not found, using python function")
|
logger.debug("timeoutsocket not found, using python function")
|
||||||
socket.setdefaulttimeout(timeout)
|
socket.setdefaulttimeout(timeout)
|
||||||
logger.debug("Socket timeout set to %d seconds", timeout)
|
logger.info("Socket timeout set to %d seconds", timeout)
|
||||||
else:
|
else:
|
||||||
logger.error("Unable to set timeout to %d seconds", timeout)
|
logger.error("Unable to set timeout to %d seconds", timeout)
|
||||||
|
@ -28,6 +28,7 @@ Todo:
|
|||||||
|
|
||||||
import os, sys, re
|
import os, sys, re
|
||||||
from ConfigParser import ConfigParser
|
from ConfigParser import ConfigParser
|
||||||
|
from urlparse import urljoin
|
||||||
|
|
||||||
parser = ConfigParser()
|
parser = ConfigParser()
|
||||||
|
|
||||||
@ -169,16 +170,44 @@ def load(config_file):
|
|||||||
for list in reading_lists:
|
for list in reading_lists:
|
||||||
cache_filename = filename(config.cache_lists_directory(), list)
|
cache_filename = filename(config.cache_lists_directory(), list)
|
||||||
try:
|
try:
|
||||||
import urllib, StringIO
|
import urllib2, StringIO
|
||||||
|
|
||||||
# read once to verify
|
# retrieve list options (e.g., etag, last-modified) from cache
|
||||||
data=StringIO.StringIO(urllib.urlopen(list).read())
|
options = {}
|
||||||
|
try:
|
||||||
cached_config = ConfigParser()
|
cached_config = ConfigParser()
|
||||||
|
cached_config.read(cache_filename)
|
||||||
|
for option in cached_config.options(list):
|
||||||
|
options[option] = cached_config.get(list,option)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
cached_config = ConfigParser()
|
||||||
|
cached_config.add_section(list)
|
||||||
|
for key, value in options.items():
|
||||||
|
cached_config.set(list, key, value)
|
||||||
|
|
||||||
|
# read list
|
||||||
|
base = urljoin('file:', os.path.abspath(os.path.curdir))
|
||||||
|
request = urllib2.Request(urljoin(base + '/', list))
|
||||||
|
if options.has_key("etag"):
|
||||||
|
request.add_header('If-None-Match', options['etag'])
|
||||||
|
if options.has_key("last-modified"):
|
||||||
|
request.add_header('If-Modified-Since',
|
||||||
|
options['last-modified'])
|
||||||
|
response = urllib2.urlopen(request)
|
||||||
|
if response.headers.has_key('etag'):
|
||||||
|
cached_config.set(list, 'etag', response.headers['etag'])
|
||||||
|
if response.headers.has_key('last-modified'):
|
||||||
|
cached_config.set(list, 'last-modified',
|
||||||
|
response.headers['last-modified'])
|
||||||
|
|
||||||
|
# convert to config.ini
|
||||||
|
data=StringIO.StringIO(response.read())
|
||||||
if content_type(list).find('opml')>=0:
|
if content_type(list).find('opml')>=0:
|
||||||
opml.opml2config(data, cached_config)
|
opml.opml2config(data, cached_config)
|
||||||
elif content_type(list).find('foaf')>=0:
|
elif content_type(list).find('foaf')>=0:
|
||||||
foaf.foaf2config(data, cached_config)
|
foaf.foaf2config(data, cached_config)
|
||||||
if not cached_config.sections(): raise Exception
|
if cached_config.sections() in [[], [list]]: raise Exception
|
||||||
|
|
||||||
# write to cache
|
# write to cache
|
||||||
cache = open(cache_filename, 'w')
|
cache = open(cache_filename, 'w')
|
||||||
@ -196,7 +225,6 @@ def load(config_file):
|
|||||||
except:
|
except:
|
||||||
log.exception("Unable to read %s readinglist", list)
|
log.exception("Unable to read %s readinglist", list)
|
||||||
continue
|
continue
|
||||||
# planet.foaf.foaf2config(data, list, config)
|
|
||||||
|
|
||||||
def cache_sources_directory():
|
def cache_sources_directory():
|
||||||
if parser.has_option('Planet', 'cache_sources_directory'):
|
if parser.has_option('Planet', 'cache_sources_directory'):
|
||||||
@ -217,7 +245,7 @@ def feed():
|
|||||||
for template_file in template_files:
|
for template_file in template_files:
|
||||||
name = os.path.splitext(os.path.basename(template_file))[0]
|
name = os.path.splitext(os.path.basename(template_file))[0]
|
||||||
if name.find('atom')>=0 or name.find('rss')>=0:
|
if name.find('atom')>=0 or name.find('rss')>=0:
|
||||||
return urlparse.urljoin(link(), name)
|
return urljoin(link(), name)
|
||||||
|
|
||||||
def feedtype():
|
def feedtype():
|
||||||
if parser.has_option('Planet', 'feedtype'):
|
if parser.has_option('Planet', 'feedtype'):
|
||||||
|
@ -45,5 +45,5 @@ class ReadingListTest(unittest.TestCase):
|
|||||||
|
|
||||||
feeds = [split(feed)[1] for feed in parser.sections()]
|
feeds = [split(feed)[1] for feed in parser.sections()]
|
||||||
feeds.sort()
|
feeds.sort()
|
||||||
self.assertEqual(['testfeed0.atom', 'testfeed1a.atom',
|
self.assertEqual(['opml.xml', 'testfeed0.atom', 'testfeed1a.atom',
|
||||||
'testfeed2.atom', 'testfeed3.rss'], feeds)
|
'testfeed2.atom', 'testfeed3.rss'], feeds)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user