aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/bbc.py
blob: cb357d1c16b62f57c9da8cd8e528853252573776 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-

# Copyright 2021-2025 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.

"""Extractors for https://bbc.co.uk/"""

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

BASE_PATTERN = r"(?:https?://)?(?:www\.)?bbc\.co\.uk(/programmes/"


class BbcGalleryExtractor(GalleryExtractor):
    """Extractor for a programme gallery on bbc.co.uk"""
    category = "bbc"
    root = "https://www.bbc.co.uk"
    directory_fmt = ("{category}", "{path:I}")
    filename_fmt = "{num:>02}.{extension}"
    archive_fmt = "{programme}_{num}"
    pattern = rf"{BASE_PATTERN}[^/?#]+(?!/galleries)(?:/[^/?#]+)?)$"
    example = "https://www.bbc.co.uk/programmes/PATH"

    def metadata(self, page):
        data = self._extract_jsonld(page)

        return {
            "title": text.unescape(text.extr(
                page, "<h1>", "</h1>").rpartition("</span>")[2]),
            "description": text.unescape(text.extr(
                page, 'property="og:description" content="', '"')),
            "programme": self.page_url.split("/")[4],
            "path": list(util.unique_sequence(
                element["name"]
                for element in data["itemListElement"]
            )),
        }

    def images(self, page):
        width = self.config("width")
        width = width - width % 16 if width else 1920
        dimensions = f"/{width}xn/"

        results = []
        for img in text.extract_iter(page, 'class="gallery__thumbnail', ">"):
            src = text.extr(img, 'data-image-src="', '"')
            results.append((
                src.replace("/320x180_b/", dimensions),
                {
                    "title_image": text.unescape(text.extr(
                        img, 'data-gallery-title="', '"')),
                    "synopsis": text.unescape(text.extr(
                        img, 'data-gallery-synopsis="', '"')),
                    "_fallback": self._fallback_urls(src, width),
                },
            ))
        return results

    def _fallback_urls(self, src, max_width):
        front, _, back = src.partition("/320x180_b/")
        for width in (1920, 1600, 1280, 976):
            if width < max_width:
                yield f"{front}/{width}xn/{back}"


class BbcProgrammeExtractor(Extractor):
    """Extractor for all galleries of a bbc programme"""
    category = "bbc"
    subcategory = "programme"
    root = "https://www.bbc.co.uk"
    pattern = rf"{BASE_PATTERN}[^/?#]+/galleries)(?:/?\?page=(\d+))?"
    example = "https://www.bbc.co.uk/programmes/ID/galleries"

    def items(self):
        path, pnum = self.groups
        data = {"_extractor": BbcGalleryExtractor}
        params = {"page": text.parse_int(pnum, 1)}
        galleries_url = self.root + path

        while True:
            page = self.request(galleries_url, params=params).text
            for programme_id in text.extract_iter(
                    page, '<a href="https://www.bbc.co.uk/programmes/', '"'):
                url = "https://www.bbc.co.uk/programmes/" + programme_id
                yield Message.Queue, url, data
            if 'rel="next"' not in page:
                return
            params["page"] += 1