2015-12-03 00:50:34 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-05-12 23:52:01 +02:00
|
|
|
# Copyright 2015-2020 Mike Fährmann
|
2015-12-03 00:50:34 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-10-01 15:54:27 +02:00
|
|
|
"""Recursive extractor"""
|
2015-12-03 00:50:34 +01:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2018-09-16 20:59:22 +02:00
|
|
|
import requests
|
|
|
|
import re
|
2015-12-03 00:50:34 +01:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2016-10-01 15:54:27 +02:00
|
|
|
class RecursiveExtractor(Extractor):
|
2017-05-23 11:48:00 +02:00
|
|
|
"""Extractor that fetches URLs from a remote or local source"""
|
2016-10-01 15:54:27 +02:00
|
|
|
category = "recursive"
|
2019-02-12 10:20:21 +01:00
|
|
|
pattern = r"r(?:ecursive)?:"
|
2019-02-08 13:45:40 +01:00
|
|
|
test = ("recursive:https://pastebin.com/raw/FLwrCYsT", {
|
2016-10-01 15:54:27 +02:00
|
|
|
"url": "eee86d65c346361b818e8f4b2b307d9429f136a2",
|
2019-02-08 13:45:40 +01:00
|
|
|
})
|
2015-12-03 00:50:34 +01:00
|
|
|
|
|
|
|
def items(self):
|
2019-02-12 10:20:21 +01:00
|
|
|
self.session.mount("file://", FileAdapter())
|
|
|
|
page = self.request(self.url.partition(":")[2]).text
|
2020-05-12 23:52:01 +02:00
|
|
|
del self.session.adapters["file://"]
|
2019-02-12 10:20:21 +01:00
|
|
|
|
2020-09-11 13:11:46 +02:00
|
|
|
for match in re.finditer(r"https?://[^\s\"']+", page):
|
|
|
|
yield Message.Queue, match.group(0), {}
|
2018-09-16 20:59:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FileAdapter(requests.adapters.BaseAdapter):
|
|
|
|
"""Requests adapter for local files"""
|
|
|
|
|
|
|
|
def send(self, request, **kwargs):
|
|
|
|
response = requests.Response()
|
|
|
|
try:
|
|
|
|
response.raw = open(request.url[7:], "rb")
|
|
|
|
except OSError:
|
|
|
|
import io
|
|
|
|
response.raw = io.BytesIO()
|
|
|
|
response.status_code = requests.codes.bad_request
|
|
|
|
else:
|
|
|
|
response.raw.release_conn = response.raw.close
|
|
|
|
response.status_code = requests.codes.ok
|
|
|
|
return response
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
pass
|