diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-01-11 15:22:26 +0100 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-01-11 15:22:26 +0100 |
commit | 6d0e7c8fe0da4ce96fb9c7df3ab9c970f3a2913d (patch) | |
tree | 36d6ff44e909aa00bcb52adbe1f2fe5ede1f53f3 /src/web/lib/feed_utils.py | |
parent | Moved the crawler in the parent folder. (diff) | |
parent | fixing stuffs (diff) | |
download | newspipe-6d0e7c8fe0da4ce96fb9c7df3ab9c970f3a2913d.tar.gz newspipe-6d0e7c8fe0da4ce96fb9c7df3ab9c970f3a2913d.tar.bz2 newspipe-6d0e7c8fe0da4ce96fb9c7df3ab9c970f3a2913d.zip |
Merge pull request #30 from jaesivsm/master
misc fixes on crawlers and use of User-Agent
Diffstat (limited to 'src/web/lib/feed_utils.py')
-rw-r--r-- | src/web/lib/feed_utils.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/web/lib/feed_utils.py b/src/web/lib/feed_utils.py index f3b18224..14e6b82b 100644 --- a/src/web/lib/feed_utils.py +++ b/src/web/lib/feed_utils.py @@ -2,19 +2,26 @@ import urllib import logging import requests import feedparser +from conf import USER_AGENT from bs4 import BeautifulSoup, SoupStrainer from web.lib.utils import try_keys, try_get_icon_url, rebuild_url logger = logging.getLogger(__name__) +logging.captureWarnings(True) + + +def is_parsing_ok(parsed_feed): + return parsed_feed['entries'] or not parsed_feed['bozo'] def construct_feed_from(url=None, fp_parsed=None, feed=None, query_site=True): + requests_kwargs = {'headers': {'User-Agent': USER_AGENT}, 'verify': False} if url is None and fp_parsed is not None: url = fp_parsed.get('url') if url is not None and fp_parsed is None: try: - response = requests.get(url, verify=False) + response = requests.get(url, **requests_kwargs) fp_parsed = feedparser.parse(response.content, request_headers=response.headers) except Exception: @@ -24,7 +31,7 @@ def construct_feed_from(url=None, fp_parsed=None, feed=None, query_site=True): feed = feed or {} feed_split = urllib.parse.urlsplit(url) site_split = None - if not fp_parsed['bozo']: + if is_parsing_ok(fp_parsed): feed['link'] = url feed['site_link'] = try_keys(fp_parsed['feed'], 'href', 'link') feed['title'] = fp_parsed['feed'].get('title') @@ -48,7 +55,7 @@ def construct_feed_from(url=None, fp_parsed=None, feed=None, query_site=True): return feed try: - response = requests.get(feed['site_link'], verify=False) + response = requests.get(feed['site_link'], **requests_kwargs) except Exception: logger.exception('failed to retreive %r', feed['site_link']) return feed |