2014-07-15 01:53:32 -04:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2014-06-29 06:05:33 -04:00
|
|
|
import os
|
|
|
|
import urllib
|
|
|
|
import urlparse
|
|
|
|
import re
|
2014-12-02 14:09:53 -05:00
|
|
|
|
2014-06-29 06:05:33 -04:00
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import encodingKludge as ek
|
2014-07-15 01:53:32 -04:00
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
2014-12-02 14:09:53 -05:00
|
|
|
from contextlib import closing
|
|
|
|
from lib.feedcache import cache
|
|
|
|
from sqliteshelf import SQLiteShelf
|
2014-06-29 06:05:33 -04:00
|
|
|
|
|
|
|
class RSSFeeds:
|
|
|
|
def __init__(self, db_name):
|
2014-12-02 14:09:53 -05:00
|
|
|
db_name = ek.ek(os.path.join, sickbeard.CACHE_DIR, 'rss', db_name) + '.db'
|
|
|
|
if not os.path.exists(os.path.dirname(db_name)):
|
|
|
|
sickbeard.helpers.makeDir(os.path.dirname(db_name))
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.rssDB = SQLiteShelf(db_name)
|
|
|
|
except Exception as e:
|
|
|
|
logger.log(u"RSS error: " + ex(e), logger.DEBUG)
|
2014-06-29 06:05:33 -04:00
|
|
|
|
|
|
|
def clearCache(self, age=None):
|
2014-07-15 01:53:32 -04:00
|
|
|
try:
|
2014-12-02 14:09:53 -05:00
|
|
|
with closing(self.rssDB) as fs:
|
2014-07-15 01:53:32 -04:00
|
|
|
fc = cache.Cache(fs)
|
|
|
|
fc.purge(age)
|
2014-07-27 06:59:21 -04:00
|
|
|
except Exception as e:
|
2014-07-27 07:58:14 -04:00
|
|
|
logger.log(u"RSS error clearing cache: " + ex(e), logger.DEBUG)
|
2014-06-29 06:05:33 -04:00
|
|
|
|
2014-06-30 13:48:18 -04:00
|
|
|
def getFeed(self, url, post_data=None, request_headers=None):
|
2014-07-14 22:00:53 -04:00
|
|
|
parsed = list(urlparse.urlparse(url))
|
|
|
|
parsed[2] = re.sub("/{2,}", "/", parsed[2]) # replace two or more / with one
|
|
|
|
|
|
|
|
if post_data:
|
|
|
|
url += urllib.urlencode(post_data)
|
|
|
|
|
2014-07-15 01:53:32 -04:00
|
|
|
try:
|
2014-12-02 14:09:53 -05:00
|
|
|
with closing(self.rssDB) as fs:
|
2014-07-15 01:53:32 -04:00
|
|
|
fc = cache.Cache(fs)
|
|
|
|
feed = fc.fetch(url, False, False, request_headers)
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-08-31 15:56:18 -04:00
|
|
|
if feed:
|
|
|
|
if 'entries' in feed:
|
|
|
|
return feed
|
|
|
|
elif 'error' in feed.feed:
|
|
|
|
err_code = feed.feed['error']['code']
|
|
|
|
err_desc = feed.feed['error']['description']
|
|
|
|
|
|
|
|
logger.log(
|
|
|
|
u"RSS ERROR:[%s] CODE:[%s]" % (err_desc, err_code), logger.DEBUG)
|
2014-07-27 07:58:14 -04:00
|
|
|
else:
|
2014-08-31 15:56:18 -04:00
|
|
|
logger.log(u"RSS error loading url: " + url, logger.DEBUG)
|
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
except Exception as e:
|
2014-07-27 07:58:14 -04:00
|
|
|
logger.log(u"RSS error: " + ex(e), logger.DEBUG)
|