| 1 |
#!/usr/bin/env python |
| 2 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 3 |
|
| 4 |
|
| 5 |
class Volkskrant(BasicNewsRecipe): |
| 6 |
title = 'Dilema' |
| 7 |
__author__ = 'Cristi Ghera' |
| 8 |
max_articles_per_feed = 100 |
| 9 |
description = '"Sint vechi, domnule!" (I.L. Caragiale)' |
| 10 |
needs_subscription = False |
| 11 |
language = 'ro' |
| 12 |
country = 'RO' |
| 13 |
category = 'politics, culture, Romania' |
| 14 |
resolve_internal_links = True |
| 15 |
remove_tags_before = {'class': 'post'} |
| 16 |
remove_tags_after = {'class': 'post_content'} |
| 17 |
remove_tags = [ |
| 18 |
dict( |
| 19 |
attrs={ |
| 20 |
'class': [ |
| 21 |
'single_meta_category', |
| 22 |
'avatar', |
| 23 |
'jm-post-like', |
| 24 |
'fa', |
| 25 |
] |
| 26 |
} |
| 27 |
), |
| 28 |
dict(name=['div'], attrs={'class': ['mb-2']}), |
| 29 |
dict(id=['like', 'dlik']), |
| 30 |
dict(name=['script', 'noscript', 'style']), |
| 31 |
] |
| 32 |
remove_attributes = ['class', 'id', 'name', 'style'] |
| 33 |
encoding = 'utf-8' |
| 34 |
no_stylesheets = True |
| 35 |
ignore_duplicate_articles = {'url'} |
| 36 |
|
| 37 |
def parse_index(self): |
| 38 |
homepage_url = 'https://www.dilema.ro/' |
| 39 |
soup = self.index_to_soup(homepage_url) |
| 40 |
|
| 41 |
articles = [] |
| 42 |
|
| 43 |
# .banner-container |
| 44 |
banner_container = soup.find('div', attrs={'class': 'banner-container'}) |
| 45 |
container = banner_container.find('h5') |
| 46 |
a = container.find('a') |
| 47 |
url = homepage_url + a.attrs['href'] |
| 48 |
articles.append( |
| 49 |
dict( |
| 50 |
title=self.tag_to_string(container).strip(), |
| 51 |
url=url, |
| 52 |
date=self.tag_to_string(banner_container.find(attrs={'class': 'post-date'})).strip(), |
| 53 |
description='', |
| 54 |
content='', |
| 55 |
) |
| 56 |
) |
| 57 |
|
| 58 |
# .homepage_builder_3grid_post |
| 59 |
containers = soup.findAll('div', attrs={'class': 'homepage_builder_3grid_post'}) |
| 60 |
for container in containers: |
| 61 |
if self.tag_to_string(container.find('h2')) in ['CELE MAI RECENTE', 'CELE MAI CITITE']: |
| 62 |
continue |
| 63 |
for article in container.findAll('div', attrs={'class': 'blog_grid_post_style'}): |
| 64 |
title_container = article.find('h3') |
| 65 |
if not title_container: |
| 66 |
continue |
| 67 |
url = title_container.find('a')['href'] |
| 68 |
url = homepage_url + url |
| 69 |
article_title = self.tag_to_string(title_container).strip() |
| 70 |
author = self.tag_to_string(article.find('a', attrs={'rel': 'author'})).strip() |
| 71 |
summary = self.tag_to_string(article.find('p')).strip() |
| 72 |
pubdate = self.tag_to_string(article.find(attrs={'class': 'post-date'})) |
| 73 |
description = author + ' - ' + summary |
| 74 |
articles.append(dict(title=article_title, url=url, date=pubdate, description=description, content='')) |
| 75 |
|
| 76 |
sections = [('Numărul curent', articles)] |
| 77 |
return sections |
| 78 |
|
| 79 |
def preprocess_html(self, soup): |
| 80 |
main_carousel = soup.find(attrs={'id': 'main-carousel'}) |
| 81 |
if main_carousel: |
| 82 |
img = main_carousel.find('img') |
| 83 |
body = soup.find('body') |
| 84 |
body.clear() |
| 85 |
body.append(img) |
| 86 |
return soup |
| 87 |
|
| 88 |
def get_cover_url(self): |
| 89 |
url = 'https://www.dilema.ro/coperta-saptaminii/' |
| 90 |
soup = self.index_to_soup(url) |
| 91 |
img = soup.find(attrs={'id': 'main-carousel'}).find('img') |
| 92 |
return url + img.attrs['src'] |