aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/speakerdeck.py
blob: a3819c76d9b4839242545d1445a4258098345d55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-

# Copyright 2020 Leonardo Taccari
#
# 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.

"""Extract images from https://speakerdeck.com/"""

from .common import Extractor, Message
from .. import text


class SpeakerdeckPresentationExtractor(Extractor):
    """Extractor for images from a presentation on speakerdeck.com"""
    category = "speakerdeck"
    subcategory = "presentation"
    directory_fmt = ("{category}", "{user}")
    filename_fmt = "{presentation}-{num:>02}.{extension}"
    archive_fmt = "{presentation}_{num}"
    pattern = (r"(?:https?://)?(?:www\.)?speakerdeck\.com"
               r"/([^/?&#]+)/([^/?&#]+)")
    test = (
        (("https://speakerdeck.com/speakerdeck/introduction-to-speakerdeck"), {
            "pattern": r"https://files.speakerdeck.com/presentations/"
                       r"50021f75cf1db900020005e7/slide_\d+.jpg",
            "content": "75c7abf0969b0bcab23e0da9712c95ee5113db3a",
            "count": 6,
        }),
    )

    def __init__(self, match):
        Extractor.__init__(self, match)
        self.user, self.presentation = match.groups()
        self.presentation_id = None

    def items(self):
        data = self.get_job_metadata()
        imgs = self.get_image_urls()
        data["count"] = len(imgs)
        yield Message.Version, 1
        yield Message.Directory, data
        for data["num"], url in enumerate(imgs, 1):
            yield Message.Url, url, text.nameext_from_url(url, data)

    def get_job_metadata(self):
        """Collect metadata for extractor-job"""
        url = "https://speakerdeck.com/oembed.json"
        params = {
            "url": "https://speakerdeck.com/" + self.user +
                   "/" + self.presentation,
        }

        data = self.request(url, params=params).json()

        self.presentation_id, pos = \
            text.extract(data["html"], 'src="//speakerdeck.com/player/', '"')

        return {
            "user": self.user,
            "presentation": self.presentation,
            "presentation_id": self.presentation_id,
            "title": data["title"],
            "author": data["author_name"],
        }

    def get_image_urls(self):
        """Extract and return a list of all image-urls"""
        page = self.request("https://speakerdeck.com/player/" +
                            self.presentation_id).text
        return list(text.extract_iter(page, 'js-sd-slide" data-url="', '"'))