From c8e5b2e89eaffec912b37a56cfa42482cc2105b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 29 Jun 2015 23:09:35 +0200 Subject: [PATCH] base class for futaba-chan boards with api --- gallery_dl/extractor/chan.py | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 gallery_dl/extractor/chan.py diff --git a/gallery_dl/extractor/chan.py b/gallery_dl/extractor/chan.py new file mode 100644 index 00000000..cb336774 --- /dev/null +++ b/gallery_dl/extractor/chan.py @@ -0,0 +1,47 @@ + +# -*- coding: utf-8 -*- + +# Copyright 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. + +"""Base classes for extractors for different Futaba Channel boards""" + +from .common import SequentialExtractor, Message + +class ChanExtractor(SequentialExtractor): + + api_url = "" + file_url = "" + + def __init__(self, config, category, board, thread): + SequentialExtractor.__init__(self, config) + self.metadata = { + "category": category, + "board": board, + "thread": thread, + } + + def items(self): + yield Message.Version, 1 + posts = self.request(self.api_url.format(**self.metadata)).json()["posts"] + self.metadata["title"] = self.get_thread_title(posts[0]) + yield Message.Directory, self.metadata + for post in posts: + if "filename" not in post: + continue + post.update(self.metadata) + yield Message.Url, self.file_url.format(**post), post + + @staticmethod + def get_thread_title(post): + """Return thread title from first post""" + if "sub" in post: + return post["sub"] + com = post["com"] + pos = com.find("
") + if pos == -1: + return com + return com[:min(pos, 50)]