Debian Codegraph @

gallery_dl/extractor/steamcommunity.py

raw
1 # -*- coding: utf-8 -*-
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6
7 """Extractors for https://steamcommunity.com/"""
8
9 from ..extractor.common import Extractor, Message
10 from .. import text, dt
11
12 BASE_PATTERN = r"(?:https?://)?(?:www\.)?steamcommunity.com"
13 CONTENT_TYPES = {"screenshots", "artwork"}
14
15
16 class SteamcommunitySharedfileExtractor(Extractor):
17 """Extractor for steamcommunity shared files"""
18 category = "steamcommunity"
19 subcategory = "sharedfile"
20 root = "https://steamcommunity.com"
21 directory_fmt = ("{category}", "{game}", "{content_type!c}")
22 filename_fmt = "{file_id} {title}.{extension}"
23 archive_fmt = "{game}_{file_id}_{ugc_id}"
24 pattern = BASE_PATTERN + r"/sharedfiles/filedetails/\?id=(\d+)"
25 example = "https://steamcommunity.com/sharedfiles/filedetails/?id=12345"
26
27 def items(self):
28 fid = self.groups[0]
29 url = f"{self.root}/sharedfiles/filedetails/?id={fid}"
30 page = self.request(url).text
31 content_type = self._extract_content_type(page)
32
33 meta = {
34 "content_type": content_type,
35 "file_id" : fid,
36 "url" : url,
37 }
38
39 return self.basic_image_items(page, meta)
40
41 def _extract_content_type(self, page):
42 tab = text.extr(
43 page, 'class="apphub_sectionTab active "><span>', '<').lower()
44 if tab in CONTENT_TYPES:
45 return tab
46 raise self.exc.AbortExtraction(f"Unsupported content type '{tab}'")
47
48 def basic_image_items(self, page, meta):
49 extr = text.extract_from(page)
50
51 data = {
52 **meta,
53 "title" : text.unescape(extr(
54 'class="workshopItemTitle">', "<")),
55 "game_appid": extr('class="screenshotAppName', "") or extr(
56 "/app/", "/"),
57 "game" : text.unescape(extr(">", "<")),
58 "creator_id": extr('class="friendBlockLinkOverlay" '
59 'href="https://steamcommunity.com/', '"',
60 ).rpartition("/")[2],
61 "creator" : text.unescape(extr(
62 'class="friendBlockContent">', "<").strip()),
63 "size" : text.parse_bytes(extr(
64 'class="detailsStatRight">', "<")[:-1]),
65 "date" : extr('class="detailsStatRight">', "<"),
66 "width" : text.parse_int(extr(
67 'class="detailsStatRight">', " x ")),
68 "height" : text.parse_int(extr("", "<")),
69 "views" : text.parse_int(extr("<td>", "<").replace(",", "")),
70 "likes" : text.parse_int(text.remove_html(extr(
71 "<tr>", "</")).replace(",", "")),
72 "description": text.unescape(extr(
73 'id="description"', "") or extr(">", "</textarea>")),
74 "extension" : "jpg", # Guess 'jpg' - Rely on extension fixing
75 }
76
77 if "," in (date := data["date"]):
78 data["date"] = self.parse_datetime(date, "%d %b, %Y @ %I:%M%p")
79 else:
80 data["date"] = self.parse_datetime(
81 date, "%d %b @ %I:%M%p").replace(year=dt.datetime.now().year)
82
83 img = text.extr(page, '<img id="ActualMedia"', '>')
84 src = text.unescape(text.extr(img, 'src="', '"'))
85 if (pos := src.find("?")) >= 0:
86 src = src[:pos]
87 data["ugc_id"] = src[src.find("/ugc/")+5:-1]
88
89 yield Message.Directory, "", data
90 yield Message.Url, src, data