Debian Codegraph @

gallery_dl/extractor/fapello.py

raw symbols SCIP
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://fapello.com/"""
8
9 from .common import Extractor, Message
10 from .. import text
11
12
13 BASE_PATTERN = r"(?:https?://)?(?:www\.)?fapello\.(?:com|su)"
14
15
16 class FapelloPostExtractor(Extractor):
17 """Extractor for individual posts on fapello.com"""
18 category = "fapello"
19 subcategory = "post"
20 directory_fmt = ("{category}", "{model}")
21 filename_fmt = "{model}_{id}.{extension}"
22 archive_fmt = "{type}_{model}_{id}"
23 pattern = BASE_PATTERN + r"/(?!search/|popular_videos/)([^/?#]+)/(\d+)"
24 example = "https://fapello.com/MODEL/12345/"
25
26 def __init__(self, match):
27 Extractor.__init__(self, match)
28 self.root = text.root_from_url(match[0])
29 self.model, self.id = match.groups()
30
31 def items(self):
32 url = f"{self.root}/{self.model}/{self.id}/"
33 page = text.extr(
34 self.request(url, allow_redirects=False).text,
35 'class="uk-align-center"', "</div>", None)
36 if page is None:
37 raise self.exc.NotFoundError("post")
38
39 data = {
40 "model": self.model,
41 "id" : text.parse_int(self.id),
42 "type" : "video" if 'type="video' in page else "photo",
43 "thumbnail": text.extr(page, 'poster="', '"'),
44 }
45 url = text.extr(page, 'src="', '"').replace(
46 ".md", "").replace(".th", "")
47 yield Message.Directory, "", data
48 yield Message.Url, url, text.nameext_from_url(url, data)
49
50
51 class FapelloModelExtractor(Extractor):
52 """Extractor for all posts from a fapello model"""
53 category = "fapello"
54 subcategory = "model"
55 pattern = (BASE_PATTERN + r"/(?!top-(?:likes|followers)|popular_videos"
56 r"|videos|trending|search/?$)"
57 r"([^/?#]+)/?$")
58 example = "https://fapello.com/model/"
59
60 def __init__(self, match):
61 Extractor.__init__(self, match)
62 self.root = text.root_from_url(match[0])
63 self.model = match[1]
64
65 def items(self):
66 num = 1
67 data = {"_extractor": FapelloPostExtractor}
68 while True:
69 url = f"{self.root}/ajax/model/{self.model}/page-{num}/"
70 page = self.request(url).text
71 if not page:
72 return
73
74 url = None
75 for url in text.extract_iter(page, '<a href="', '"'):
76 if url == "javascript:void(0);":
77 continue
78 yield Message.Queue, url, data
79 if url is None:
80 return
81 num += 1
82
83
84 class FapelloPathExtractor(Extractor):
85 """Extractor for models and posts from fapello.com paths"""
86 category = "fapello"
87 subcategory = "path"
88 pattern = (BASE_PATTERN +
89 r"/(?!search/?$)(top-(?:likes|followers)|videos|trending"
90 r"|popular_videos/[^/?#]+)/?$")
91 example = "https://fapello.com/trending/"
92
93 def __init__(self, match):
94 Extractor.__init__(self, match)
95 self.root = text.root_from_url(match[0])
96 self.path = match[1]
97
98 def items(self):
99 num = 1
100 if self.path in {"top-likes", "top-followers"}:
101 data = {"_extractor": FapelloModelExtractor}
102 else:
103 data = {"_extractor": FapelloPostExtractor}
104
105 if "fapello.su" in self.root:
106 self.path = self.path.replace("-", "/")
107 if self.path == "trending":
108 data = {"_extractor": FapelloModelExtractor}
109
110 while True:
111 url = f"{self.root}/ajax/{self.path}/page-{num}/"
112 page = self.request(url).text
113 if not page:
114 return
115
116 for item in text.extract_iter(
117 page, 'uk-transition-toggle">', "</a>"):
118 yield Message.Queue, text.extr(item, '<a href="', '"'), data
119 num += 1