[4chan] more metadata entries (size, width, height)

This commit is contained in:
Mike Fährmann
2015-05-04 14:57:47 +02:00
parent f82cd5c7f7
commit 5993177ae3

View File

@@ -18,51 +18,55 @@ info = {
"directory": ["{category}", "{board}-{thread-id}"],
"filename": "{timestamp}-{name}",
"pattern": [
r"(?:https?://)?boards\.4chan\.org/([^/]+)/thread/(\d+)(?:/([^#]*))?",
r"(?:https?://)?boards\.4chan\.org/([^/]+)/thread/(\d+).*",
],
}
class FourChanExtractor(SequentialExtractor):
url_fmt = "https://boards.4chan.org/{board}/res/{thread-id}.html"
url_fmt = "https://boards.4chan.org/{0}/res/{1}.html"
regex = (
r'<a href="(//i.4cdn.org/[^/]+/(\d+)\.([^"]+))"'
r' target="_blank">([^<]+)</a>'
r'<a (?:title="(?P<orig_name>[^"]+)" )?href="'
r'(?P<url>//i.4cdn.org/[^/]+/(?P<timestamp>\d+)\.(?P<extension>[^"]+))'
r'" target="_blank">(?P<name>[^<]+)</a> '
r'\((?P<size>[^,]+), (?P<width>\d+)x(?P<height>\d+)\)'
)
def __init__(self, match, config):
SequentialExtractor.__init__(self, config)
self.metadata = self.get_job_metadata(match)
self.match = match
self.metadata = None
def items(self):
yield Message.Version, 1
yield Message.Directory, self.metadata
url = self.url_fmt.format(**self.metadata)
url = self.url_fmt.format(*self.match.groups())
text = self.request(url).text
self.metadata = self.get_job_metadata(text)
yield Message.Directory, self.metadata
for match in re.finditer(self.regex, text):
yield Message.Url, self.get_file_url(match), self.get_file_metadata(match)
@staticmethod
def get_job_metadata(match):
def get_job_metadata(self, text):
"""Collect metadata for extractor-job"""
board, thread_id, title = match.groups()
board, thread_id = self.match.groups()
title, _ = self.extract(text, '"description" content="', ' - &quot;/')
return {
"category": info["category"],
"board": board,
"thread-id": thread_id,
"title": title,
"title": unquote(title),
}
def get_file_metadata(self, match):
"""Collect metadata for a downloadable file"""
data = self.metadata
data["timestamp"] = match.group(2)
data["extension"] = match.group(3)
data["name"] = unquote(match.group(4))
data.update(match.groupdict(default=""))
data["name"] = unquote(data["orig_name"] or data["name"])
return data
@staticmethod
def get_file_url(match):
"""Extract download-url from 'match'"""
return "https:" + match.group(1)
return "https:" + match.group("url")