| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
from calibre.utils.date import parse_date, utcnow |
| 4 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 5 |
|
| 6 |
|
| 7 |
class AdvancedUserRecipe1639926896(BasicNewsRecipe): |
| 8 |
__author__ = 'Aisteru' |
| 9 |
__copyright__ = '2021, Timothée Andres <timothee dot andres at gmail dot com>' |
| 10 |
__license__ = 'GNU General Public License v3 - http://www.gnu.org/copyleft/gpl.html' |
| 11 |
|
| 12 |
title = 'Equestria Daily' |
| 13 |
description = 'Everything new in Equestria and beyond!' |
| 14 |
language = 'en' |
| 15 |
|
| 16 |
# Max. supported by website: 50 |
| 17 |
max_articles_per_feed = 30 |
| 18 |
|
| 19 |
compress_news_images = True |
| 20 |
compress_news_images_auto_size = 4 |
| 21 |
no_stylesheets = True |
| 22 |
keep_only_tags = [{'name': 'div', 'class_': ['post', 'hentry']}] |
| 23 |
remove_tags = [{'name': 'div', 'class_': 'post-footer'}] |
| 24 |
extra_css = '.article_date { margin-left: 10px; }' |
| 25 |
|
| 26 |
# Masthead image dimensions |
| 27 |
MI_WIDTH = 600 |
| 28 |
MI_HEIGHT = 200 |
| 29 |
|
| 30 |
# To discard posts under a certain section, simply comment the whole line |
| 31 |
sections = [ |
| 32 |
('Art', 'Art'), |
| 33 |
('News', 'News'), |
| 34 |
('Fics', 'Fanfiction'), |
| 35 |
('Media', 'Media'), |
| 36 |
('Comics', 'Comic'), |
| 37 |
('Community', 'Community'), |
| 38 |
('Editorial', 'Editorial'), |
| 39 |
] |
| 40 |
|
| 41 |
def get_masthead_url(self): |
| 42 |
soup = self.index_to_soup('https://www.equestriadaily.com') |
| 43 |
img = soup.select_one('#header img') |
| 44 |
return img['src'] |
| 45 |
|
| 46 |
def parse_index(self): |
| 47 |
results = {} |
| 48 |
current_date = utcnow() |
| 49 |
|
| 50 |
def clean_description(description): |
| 51 |
lines = description.split('\n') |
| 52 |
return '\n'.join([line.strip() for line in lines if len(line.strip()) > 0]) |
| 53 |
|
| 54 |
for section_name, section_url_name in self.sections: |
| 55 |
soup = self.index_to_soup(f'https://www.equestriadaily.com/search/label/{section_url_name}?max-results={self.max_articles_per_feed}') |
| 56 |
articles = soup.select('div.post.hentry') |
| 57 |
previous_post_date = current_date |
| 58 |
|
| 59 |
for article in articles: |
| 60 |
article_entry = {} |
| 61 |
|
| 62 |
header = article.select_one('h3 > a') |
| 63 |
article_entry['title'] = header.text |
| 64 |
article_entry['url'] = header['href'] |
| 65 |
article_entry['date'] = article.select_one('span.post-timestamp').text.split('\n')[1] |
| 66 |
article_entry['description'] = clean_description(article.select_one('div.entry-content').text) |
| 67 |
article_entry['content'] = '' # Must be empty |
| 68 |
|
| 69 |
post_date = previous_post_date |
| 70 |
|
| 71 |
try: |
| 72 |
post_date = parse_date(article_entry['date']) |
| 73 |
previous_post_date = post_date |
| 74 |
except Exception: |
| 75 |
pass |
| 76 |
|
| 77 |
if (current_date - post_date).days <= self.oldest_article: |
| 78 |
results.setdefault(section_name, []).append(article_entry) |
| 79 |
|
| 80 |
return [(section, results[section]) for section in results] |