2015-06-29 23:09:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-07-03 16:43:04 +02:00
|
|
|
# Copyright 2015-2017 Mike Fährmann
|
2015-06-29 23:09:35 +02: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.
|
|
|
|
|
2017-07-14 08:44:31 +02:00
|
|
|
"""Base classes for extractors for different Futaba Channel-like boards"""
|
2015-06-29 23:09:35 +02:00
|
|
|
|
2017-08-29 22:42:48 +02:00
|
|
|
from .common import Extractor, SharedConfigExtractor, Message
|
2015-10-03 15:43:02 +02:00
|
|
|
from .. import text
|
2017-07-14 13:24:09 +02:00
|
|
|
import itertools
|
2017-08-13 14:27:58 +02:00
|
|
|
import operator
|
2015-06-29 23:09:35 +02:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2016-10-03 08:23:40 +02:00
|
|
|
class ChanThreadExtractor(Extractor):
|
2017-07-14 08:44:31 +02:00
|
|
|
"""Base class for extractors for Futaba Channel-like boards"""
|
2016-10-03 08:23:40 +02:00
|
|
|
category = "chan"
|
|
|
|
subcategory = "thread"
|
2015-11-21 03:13:06 +01:00
|
|
|
directory_fmt = ["{category}", "{board}-{thread}"]
|
|
|
|
filename_fmt = "{tim}-{filename}{ext}"
|
2015-06-29 23:09:35 +02:00
|
|
|
api_url = ""
|
|
|
|
file_url = ""
|
|
|
|
|
2016-10-03 08:23:40 +02:00
|
|
|
def __init__(self, match):
|
2015-10-05 17:15:31 +02:00
|
|
|
Extractor.__init__(self)
|
2015-06-29 23:09:35 +02:00
|
|
|
self.metadata = {
|
2016-10-03 08:23:40 +02:00
|
|
|
"board": match.group(1),
|
|
|
|
"thread": match.group(2),
|
2015-06-29 23:09:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
2016-10-03 08:23:40 +02:00
|
|
|
url = self.api_url.format_map(self.metadata)
|
|
|
|
posts = self.request(url).json()["posts"]
|
2015-06-29 23:09:35 +02:00
|
|
|
self.metadata["title"] = self.get_thread_title(posts[0])
|
|
|
|
yield Message.Directory, self.metadata
|
|
|
|
for post in posts:
|
|
|
|
if "filename" not in post:
|
|
|
|
continue
|
2016-10-07 15:11:58 +02:00
|
|
|
self.update(post)
|
2016-10-15 23:06:54 +02:00
|
|
|
yield Message.Url, self.build_url(post), post
|
2015-09-07 16:32:20 +02:00
|
|
|
if "extra_files" in post:
|
|
|
|
for file in post["extra_files"]:
|
2016-10-07 15:11:58 +02:00
|
|
|
self.update(post, file)
|
2016-10-15 23:06:54 +02:00
|
|
|
yield Message.Url, self.build_url(post), post
|
2015-06-29 23:09:35 +02:00
|
|
|
|
2016-10-07 15:11:58 +02:00
|
|
|
def update(self, post, data=None):
|
|
|
|
"""Update keyword dictionary"""
|
|
|
|
post.update(data or self.metadata)
|
|
|
|
post["extension"] = post["ext"][1:]
|
|
|
|
|
2016-10-15 23:06:54 +02:00
|
|
|
def build_url(self, post):
|
|
|
|
"""Construct an image url out of a post object"""
|
|
|
|
return self.file_url.format_map(post)
|
|
|
|
|
2015-06-29 23:09:35 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_thread_title(post):
|
|
|
|
"""Return thread title from first post"""
|
2015-11-11 00:51:07 +01:00
|
|
|
title = post["sub"] if "sub" in post else text.remove_html(post["com"])
|
|
|
|
return text.unescape(title)[:50]
|
2017-07-03 16:43:04 +02:00
|
|
|
|
|
|
|
|
2017-08-29 22:42:48 +02:00
|
|
|
class FoolfuukaThreadExtractor(SharedConfigExtractor):
|
2017-07-03 16:43:04 +02:00
|
|
|
"""Base extractor for FoolFuuka based boards/archives"""
|
2017-08-29 22:42:48 +02:00
|
|
|
basecategory = "foolfuuka"
|
2017-07-03 16:43:04 +02:00
|
|
|
subcategory = "thread"
|
|
|
|
directory_fmt = ["{category}", "{board[shortname]}",
|
|
|
|
"{thread_num} - {title}"]
|
|
|
|
filename_fmt = "{media[media]}"
|
|
|
|
root = ""
|
|
|
|
|
|
|
|
def __init__(self, match):
|
2017-08-29 22:42:48 +02:00
|
|
|
SharedConfigExtractor.__init__(self)
|
2017-07-03 16:43:04 +02:00
|
|
|
self.board, self.thread = match.groups()
|
2017-07-15 13:23:04 +02:00
|
|
|
self.session.headers["User-Agent"] = "Mozilla 5.0"
|
|
|
|
self.session.headers["Referer"] = self.root
|
2017-07-03 16:43:04 +02:00
|
|
|
|
|
|
|
def items(self):
|
|
|
|
op = True
|
|
|
|
yield Message.Version, 1
|
|
|
|
for post in self.posts():
|
|
|
|
if op:
|
|
|
|
yield Message.Directory, post
|
|
|
|
op = False
|
|
|
|
if not post["media"]:
|
|
|
|
continue
|
2017-07-14 13:24:09 +02:00
|
|
|
|
|
|
|
media = post["media"]
|
|
|
|
url = media["media_link"]
|
|
|
|
|
|
|
|
if not url and "remote_media_link" in media:
|
|
|
|
needle = '<meta http-equiv="Refresh" content="0; url='
|
|
|
|
page = self.request(media["remote_media_link"]).text
|
|
|
|
url = text.extract(page, needle, '"')[0]
|
|
|
|
|
2017-07-03 16:43:04 +02:00
|
|
|
post["extension"] = url.rpartition(".")[2]
|
|
|
|
yield Message.Url, url, post
|
|
|
|
|
|
|
|
def posts(self):
|
|
|
|
url = self.root + "/_/api/chan/thread/"
|
|
|
|
params = {"board": self.board, "num": self.thread}
|
|
|
|
data = self.request(url, params=params).json()[self.thread]
|
2017-08-13 14:27:58 +02:00
|
|
|
|
|
|
|
# sort post-objects by their key
|
|
|
|
posts = sorted(data["posts"].items(), key=operator.itemgetter(0))
|
|
|
|
posts = map(operator.itemgetter(1), posts)
|
|
|
|
|
|
|
|
return itertools.chain((data["op"],), posts)
|