qotnews/apiserver/scrapers/simple.py

28 lines
847 B
Python
Raw Normal View History

import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG)
import requests
2020-11-18 04:21:37 +00:00
from settings import SIMPLE_READER_PORT
2020-11-18 04:21:37 +00:00
READ_API = 'http://127.0.0.1:{}/simple/details'.format(SIMPLE_READER_PORT or 33843)
2020-11-04 02:14:51 +00:00
TIMEOUT = 20
def get_html(url):
2020-11-17 02:50:31 +00:00
logging.info(f"Simple Scraper: {url}")
2020-11-04 02:37:19 +00:00
details = get_details(url)
if not details:
return ''
return details['content']
def get_details(url):
try:
2020-11-04 02:14:51 +00:00
r = requests.post(READ_API, data=dict(url=url), timeout=TIMEOUT)
if r.status_code != 200:
raise Exception('Bad response code ' + str(r.status_code))
return r.json()
except KeyboardInterrupt:
raise
except BaseException as e:
logging.error('Problem getting article: {}'.format(str(e)))
2020-11-04 02:14:51 +00:00
return None