Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mikf
GitHub Repository: mikf/gallery-dl
Path: blob/master/gallery_dl/extractor/bbc.py
5399 views
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2021-2025 Mike Fährmann
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 2 as
7
# published by the Free Software Foundation.
8
9
"""Extractors for https://bbc.co.uk/"""
10
11
from .common import GalleryExtractor, Extractor, Message
12
from .. import text, util
13
14
BASE_PATTERN = r"(?:https?://)?(?:www\.)?bbc\.co\.uk(/programmes/"
15
16
17
class BbcGalleryExtractor(GalleryExtractor):
18
"""Extractor for a programme gallery on bbc.co.uk"""
19
category = "bbc"
20
root = "https://www.bbc.co.uk"
21
directory_fmt = ("{category}", "{path[0]}", "{path[1]}", "{path[2]}",
22
"{path[3:]:J - /}")
23
filename_fmt = "{num:>02}.{extension}"
24
archive_fmt = "{programme}_{num}"
25
pattern = BASE_PATTERN + r"[^/?#]+(?!/galleries)(?:/[^/?#]+)?)$"
26
example = "https://www.bbc.co.uk/programmes/PATH"
27
28
def metadata(self, page):
29
data = self._extract_jsonld(page)
30
31
return {
32
"title": text.unescape(text.extr(
33
page, "<h1>", "</h1>").rpartition("</span>")[2]),
34
"description": text.unescape(text.extr(
35
page, 'property="og:description" content="', '"')),
36
"programme": self.page_url.split("/")[4],
37
"path": list(util.unique_sequence(
38
element["name"]
39
for element in data["itemListElement"]
40
)),
41
}
42
43
def images(self, page):
44
width = self.config("width")
45
width = width - width % 16 if width else 1920
46
dimensions = f"/{width}xn/"
47
48
results = []
49
for img in text.extract_iter(page, 'class="gallery__thumbnail', ">"):
50
src = text.extr(img, 'data-image-src="', '"')
51
results.append((
52
src.replace("/320x180_b/", dimensions),
53
{
54
"title_image": text.unescape(text.extr(
55
img, 'data-gallery-title="', '"')),
56
"synopsis": text.unescape(text.extr(
57
img, 'data-gallery-synopsis="', '"')),
58
"_fallback": self._fallback_urls(src, width),
59
},
60
))
61
return results
62
63
def _fallback_urls(self, src, max_width):
64
front, _, back = src.partition("/320x180_b/")
65
for width in (1920, 1600, 1280, 976):
66
if width < max_width:
67
yield f"{front}/{width}xn/{back}"
68
69
70
class BbcProgrammeExtractor(Extractor):
71
"""Extractor for all galleries of a bbc programme"""
72
category = "bbc"
73
subcategory = "programme"
74
root = "https://www.bbc.co.uk"
75
pattern = BASE_PATTERN + r"[^/?#]+/galleries)(?:/?\?page=(\d+))?"
76
example = "https://www.bbc.co.uk/programmes/ID/galleries"
77
78
def items(self):
79
path, pnum = self.groups
80
data = {"_extractor": BbcGalleryExtractor}
81
params = {"page": text.parse_int(pnum, 1)}
82
galleries_url = self.root + path
83
84
while True:
85
page = self.request(galleries_url, params=params).text
86
for programme_id in text.extract_iter(
87
page, '<a href="https://www.bbc.co.uk/programmes/', '"'):
88
url = "https://www.bbc.co.uk/programmes/" + programme_id
89
yield Message.Queue, url, data
90
if 'rel="next"' not in page:
91
return
92
params["page"] += 1
93
94