2015-04-08 01:43:25 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2014, 2015 Mike Fährmann
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Common classes and constants used by extractor modules."""
|
|
|
|
|
2015-04-08 01:46:04 +02:00
|
|
|
import time
|
2014-10-12 21:56:44 +02:00
|
|
|
import queue
|
|
|
|
import requests
|
2015-04-08 01:46:04 +02:00
|
|
|
import threading
|
2015-10-05 15:35:48 +02:00
|
|
|
from .. import config
|
2015-04-08 01:46:04 +02:00
|
|
|
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-04-08 01:43:25 +02:00
|
|
|
class Message():
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-04-08 01:43:25 +02:00
|
|
|
Version = 1
|
|
|
|
Directory = 2
|
|
|
|
Url = 3
|
2015-04-08 19:06:50 +02:00
|
|
|
Headers = 4
|
|
|
|
Cookies = 5
|
2015-04-08 01:43:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Extractor():
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.session = requests.Session()
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
def __iter__(self):
|
2015-04-08 01:43:25 +02:00
|
|
|
return self.items()
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
return
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
def request(self, url, *args, **kwargs):
|
|
|
|
return safe_request(self.session, url, *args, **kwargs)
|
|
|
|
|
|
|
|
def enable_useragent(self):
|
2015-04-08 01:43:25 +02:00
|
|
|
self.session.headers["User-Agent"] = (
|
|
|
|
"Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0"
|
|
|
|
)
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-04-08 01:43:25 +02:00
|
|
|
|
2015-04-10 15:29:09 +02:00
|
|
|
class AsynchronousExtractor(Extractor):
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-10-05 15:35:48 +02:00
|
|
|
def __init__(self):
|
2015-04-08 01:43:25 +02:00
|
|
|
Extractor.__init__(self)
|
2015-10-05 15:35:48 +02:00
|
|
|
queue_size = int(config.get(("queue-size",), default=5))
|
2015-04-08 01:43:25 +02:00
|
|
|
self.__queue = queue.Queue(maxsize=queue_size)
|
2015-04-10 17:31:49 +02:00
|
|
|
self.__thread = threading.Thread(target=self.async_items, daemon=True)
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
def __iter__(self):
|
2015-04-08 01:43:25 +02:00
|
|
|
get = self.__queue.get
|
2014-10-12 21:56:44 +02:00
|
|
|
done = self.__queue.task_done
|
|
|
|
|
|
|
|
self.__thread.start()
|
|
|
|
while True:
|
|
|
|
task = get()
|
|
|
|
if task is None:
|
|
|
|
return
|
|
|
|
yield task
|
|
|
|
done()
|
|
|
|
|
2015-04-08 01:43:25 +02:00
|
|
|
def async_items(self):
|
2014-10-12 21:56:44 +02:00
|
|
|
put = self.__queue.put
|
|
|
|
try:
|
2015-04-08 01:43:25 +02:00
|
|
|
for task in self.items():
|
2014-10-12 21:56:44 +02:00
|
|
|
put(task)
|
2015-04-08 01:43:25 +02:00
|
|
|
except Exception:
|
2014-10-12 21:56:44 +02:00
|
|
|
import traceback
|
|
|
|
print(traceback.format_exc())
|
|
|
|
put(None)
|
2015-04-08 01:46:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def safe_request(session, url, method="GET", *args, **kwargs):
|
|
|
|
tries = 0
|
|
|
|
while True:
|
|
|
|
# try to connect to remote source
|
|
|
|
try:
|
|
|
|
r = session.request(method, url, *args, **kwargs)
|
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
tries += 1
|
|
|
|
time.sleep(1)
|
|
|
|
if tries == 5:
|
|
|
|
raise
|
|
|
|
continue
|
|
|
|
|
|
|
|
# reject error-status-codes
|
|
|
|
if r.status_code != requests.codes.ok:
|
|
|
|
tries += 1
|
|
|
|
time.sleep(1)
|
|
|
|
if tries == 5:
|
|
|
|
r.raise_for_status()
|
|
|
|
continue
|
|
|
|
|
|
|
|
# everything ok -- proceed to download
|
|
|
|
return r
|