Debian Codegraph @

recipes/go_comics.recipe

raw
1 #!/usr/bin/env python
2 import json
3 from datetime import datetime, timedelta
4
5 from calibre.ebooks.BeautifulSoup import BeautifulSoup
6 from calibre.web.feeds.news import BasicNewsRecipe
7
8
9 class GoComics(BasicNewsRecipe):
10 title = 'Go Comics'
11 __author__ = 'unkn0wn'
12 description = 'The best and funniest cartoons you loved then and now—all in one place!'
13 oldest_article = 2
14 language = 'en'
15 remove_empty_feeds = True
16 masthead_url = 'https://auth.amuniversal.com/development-assets/go-comics/images/logo-horizontal.svg'
17 cover_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/GoComics_logo.jpg/960px-GoComics_logo.jpg'
18 simultaneous_downloads = 1
19
20 recipe_specific_options = {
21 'coverurl': {
22 'short': 'Cover URL',
23 'long': 'URL of an image to be used as book cover',
24 'default': 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/GoComics_logo.jpg/960px-GoComics_logo.jpg',
25 },
26 'mastheadurl': {
27 'short': 'Masthead URL',
28 'long': 'URL of an image to be used as masthead',
29 'default': 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/GoComics_logo.jpg/960px-GoComics_logo.jpg',
30 },
31 'comics': {
32 'short': 'enter the slugs of the url\nof the comics you want seperated by a space',
33 'long': 'calvinandhobbes garfield animalcrackers the-born-loser ... ...',
34 'default': 'calvinandhobbes garfield animalcrackers pearlsbeforeswine bc peanuts',
35 },
36 'days': {
37 'short': 'Oldest article to download from this news source. In days ',
38 'long': 'For example, 3, gives you comics from past 3 days',
39 'default': str(oldest_article),
40 },
41 'res': {
42 'short': 'For hi-res images, select a resolution from the\nfollowing options: 800, 1000, 1200 or 2400',
43 'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use 400 or 300.',
44 'default': '1000',
45 },
46 'rev': {
47 'short': 'Reverse the order of articles in each feed?',
48 'long': 'enter yes',
49 'default': 'no',
50 },
51 }
52
53 def __init__(self, *args, **kwargs):
54 BasicNewsRecipe.__init__(self, *args, **kwargs)
55 r = self.recipe_specific_options.get('rev')
56 if r and isinstance(r, str):
57 if r.lower().strip() == 'yes':
58 self.reverse_article_order = True
59 r = self.recipe_specific_options.get('coverurl')
60 if r and isinstance(r, str):
61 self.cover_url = r
62 r = self.recipe_specific_options.get('mastheadurl')
63 if r and isinstance(r, str):
64 self.masthead_url = r
65
66 def parse_index(self):
67 d = self.recipe_specific_options.get('days')
68 if d and isinstance(d, str):
69 self.oldest_article = int(d)
70
71 dates = [(datetime.now() - timedelta(days=i)).strftime('%Y/%m/%d') for i in range(self.oldest_article)]
72
73 u = self.recipe_specific_options.get('comics')
74 if u and isinstance(u, str):
75 coms = u.split()
76
77 feeds = []
78
79 for x in coms:
80 section = x.upper()
81 self.log(section)
82 articles = []
83 for day in dates:
84 title = section + ' | ' + day
85 url = 'https://www.gocomics.com/' + x + '/' + day
86 self.log('\t', title, '\n\t\t', url)
87 articles.append({'title': title, 'url': url})
88 if articles:
89 feeds.append((section, articles))
90 return feeds
91
92 def preprocess_raw_html(self, raw, *a):
93 width = '1000'
94 w = self.recipe_specific_options.get('res')
95 if w and isinstance(w, str):
96 width = w
97 soup = BeautifulSoup(raw)
98 div = soup.find(attrs={'class': lambda x: x and x.startswith('ShowComicViewer_showComicViewer__comic')})
99 script = div.find('script', attrs={'type': 'application/ld+json'})
100 data = json.loads(self.tag_to_string(script))
101 title = '<h1>' + data['name'] + '</h1>'
102 auth = '<p style="font-size: small;">' + data['description'] + '</p>'
103 img = '<img src={}>'.format(data['contentUrl'] + '?optimizer=image&width=' + width + '&quality=85')
104 return '<html><body>' + title + img + auth + '</body></html>'
105
106 def populate_article_metadata(self, article, soup, first):
107 article.title = self.tag_to_string(soup.find('h1'))
108 article.summary = article.text_summary = self.tag_to_string(soup.find('p'))