2015-06-15 19:09:01 +00:00
|
|
|
# coding=utf-8
|
|
|
|
#
|
|
|
|
# This file is part of SickGear.
|
|
|
|
#
|
|
|
|
|
2017-01-27 14:23:35 +00:00
|
|
|
import feedparser
|
2014-06-29 10:05:33 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
from sickbeard import helpers, logger
|
2014-07-15 05:53:32 +00:00
|
|
|
from sickbeard.exceptions import ex
|
2014-07-15 02:00:53 +00:00
|
|
|
|
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
class RSSFeeds:
|
|
|
|
|
|
|
|
def __init__(self, provider=None):
|
|
|
|
|
|
|
|
self.provider = provider
|
2016-02-26 01:07:39 +00:00
|
|
|
self.response = None
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
def _check_auth_cookie(self):
|
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
if self.provider:
|
|
|
|
return self.provider.check_auth_cookie()
|
2015-06-15 19:09:01 +00:00
|
|
|
return True
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2016-02-26 01:07:39 +00:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def cb_response(self, r, *args, **kwargs):
|
|
|
|
self.response = dict(url=r.url, elapsed=r.elapsed, from_cache=r.from_cache)
|
|
|
|
return r
|
|
|
|
|
|
|
|
def get_feed(self, url, request_headers=None, **kwargs):
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
if not self._check_auth_cookie():
|
|
|
|
return
|
|
|
|
|
|
|
|
session = None
|
|
|
|
if self.provider and hasattr(self.provider, 'session'):
|
|
|
|
session = self.provider.session
|
|
|
|
|
2016-02-26 01:07:39 +00:00
|
|
|
response = helpers.getURL(url, headers=request_headers, session=session,
|
|
|
|
hooks=dict(response=self.cb_response), **kwargs)
|
2015-06-15 19:09:01 +00:00
|
|
|
if not response:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
feed = feedparser.parse(response)
|
2016-02-26 01:07:39 +00:00
|
|
|
feed['rq_response'] = self.response
|
2015-06-15 19:09:01 +00:00
|
|
|
if feed and 'entries' in feed:
|
2015-06-06 02:23:39 +00:00
|
|
|
return feed
|
2015-06-15 19:09:01 +00:00
|
|
|
|
|
|
|
if feed and 'error' in feed.feed:
|
2015-06-06 02:23:39 +00:00
|
|
|
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)
|
2015-06-15 19:09:01 +00:00
|
|
|
else:
|
|
|
|
logger.log(u'RSS error loading url: ' + url, logger.DEBUG)
|
2014-08-31 19:56:18 +00:00
|
|
|
|
2015-06-15 19:09:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.log(u'RSS error: ' + ex(e), logger.DEBUG)
|