| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# License: GNU General Public License v3 - http://www.gnu.org/copyleft/gpl.html |
| 4 |
# Copyright: [email protected] |
| 5 |
|
| 6 |
__license__ = 'GNU General Public License v3 - http://www.gnu.org/copyleft/gpl.html' |
| 7 |
__copyright__ = '[email protected]' |
| 8 |
|
| 9 |
|
| 10 |
# This imports the version bundled with Calibre |
| 11 |
import lxml |
| 12 |
from lxml.builder import E |
| 13 |
|
| 14 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup |
| 15 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 16 |
|
| 17 |
respekt_url = 'https://www.respekt.cz' |
| 18 |
|
| 19 |
|
| 20 |
class respektRecipe(BasicNewsRecipe): |
| 21 |
__author__ = 'Tomáš Hnyk' |
| 22 |
publisher = 'Respekt Publishing a. s.' |
| 23 |
description = 'Articles from the print edition' |
| 24 |
title = 'Respekt Magazine — Print' |
| 25 |
encoding = 'utf-8' |
| 26 |
language = 'cs' |
| 27 |
delay = 0.001 |
| 28 |
remove_javascript = True |
| 29 |
remove_tags_before = dict(name='h1') |
| 30 |
remove_tags_after = [dict(id='postcontent')] |
| 31 |
remove_tags = [ |
| 32 |
dict(name='div', attrs={'id': ['postsharepopup', 'survey-respondents']}), |
| 33 |
dict(name='div', attrs={'class': ['ad', 'ad-content', 'adinarticle', 'ad-caption', 'post-actions', 'authorship-note', 'quote', 'postgallery']}), |
| 34 |
dict(name='a', attrs={'class': ['quote', 'authorship-face']}), |
| 35 |
dict(name='span', attrs={'class': 'embed'}), |
| 36 |
dict(name='svg'), |
| 37 |
dict(name='script'), |
| 38 |
] |
| 39 |
|
| 40 |
extra_css = '''p {text-align:justify;margin-top:0;margin-bottom:0} |
| 41 |
ul {color:black} |
| 42 |
.frame-caption {font-weight:normal;font-size:50%;font-style:italic;} |
| 43 |
h1 {font-size:150%;margin-bottom:0;} |
| 44 |
h2 {font-size:100%;margin-bottom:0;} |
| 45 |
.post-subtitle {margin-top:0;} |
| 46 |
h3 {font-size:100%;margin-bottom:0;margin-top:0;} |
| 47 |
.box-title {background-color: lightgray;font-size:150%;font-weight:bold;margin-left:12%;margin-right:12%;margin-top:12%;margin-bottom:0;} |
| 48 |
.box-content {background-color:lightgray;margin-left:12%;margin-right:12%;margin-top:0;margin-bottom:12%;} |
| 49 |
p.indent_first_line {text-indent:30px;} |
| 50 |
a {text-decoration:none;color:black;}''' |
| 51 |
|
| 52 |
needs_subscription = True |
| 53 |
|
| 54 |
def get_browser(self): |
| 55 |
br = BasicNewsRecipe.get_browser(self) |
| 56 |
if self.username is not None and self.password is not None: |
| 57 |
br.open('https://www.respekt.cz/uzivatel/prihlaseni') |
| 58 |
for form in br.forms(): |
| 59 |
if form.attrs.get('id') == 'frm-loginForm': |
| 60 |
br.form = form |
| 61 |
break |
| 62 |
for c in form.controls: |
| 63 |
if 'name' in c.attrs: |
| 64 |
if c.attrs['name'] == 'username': |
| 65 |
c.value = self.username |
| 66 |
if c.attrs['name'] == 'password': |
| 67 |
c.value = self.password |
| 68 |
br.submit() |
| 69 |
return br |
| 70 |
|
| 71 |
# So that remove_tags_before works for this section |
| 72 |
def preprocess_raw_html(self, raw_html, url): |
| 73 |
root = lxml.html.fromstring(raw_html) |
| 74 |
if root.xpath('//title')[0].text == ('Respekt • Despekt • RESPEKT'): |
| 75 |
raw_html = raw_html.replace('h2', 'h1') |
| 76 |
return raw_html |
| 77 |
|
| 78 |
def parse_index(self): |
| 79 |
raw1 = self.index_to_soup('https://www.respekt.cz/tydenik/', raw=True) |
| 80 |
root1 = lxml.html.fromstring(raw1) |
| 81 |
current_edition_url = root1.xpath("//div[@class='heroissue']/a")[0].items()[0][1] |
| 82 |
raw2 = self.index_to_soup('https://www.respekt.cz/' + current_edition_url, raw=True) |
| 83 |
root2 = lxml.html.fromstring(raw2) |
| 84 |
self.cover_url = root2.xpath("//i[contains(@class, 'heroissue-cover')]")[0].get('data-src') |
| 85 |
# Fetch date |
| 86 |
date_text = root2.xpath("//time[@class='heroissue-date']")[0].text.split(',')[1] |
| 87 |
s = date_text.split(' ') |
| 88 |
# Are the dates of the issue in the same month and year? |
| 89 |
if len(s) == 4 or len(s) == 7: |
| 90 |
date = '/'.join([s[1].split('.')[0], s[2].split('.')[0], s[-1]]) |
| 91 |
elif len(s) == 8: |
| 92 |
date = '/'.join([s[1].split('.')[0], s[2].split('.')[0], s[3]]) |
| 93 |
self.conversion_options = {'pubdate': date} |
| 94 |
self.title = 'Respekt magazine #' + '/'.join(current_edition_url.split('/')[-1:-3:-1]) |
| 95 |
ans = [] |
| 96 |
for section in root2.xpath("//div[@class='col-md-6']/div[@class='issuedetail-categorized-sectionname']"): |
| 97 |
section_name = section.text |
| 98 |
articles = [] |
| 99 |
article = section.getnext() |
| 100 |
while hasattr(article, 'text') and not article.text.strip(): |
| 101 |
title = article.xpath("span[@class='issuedetail-categorized-title']")[0].text |
| 102 |
url = respekt_url + article.xpath('@href')[0] |
| 103 |
articles.append({'title': title, 'url': url}) |
| 104 |
article = article.getnext() |
| 105 |
ans.append((section_name, articles)) |
| 106 |
return ans |
| 107 |
|
| 108 |
def cleanup(self): |
| 109 |
self.browser.open('https://www.respekt.cz/?do=logout') |
| 110 |
|
| 111 |
def preprocess_html(self, soup): |
| 112 |
raw = ''.join(type('')(a) for a in soup.contents) |
| 113 |
root = lxml.html.fromstring(raw) |
| 114 |
# Fix Letem světem |
| 115 |
if 'Letem sv' in root.xpath('//title')[0].text: |
| 116 |
p = root.xpath('//p') |
| 117 |
for par in p[:]: |
| 118 |
next = par.getnext() |
| 119 |
if par.getchildren(): |
| 120 |
child = par.getchildren()[0] |
| 121 |
if hasattr(next, 'tag') and next.tag == 'h2' and hasattr(child, 'tag') and child.tag == 'strong': |
| 122 |
text = child.text_content() |
| 123 |
if next.text: |
| 124 |
next.text = next.text + ' • ' + text |
| 125 |
elif next.getchildren(): |
| 126 |
next_child = next.getchildren()[0] |
| 127 |
next_child.text = next_child.text + ' • ' + text |
| 128 |
par.getparent().remove(par) |
| 129 |
# Insert text length |
| 130 |
text = root.xpath("//div[@id='postcontent']")[0] |
| 131 |
article_length = ' • ' + str(len(text.text_content().split(' '))) + ' slov' |
| 132 |
try: |
| 133 |
aut = root.xpath("//div[@class='authorship-names']")[0] |
| 134 |
if aut.getchildren() and aut.getchildren()[0].tag == 'a': |
| 135 |
for i, t in enumerate(aut.getchildren()): |
| 136 |
if i == 0: |
| 137 |
t.text = 'Autor: ' + t.text + ' ' |
| 138 |
else: |
| 139 |
t.text = t.text + ' ' |
| 140 |
# Remove link |
| 141 |
e = E.span(t.text) |
| 142 |
t.getparent().replace(t, e) |
| 143 |
else: |
| 144 |
t = root.xpath("//span[@class='post-author-name']")[0] |
| 145 |
t.text = 'Autor: ' + t.text + ' ' |
| 146 |
root.xpath("//div[@class='authorship-names']")[0].append(E.span(article_length)) |
| 147 |
except Exception: |
| 148 |
pass |
| 149 |
# Make images visible |
| 150 |
pictures = root.xpath('//picture') |
| 151 |
for picture in pictures: |
| 152 |
image = picture.xpath('//source')[0] |
| 153 |
image_link = [a for a in image.get('srcset').split(' ') if a[:4] == 'http'][-1] |
| 154 |
e = E.img({'src': image_link}) |
| 155 |
picture.getparent().replace(picture, e) |
| 156 |
# Properly indent |
| 157 |
paragraphs = root.xpath('//p') |
| 158 |
paragraphs.reverse() |
| 159 |
# First paragraph is never indented |
| 160 |
for par in paragraphs[:-1]: |
| 161 |
prev = par.getprevious() |
| 162 |
# Do not indent after headings |
| 163 |
if hasattr(prev, 'tag') and prev.tag not in ['h2', 'h3']: |
| 164 |
par.attrib['class'] = 'indent_first_line' |
| 165 |
# Fix subtitle for Téma |
| 166 |
try: |
| 167 |
o = root.xpath("//p[@class='post-perex']")[0] |
| 168 |
e = E.h2({'class': 'post-subtitle'}) |
| 169 |
e.text = o.text |
| 170 |
o.getparent().replace(o, e) |
| 171 |
except Exception: |
| 172 |
pass |
| 173 |
return BeautifulSoup(lxml.etree.tostring(root, encoding='unicode')) |