Debian Codegraph @

gallery_dl/extractor/itchio.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2023 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://itch.io/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14
15 class ItchioGameExtractor(Extractor):
16 """Extractor for itch.io games"""
17 category = "itchio"
18 subcategory = "game"
19 root = "https://itch.io"
20 directory_fmt = ("{category}", "{user[name]}")
21 filename_fmt = "{game[title]} ({id}).{extension}"
22 archive_fmt = "{id}"
23 pattern = r"(?:https?://)?(\w+)\.itch\.io/([\w-]+)"
24 example = "https://USER.itch.io/GAME"
25
26 def __init__(self, match):
27 self.user, self.slug = match.groups()
28 Extractor.__init__(self, match)
29
30 def items(self):
31 game_url = f"https://{self.user}.itch.io/{self.slug}"
32 page = self.request(game_url).text
33
34 params = {
35 "source": "view_game",
36 "as_props": "1",
37 "after_download_lightbox": "true",
38 }
39 headers = {
40 "Referer": game_url,
41 "X-Requested-With": "XMLHttpRequest",
42 "Origin": f"https://{self.user}.itch.io",
43 }
44 data = {
45 "csrf_token": text.unquote(self.cookies["itchio_token"]),
46 }
47
48 for upload_id in text.extract_iter(page, 'data-upload_id="', '"'):
49 file_url = f"{game_url}/file/{upload_id}"
50 info = self.request_json(file_url, method="POST", params=params,
51 headers=headers, data=data)
52
53 game = info["lightbox"]["game"]
54 user = info["lightbox"]["user"]
55 game["url"] = game_url
56 user.pop("follow_button", None)
57 game = {"game": game, "user": user, "id": upload_id}
58
59 url = info["url"]
60 yield Message.Directory, "", game
61 yield Message.Url, url, text.nameext_from_url(url, game)