Debian Codegraph @

recipes/nspm.recipe

raw
1 __license__ = 'GPL v3'
2 __copyright__ = '2008-2011, Darko Miletic <darko.miletic at gmail.com>'
3 '''
4 nspm.rs
5 '''
6
7 import re
8
9 from calibre.ebooks.BeautifulSoup import NavigableString, Tag
10 from calibre.web.feeds.news import BasicNewsRecipe
11
12
13 def new_tag(soup, name, attrs=()):
14 impl = getattr(soup, 'new_tag', None)
15 if impl is not None:
16 return impl(name, attrs=dict(attrs))
17 return Tag(soup, name, attrs=attrs or None)
18
19
20 class Nspm(BasicNewsRecipe):
21 title = 'Nova srpska politicka misao'
22 __author__ = 'Darko Miletic'
23 description = 'Casopis za politicku teoriju i drustvena istrazivanja'
24 publisher = 'NSPM'
25 category = 'news, politics, Serbia'
26 oldest_article = 7
27 max_articles_per_feed = 100
28 no_stylesheets = True
29 use_embedded_content = False
30 INDEX = 'http://www.nspm.rs/?alphabet=l'
31 encoding = 'utf-8'
32 language = 'sr'
33 remove_empty_feeds = True
34 publication_type = 'magazine'
35 masthead_url = 'http://www.nspm.rs/templates/jsn_epic_pro/images/logol.jpg'
36 extra_css = ''' @font-face {font-family: "serif1";src:url(res:///opt/sony/ebook/FONT/tt0011m_.ttf)}
37 @font-face {font-family: "sans1";src:url(res:///opt/sony/ebook/FONT/tt0003m_.ttf)}
38 body{font-family: "Times New Roman", serif1, serif}
39 .article_description{font-family: Arial, sans1, sans-serif}
40 img{margin-top:0.5em; margin-bottom: 0.7em; display: block}
41 .author{color: #990000; font-weight: bold}
42 .author,.createdate{font-size: 0.9em} '''
43
44 conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language, 'pretty_print': True}
45
46 preprocess_regexps = [(re.compile('\u0110'), lambda match: '\u00d0')] # noqa: RUF039
47 remove_tags = [dict(name=['link', 'script', 'meta', 'base', 'img'])]
48 remove_attributes = ['width', 'height', 'lang', 'xmlns:fb', 'xmlns:og', 'vspace', 'hspace', 'type', 'start', 'size']
49
50 def get_browser(self):
51 br = BasicNewsRecipe.get_browser(self)
52 br.open(self.INDEX)
53 return br
54
55 feeds = [
56 ('Rubrike', 'http://www.nspm.rs/rubrike/feed/rss.html'),
57 ('Debate', 'http://www.nspm.rs/debate/feed/rss.html'),
58 ('Reci i misli', 'http://www.nspm.rs/reci-i-misli/feed/rss.html'),
59 ('Samo smeh srbina spasava', 'http://www.nspm.rs/samo-smeh-srbina-spasava/feed/rss.html'),
60 ('Polemike', 'http://www.nspm.rs/polemike/feed/rss.html'),
61 ('Prikazi', 'http://www.nspm.rs/prikazi/feed/rss.html'),
62 ('Prenosimo', 'http://www.nspm.rs/prenosimo/feed/rss.html'),
63 ('Hronika', 'http://www.nspm.rs/tabela/hronika/feed/rss.html'),
64 ]
65
66 def preprocess_html(self, soup):
67 atitle = soup.body.find('a', attrs={'class': 'contentpagetitle'})
68 if atitle:
69 cleanTitle = new_tag(soup, 'h1', [('class', 'contentpagetitle')])
70 cnt = NavigableString(self.tag_to_string(atitle))
71 cleanTitle.append(cnt)
72
73 author = soup.body.find('span', attrs={'class': 'author'})
74 if author:
75 author.extract()
76 author.name = 'div'
77
78 crdate = soup.body.find('td', attrs={'class': 'createdate'})
79 if crdate:
80 cleanCrdate = new_tag(soup, 'div', [('class', 'createdate')])
81 cnt = NavigableString(self.tag_to_string(crdate))
82 cleanCrdate.append(cnt)
83
84 # get the dependant element
85 artText = new_tag(soup, 'div', [('class', 'text')])
86 textHolderp = crdate.parent
87 textHolder = textHolderp.nextSibling
88 while textHolder and (not isinstance(textHolder, Tag) or (textHolder.name != textHolderp.name)):
89 textHolder = textHolder.nextSibling
90 if textHolder.td:
91 artText = textHolder.td
92 artText.name = 'div'
93 artText.attrs = []
94 artText['class'] = 'text'
95 artText.extract()
96
97 soup.body.contents = []
98
99 soup.body.append(cleanTitle)
100 soup.body.append(author)
101 soup.body.append(cleanCrdate)
102 soup.body.append(artText)
103
104 for item in soup.findAll('a'):
105 limg = item.find('img')
106 if item.string is not None:
107 str = item.string
108 item.replaceWith(str)
109 elif limg:
110 item.name = 'div'
111 item.attrs = []
112 else:
113 str = self.tag_to_string(item)
114 item.replaceWith(str)
115 for item in soup.findAll('img', alt=False):
116 item['alt'] = 'image'
117 return soup