Debian Codegraph @

gallery_dl/extractor/imdb.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2026 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://www.imdb.com/"""
10
11 from .common import Extractor, Message
12 from .. import text, util
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.)?imdb\.com"
15
16 GQL_HASHES = {
17 "NameImages":
18 "4223ddf53fc6196821daf52d9ce5bb3080cae25db60f76bb8fd4ff7bd1ff94ce",
19 "TitleImages":
20 "1e5d5f0f8f7a27bad078b4eb915554a92f0123750f6e39abdd16ac981ef8a84c",
21 }
22
23
24 class ImdbExtractor(Extractor):
25 """Base class for imdb extractors"""
26 category = "imdb"
27 root = "https://www.imdb.com"
28
29 def request_graphql(self, opname, variables, sub="api"):
30 extensions = {
31 "persistedQuery": {
32 "sha256Hash": GQL_HASHES[opname],
33 "version" : 1,
34 }
35 }
36 params = {
37 "operationName": opname,
38 "variables" : util.json_dumps(variables),
39 "extensions" : util.json_dumps(extensions),
40 }
41 headers = {
42 "Accept": "application/graphql+json, application/json",
43 "content-type": "application/json",
44 "x-imdb-client-name": "imdb-web-next-localized",
45 # "x-amzn-sessionid": "...",
46 # "x-imdb-client-rid": "...",
47 "x-imdb-user-language": "en-US",
48 "x-imdb-user-country": "US",
49 "x-imdb-consent-info": "eyJwdXJwb3NlcyI6W10sInZlbmRvcnMiOltdLCJhZ2"
50 "TaWduYWwiOiJBRFVMVCIsImlzR2RwciI6dHJ1ZX0",
51 }
52 url = f"https://{sub}.graphql.imdb.com/"
53 return self.request_json(url, params=params, headers=headers)
54
55 def items_images(self, opname, callback=None):
56 variables = {
57 "after" : "",
58 "first" : 20,
59 "firstYes": True,
60 "id" : self.groups[0],
61 "lastYes" : False,
62 }
63
64 while True:
65 data = self.request_graphql(opname, variables)
66
67 info = data["data"].popitem()[1]
68 if callback is not None:
69 callback(info)
70 callback = None
71
72 images = info["images"]
73 self.kwdict["count"] = images["total"]
74 for edge in images["edges"]:
75 node = edge["node"]
76 node.pop("correctionLink", None)
77 node.pop("reportingLink", None)
78 node["num"] = edge.get("position") or 0
79 node["names"] = [
80 n["nameText"]["text"] for n in node.get("names") or ()]
81 node["titles"] = [
82 t["titleText"]["text"] for t in node.get("titles") or ()]
83 node["countries"] = [
84 c["text"] for c in node.get("countries") or ()]
85 node["languages"] = [
86 c["text"] for c in node.get("languages") or ()]
87
88 url = node["url"]
89 yield Message.Directory, "", node
90 yield Message.Url, url, text.nameext_from_url(url, node)
91
92 if not images["pageInfo"]["hasNextPage"]:
93 break
94 variables["after"] = images["pageInfo"]["endCursor"]
95
96
97 class ImdbNameImagesExtractor(ImdbExtractor):
98 subcategory = "name-images"
99 directory_fmt = ("{category}", "{name}", "Photos")
100 filename_fmt = "{num:>03}{titles:J - /? //} ({id}).{extension}"
101 archive_fmt = "{name}.{id}"
102 pattern = BASE_PATTERN + r"/name/(nm\d+)/mediaviewer"
103 example = "https://www.imdb.com/name/nm012345/mediaviewer/"
104
105 def items(self):
106 return self.items_images("NameImages", self._callback)
107
108 def _callback(self, info):
109 kwdict = self.kwdict
110 kwdict["name"] = info["nameText"]["text"]
111
112
113 class ImdbTitleImagesExtractor(ImdbExtractor):
114 subcategory = "title-images"
115 directory_fmt = ("{category}", "{title}", "Photos")
116 filename_fmt = "{num:>03}{names:J - /? //} ({id}).{extension}"
117 archive_fmt = "{title}.{id}"
118 pattern = BASE_PATTERN + r"/title/(tt\d+)/mediaviewer"
119 example = "https://www.imdb.com/title/tt012345/mediaviewer/"
120
121 def items(self):
122 return self.items_images("TitleImages", self._callback)
123
124 def _callback(self, info):
125 kwdict = self.kwdict
126 kwdict["title"] = info["titleText"]["text"]
127 kwdict["year"] = info["releaseYear"]["year"]
128 kwdict["status"] = info["meta"]["publicationStatus"]