2015-06-29 23:09:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-10-03 08:23:40 +02:00
|
|
|
# Copyright 2015, 2016 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.
|
|
|
|
|
|
|
|
"""Base classes for extractors for different Futaba Channel boards"""
|
|
|
|
|
2015-10-05 17:15:31 +02:00
|
|
|
from .common import Extractor, Message
|
2015-10-03 15:43:02 +02:00
|
|
|
from .. import text
|
2015-06-29 23:09:35 +02:00
|
|
|
|
2016-10-03 08:23:40 +02:00
|
|
|
class ChanThreadExtractor(Extractor):
|
2016-09-12 10:20:57 +02:00
|
|
|
"""Base class for extractors for Futaba Channel 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]
|