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-03 07:10:00 -05:00
|
|
|
import collections
|
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-03 07:10:00 -05:00
|
|
|
from feedcache.cache import Cache
|
2014-12-02 14:09:53 -05:00
|
|
|
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
|
|
|
try:
|
2014-12-03 07:10:00 -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))
|
|
|
|
|
2014-12-02 14:09:53 -05:00
|
|
|
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-03 07:10:00 -05:00
|
|
|
fc = Cache(self.rssDB).purge(age)
|
|
|
|
fc.purge(age)
|
|
|
|
finally:
|
|
|
|
self.rssDB.close()
|
2014-06-29 06:05:33 -04:00
|
|
|
|
2014-12-07 12:16:41 -05:00
|
|
|
def getFeed(self, url, post_data=None, request_headers=None, items=[]):
|
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-12-08 04:41:42 -05:00
|
|
|
data = dict.fromkeys(items, None)
|
|
|
|
|
2014-07-15 01:53:32 -04:00
|
|
|
try:
|
2014-12-03 07:10:00 -05:00
|
|
|
fc = Cache(self.rssDB)
|
2014-12-07 12:16:41 -05:00
|
|
|
resp = fc.fetch(url, False, False, request_headers)
|
2014-12-06 01:16:30 -05:00
|
|
|
|
2014-12-07 12:16:41 -05:00
|
|
|
for item in items:
|
2014-12-08 04:41:42 -05:00
|
|
|
try:data[item] = resp[item]
|
|
|
|
except:continue
|
2014-12-07 12:16:41 -05:00
|
|
|
|
2014-12-03 07:10:00 -05:00
|
|
|
finally:
|
2014-12-08 04:41:42 -05:00
|
|
|
self.rssDB.close()
|
|
|
|
|
|
|
|
return data
|