Debian Codegraph @

gallery_dl/extractor/telegraph.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 """Extractor for https://telegra.ph/"""
8
9 from .common import GalleryExtractor
10 from .. import text
11
12
13 class TelegraphGalleryExtractor(GalleryExtractor):
14 """Extractor for articles from telegra.ph"""
15 category = "telegraph"
16 root = "https://telegra.ph"
17 directory_fmt = ("{category}", "{slug}")
18 filename_fmt = "{num_formatted}_{filename}.{extension}"
19 archive_fmt = "{slug}_{num}"
20 pattern = r"(?:https?://)(?:www\.)??telegra\.ph(/[^/?#]+)"
21 example = "https://telegra.ph/TITLE"
22
23 def metadata(self, page):
24 extr = text.extract_from(page)
25 data = {
26 "title": text.unescape(extr(
27 'property="og:title" content="', '"')),
28 "description": text.unescape(extr(
29 'property="og:description" content="', '"')),
30 "date": self.parse_datetime_iso(extr(
31 'property="article:published_time" content="', '"')),
32 "author": text.unescape(extr(
33 'property="article:author" content="', '"')),
34 "post_url": text.unescape(extr(
35 'rel="canonical" href="', '"')),
36 }
37
38 pos = page.find("<article") + 8
39 data["html"] = self.html = \
40 page[page.find(">", pos)+1:page.find("</article>", pos)]
41 data["links"] = text.extract_urls(self.html)
42 data["slug"] = data["post_url"][19:]
43
44 return data
45
46 def images(self, page):
47 figures = (
48 tuple(text.extract_iter(self.html, "<figure>", "</figure>")) or
49 tuple(text.extract_iter(self.html, "<img", ">")))
50 num_zeroes = len(str(len(figures)))
51 num = 0
52
53 results = []
54 for figure in figures:
55 url, pos = text.extract(figure, 'src="', '"')
56 if url.startswith("/embed/"):
57 continue
58 elif url[0] == "/":
59 url = self.root + url
60 caption, pos = text.extract(figure, "<figcaption>", "<", pos)
61 num += 1
62
63 results.append((url, {
64 "url" : url,
65 "caption" : text.unescape(caption) if caption else "",
66 "num" : num,
67 "num_formatted": str(num).zfill(num_zeroes),
68 }))
69 return results