Debian Codegraph @

gallery_dl/extractor/listal.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://listal.com"""
8
9 from .common import Extractor, Message
10 from .. import text
11
12 BASE_PATTERN = r"(?:https?://)?(?:www\.)?listal\.com"
13
14
15 class ListalExtractor(Extractor):
16 """Base class for Listal extractor"""
17 category = "listal"
18 root = "https://www.listal.com"
19 directory_fmt = ("{category}", "{title}")
20 filename_fmt = "{id}_{filename}.{extension}"
21 archive_fmt = "{id}/{filename}"
22
23 def items(self):
24 for image_id in self.image_ids():
25 img = self._extract_image(image_id)
26 url = img["url"]
27 text.nameext_from_url(url, img)
28 yield Message.Directory, "", img
29 yield Message.Url, url, img
30
31 def _pagination(self, base_url, pnum=None):
32 if pnum is None:
33 url = base_url
34 pnum = 1
35 else:
36 url = f"{base_url}/{pnum}"
37
38 while True:
39 page = self.request(url).text
40
41 yield page
42
43 if pnum is None or "<span class='nextprev'>Next" in page:
44 return
45 pnum += 1
46 url = f"{base_url}/{pnum}"
47
48 def _extract_image(self, image_id):
49 url = f"{self.root}/viewimage/{image_id}h"
50 page = self.request(url).text
51 extr = text.extract_from(page)
52
53 return {
54 "id" : image_id,
55 "url" : extr("<div><center><img src='", "'"),
56 "title" : text.unescape(extr('title="', '"')),
57 "width" : text.parse_int(extr("width='", "'")),
58 "height" : text.parse_int(extr("height='", "'")),
59 "author_url": extr("Added by <a href='", "'"),
60 "author" : text.unescape(extr(">", "<")),
61 "date" : self.parse_datetime(extr(
62 " ago on ", "<"), "%d %B %Y %H:%M"),
63 }
64
65
66 class ListalImageExtractor(ListalExtractor):
67 """Extractor for listal pictures"""
68 subcategory = "image"
69 pattern = BASE_PATTERN + r"/viewimage/(\d+)"
70 example = "https://www.listal.com/viewimage/12345678"
71
72 def image_ids(self):
73 return (self.groups[0],)
74
75
76 class ListalPeopleExtractor(ListalExtractor):
77 """Extractor for listal people pictures"""
78 subcategory = "people"
79 pattern = BASE_PATTERN + r"/([^/?#]+)/pictures"
80 example = "https://www.listal.com/NAME/pictures"
81
82 def image_ids(self):
83 url = f"{self.root}/{self.groups[0]}/pictures"
84 for page in self._pagination(url):
85 yield from text.extract_iter(page, "listal.com/viewimage/", "'")