Debian Codegraph @

recipes/psych.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3
4 from calibre.web.feeds.recipes import BasicNewsRecipe
5
6
7 def absurl(url):
8 if url.startswith('//'):
9 return 'https:' + url
10 if url.startswith('/'):
11 return 'https://www.psychologytoday.com' + url
12 return url
13
14
15 def classes(classes):
16 q = frozenset(classes.split(' '))
17 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
18
19
20 class PsychologyToday(BasicNewsRecipe):
21 title = 'Psychology Today'
22 __author__ = 'Kovid Goyal'
23
24 description = (
25 'This magazine takes information from the latest research'
26 ' in the field of psychology and makes it useful to people in their everyday'
27 ' lives. Its coverage encompasses self-improvement, relationships, the mind-body'
28 ' connection, health, family, the workplace and culture.'
29 )
30 language = 'en'
31 encoding = 'UTF-8'
32 no_stylesheets = True
33 publication_type = 'magazine'
34 remove_attributes = ['style', 'height', 'width']
35 extra_css = '''
36 .image-article_inline_full, .image-article-inline-half { text-align:center; font-size:small; }
37 em, blockquote { color:#202020; }
38 .blog-entry__date--full { font-size:small; }
39 '''
40
41 recipe_specific_options = {'date': {'short': 'The date of the Past Edition to download (YYYY/MM format)', 'long': 'For example, 2024/07'}}
42
43 keep_only_tags = [dict(attrs={'id': 'block-pt-content'})]
44 remove_tags = [classes('pt-social-media')]
45
46 def parse_index(self):
47 soup = self.index_to_soup('https://www.psychologytoday.com/us/magazine/archive')
48 a = soup.find(**classes('magazine-thumbnail')).a
49 url = a['href']
50 past_edition = self.recipe_specific_options.get('date')
51 if past_edition and isinstance(past_edition, str):
52 url = '/us/magazine/archive/' + past_edition
53 soup = self.index_to_soup(absurl(url))
54 cov = soup.find(**classes('content-header--cover-image'))
55 if cov:
56 self.cover_url = cov.img['src']
57 self.timefmt = ' [%s]' % cov.img['alt'].replace(' magazine cover', '')
58 articles = []
59 for article in soup.findAll('div', attrs={'class': 'article-text'}):
60 title = self.tag_to_string(article.find(attrs={'class': ['h2', 'h3']})).strip()
61 url = absurl(article.find(attrs={'class': ['h2', 'h3']}).a['href'])
62 self.log('\n', title, 'at', url)
63 desc = self.tag_to_string(article.find('p', **classes('description'))).strip()
64 author = self.tag_to_string(article.find('p', **classes('byline')).a).strip()
65 if desc:
66 self.log(desc)
67 else:
68 desc = ''
69 articles.append({'title': title, 'url': url, 'description': desc, 'author': author})
70 return [('Articles', articles)]