| 1 |
import re |
| 2 |
from datetime import datetime, timezone |
| 3 |
|
| 4 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 5 |
|
| 6 |
|
| 7 |
class ellenpontRecipe(BasicNewsRecipe): |
| 8 |
__author__ = 'bubak' |
| 9 |
title = 'Ellenpont' |
| 10 |
description = 'ellenpont.hu' |
| 11 |
oldest_article = 2 |
| 12 |
max_articles_per_feed = 20 |
| 13 |
|
| 14 |
language = 'hu' |
| 15 |
cover_url = 'https://ellenpont.hu/images/ellenpont_cim_nagy.png' |
| 16 |
remove_javascript = True |
| 17 |
no_stylesheets = True |
| 18 |
|
| 19 |
sections = [('Politika', 'https://ellenpont.hu/politika'), ('Oknyomozás', 'https://ellenpont.hu/oknyomozas'), ('Vélemény', 'https://ellenpont.hu/velemeny')] |
| 20 |
ignore_duplicate_articles = {'title', 'url'} |
| 21 |
|
| 22 |
preprocess_regexps = [(re.compile(r'<a\s[^>]*>([^<]*)</a>', re.DOTALL | re.IGNORECASE), lambda match: match.group(1))] |
| 23 |
|
| 24 |
remove_attributes = [] |
| 25 |
remove_tags_before = dict(name='h1', attrs={'class': ['AAAtitulek-clanku']}) |
| 26 |
remove_tags = [ |
| 27 |
dict(name='div', attrs={'id': ['AAAsocial-bookmark']}), |
| 28 |
dict(name='div', attrs={'class': re.compile(r'sidebar-column|post-share|no-login-icon', re.IGNORECASE)}), |
| 29 |
dict(name=['header', 'footer']), |
| 30 |
dict(name='ol', attrs={'class': ['frontend_footnotes-list']}), |
| 31 |
dict(name='div', attrs={'class': ['article-footer']}), |
| 32 |
] |
| 33 |
|
| 34 |
visited_urls = {} |
| 35 |
|
| 36 |
def get_article_url(self, article): |
| 37 |
url = BasicNewsRecipe.get_article_url(self, article) |
| 38 |
if url in self.visited_urls: |
| 39 |
self.log.debug('Ignoring duplicate: ' + url) |
| 40 |
return None |
| 41 |
else: |
| 42 |
self.visited_urls[url] = True |
| 43 |
self.log.debug('Accepting: ' + url) |
| 44 |
return url |
| 45 |
|
| 46 |
def get_browser(self, *a, **kw): |
| 47 |
kw['verify_ssl_certificates'] = False |
| 48 |
return super().get_browser(*a, **kw) |
| 49 |
|
| 50 |
def parse_index(self): |
| 51 |
res = [] |
| 52 |
for section in self.sections: |
| 53 |
self.log('\tTrying section ' + section[0]) |
| 54 |
soup = self.index_to_soup(section[1]) |
| 55 |
content = soup.findAll('div', attrs={'class': 'news-card'}) |
| 56 |
|
| 57 |
if content is None: |
| 58 |
raise ValueError('Could not find section content') |
| 59 |
seen_urls = set() |
| 60 |
articles = [] |
| 61 |
for a in content: |
| 62 |
if a.a: |
| 63 |
url = a.a['href'] |
| 64 |
if url.startswith('/'): |
| 65 |
url = 'https://ellenpont.hu' + url |
| 66 |
if url in seen_urls: |
| 67 |
continue |
| 68 |
seen_urls.add(url) |
| 69 |
self.log('\tFound a ' + url) |
| 70 |
title = a.h3.a.contents[0] |
| 71 |
self.log('\tFound article:', title, url) |
| 72 |
articles.append({'title': title, 'url': url, 'description': '', 'date': ''}) |
| 73 |
else: |
| 74 |
url = a['href'] |
| 75 |
if url.startswith('/'): |
| 76 |
url = 'https://ellenpont.hu' + url |
| 77 |
if url in seen_urls: |
| 78 |
continue |
| 79 |
seen_urls.add(url) |
| 80 |
self.log('\tFound ' + url) |
| 81 |
title = a.find('div').find('div').find('div').contents[0] |
| 82 |
self.log('\tFound article:', title, url) |
| 83 |
articles.append({'title': title, 'url': url, 'description': '', 'date': ''}) |
| 84 |
res.append((section[0], articles)) |
| 85 |
return res |
| 86 |
|
| 87 |
def populate_article_metadata(self, article, soup, first): |
| 88 |
self.log('article is ', str(article)) |
| 89 |
s = soup.find('div', attrs={'class': 'intro-content'}) |
| 90 |
dd = s('ul')[1].li.text |
| 91 |
d = datetime.strptime(dd, '%Y. %B %d. %-H:%-M').timestamp() |
| 92 |
nn = datetime.now(timezone.utc).timestamp() |
| 93 |
article.date = dd |
| 94 |
self.log('Article date ', article.date) |
| 95 |
if d < nn - self.oldest_article * 86400: |
| 96 |
self.log('Aborting article date ', article.date, ' oldest ', self.oldest_article) |
| 97 |
self.abort_article('aborting date dd') |