Debian Codegraph @

gallery_dl/extractor/myportfolio.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018-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.myportfolio.com/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14
15 class MyportfolioGalleryExtractor(Extractor):
16 """Extractor for an image gallery on www.myportfolio.com"""
17 category = "myportfolio"
18 subcategory = "gallery"
19 directory_fmt = ("{category}", "{user}", "{title}")
20 filename_fmt = "{num:>02}.{extension}"
21 archive_fmt = "{user}_{filename}"
22 pattern = (r"(?:myportfolio:(?:https?://)?([^/]+)|"
23 r"(?:https?://)?(?!cdn\.)([\w-]+\.myportfolio\.com))"
24 r"(/[^/?#]+)?")
25 example = "https://USER.myportfolio.com/TITLE"
26
27 def items(self):
28 domain_alt, domain, path = self.groups
29 if domain is None:
30 domain = domain_alt
31 prefix = "myportfolio:"
32 else:
33 prefix = ""
34
35 url = f"https://{domain}{path or ''}"
36 response = self.request(url)
37 if response.history and response.url.endswith(".adobe.com/missing"):
38 raise self.exc.NotFoundError()
39 page = response.text
40
41 projects = text.extr(
42 page, '<section class="project-covers', '</section>')
43
44 if projects:
45 data = {"_extractor": MyportfolioGalleryExtractor}
46 base = f"{prefix}https://{domain}"
47 for path in text.extract_iter(projects, ' href="', '"'):
48 yield Message.Queue, base + path, data
49 else:
50 data = self.metadata(page)
51 imgs = self.images(page)
52 data["count"] = len(imgs)
53 yield Message.Directory, "", data
54 for data["num"], url in enumerate(imgs, 1):
55 yield Message.Url, url, text.nameext_from_url(url, data)
56
57 def metadata(self, page):
58 """Collect general image metadata"""
59 # og:title contains data as "<user> - <title>", but both
60 # <user> and <title> can contain a "-" as well, so we get the title
61 # from somewhere else and cut that amount from the og:title content
62
63 extr = text.extract_from(page)
64 user = extr('property="og:title" content="', '"') or \
65 extr('property=og:title content="', '"')
66 descr = extr('property="og:description" content="', '"') or \
67 extr('property=og:description content="', '"')
68 title = extr('<h1 ', '</h1>')
69
70 if title:
71 title = title.partition(">")[2]
72 user = user[:-len(title)-3]
73 elif user:
74 user, _, title = user.partition(" - ")
75 else:
76 raise self.exc.NotFoundError()
77
78 return {
79 "user": text.unescape(user),
80 "title": text.unescape(title),
81 "description": text.unescape(descr),
82 }
83
84 def images(self, page):
85 """Extract and return a list of all image-urls"""
86 return (
87 list(text.extract_iter(page, 'js-lightbox" data-src="', '"')) or
88 list(text.extract_iter(page, 'data-src="', '"'))
89 )