| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
__license__ = 'GPL v3' |
| 4 |
''' |
| 5 |
sciencenews.org |
| 6 |
''' |
| 7 |
|
| 8 |
import re |
| 9 |
|
| 10 |
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes |
| 11 |
|
| 12 |
|
| 13 |
class ScienceNewsIssue(BasicNewsRecipe): |
| 14 |
title = 'Science News' |
| 15 |
description = ( |
| 16 |
'Science News is an award-winning bi-weekly newsmagazine covering the most important research' |
| 17 |
' in all fields of science. This recipe downloads all the articles from the latest issue.' |
| 18 |
) |
| 19 |
category = 'Science, Technology, News' |
| 20 |
publisher = 'Society for Science & the Public' |
| 21 |
language = 'en' |
| 22 |
no_stylesheets = True |
| 23 |
use_embedded_content = False |
| 24 |
auto_cleanup = False |
| 25 |
remove_attributes = ['height', 'width', 'style'] |
| 26 |
ignore_duplicate_articles = {'url'} |
| 27 |
resolve_internal_links = True |
| 28 |
|
| 29 |
keep_only_tags = [ |
| 30 |
dict( |
| 31 |
attrs={ |
| 32 |
'class': lambda x: ( |
| 33 |
x and ('single__content___' in x or 'header-default__title___' in x or 'header-default__deck___' in x or 'header-default__figure___' in x) |
| 34 |
) |
| 35 |
} |
| 36 |
) |
| 37 |
] |
| 38 |
remove_tags = [dict(name=['svg', 'button']), dict(attrs={'class': lambda x: x and ('newsletter-signup__wrapper___' in x)})] |
| 39 |
|
| 40 |
def parse_index(self): |
| 41 |
|
| 42 |
index = self.index_to_soup('https://www.sciencenews.org/sn-magazine') |
| 43 |
a = index.find(**prefixed_classes('magazine-archive__issue-thumbnail___')) |
| 44 |
url = a['href'] |
| 45 |
self.timefmt = ' [' + url.split('/')[-1] + ']' |
| 46 |
self.cover_url = a.img['src'] |
| 47 |
|
| 48 |
# Get articles |
| 49 |
soup = self.index_to_soup(url) |
| 50 |
soup = soup.find('main', attrs={'id': 'content'}) |
| 51 |
re_article = re.compile(r'https://www.sciencenews.org/article/') |
| 52 |
stories = [] |
| 53 |
past_urls = set() |
| 54 |
for sec in soup.find_all(href=re_article): |
| 55 |
article_url = sec['href'] |
| 56 |
article_title = sec.text.strip() |
| 57 |
|
| 58 |
# Ignore image URLs which do not have text title |
| 59 |
if article_title == '': |
| 60 |
continue |
| 61 |
|
| 62 |
# Ignore if link is a duplicate |
| 63 |
if article_url in past_urls: |
| 64 |
continue |
| 65 |
|
| 66 |
past_urls.add(article_url) |
| 67 |
self.log('\t', article_title, ' ', article_url) |
| 68 |
article_info = { |
| 69 |
'url': article_url, |
| 70 |
'title': article_title, |
| 71 |
} |
| 72 |
stories.append(article_info) |
| 73 |
|
| 74 |
index = [ |
| 75 |
('Articles', stories), |
| 76 |
] |
| 77 |
return index |