| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 4 |
|
| 5 |
from collections import OrderedDict |
| 6 |
|
| 7 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 8 |
|
| 9 |
|
| 10 |
def absurl(x): |
| 11 |
if x.startswith('/'): |
| 12 |
x = 'http://www.epw.in' + x |
| 13 |
return x |
| 14 |
|
| 15 |
|
| 16 |
class EconomicAndPoliticalWeekly(BasicNewsRecipe): |
| 17 |
title = 'Economic and Political Weekly' |
| 18 |
__author__ = 'Kovid Goyal' |
| 19 |
description = 'Economic and Political news from India' |
| 20 |
publisher = 'epw.in' |
| 21 |
category = 'news, finances, politics, India' |
| 22 |
oldest_article = 7 |
| 23 |
max_articles_per_feed = 100 |
| 24 |
no_stylesheets = True |
| 25 |
use_embedded_content = False |
| 26 |
simultaneous_downloads = 1 |
| 27 |
encoding = 'utf-8' |
| 28 |
language = 'en_IN' |
| 29 |
publication_type = 'newspaper' |
| 30 |
masthead_url = 'http://www.epw.in/system/files/epw_masthead.png' |
| 31 |
|
| 32 |
keep_only_tags = [ |
| 33 |
dict(id=['block-system-main', 'page-title']), |
| 34 |
] |
| 35 |
|
| 36 |
def parse_index(self): |
| 37 |
soup = self.index_to_soup('http://www.epw.in/') |
| 38 |
main = soup.find('div', id='main-content') |
| 39 |
sections = OrderedDict() |
| 40 |
current_section = None |
| 41 |
|
| 42 |
for div in main.findAll(attrs={'class': lambda x: x and 'views-field-title' in x.split()}): |
| 43 |
section = self.tag_to_string(div.findParent(attrs={'class': 'block-inner'}).find('h2')) |
| 44 |
if section != current_section: |
| 45 |
current_section = section |
| 46 |
if section not in sections: |
| 47 |
sections[section] = [] |
| 48 |
self.log('\n\n' + section) |
| 49 |
title = self.tag_to_string(div) |
| 50 |
a = div.find('a', href=True) |
| 51 |
url = absurl(a['href']) |
| 52 |
desc = '' |
| 53 |
if a.get('title'): |
| 54 |
desc = a['title'] |
| 55 |
else: |
| 56 |
d = div.findNextSibling(attrs={'class': lambda x: x and 'views-field-body' in x.split()}) |
| 57 |
if d is not None: |
| 58 |
desc = self.tag_to_string(d) |
| 59 |
self.log('\t', title, url) |
| 60 |
self.log('\t\t', desc) |
| 61 |
sections[current_section].append({'title': title, 'url': url, 'description': desc}) |
| 62 |
|
| 63 |
current_section = 'Web Exclusive' |
| 64 |
sections[current_section] = [] |
| 65 |
self.log('\n\n' + current_section) |
| 66 |
for div in main.findAll(attrs={'class': lambda x: x and 'views-field-nothing' in x.split()}): |
| 67 |
title = self.tag_to_string(div) |
| 68 |
elems = div.findAll('a', href=True) |
| 69 |
if len(elems) == 2: |
| 70 |
desc, title = map(self.tag_to_string, elems) |
| 71 |
else: |
| 72 |
title, desc = self.tag_to_string(elems[0]), '' |
| 73 |
url = absurl(elems[-1]['href']) |
| 74 |
self.log('\t', title, url) |
| 75 |
self.log('\t\t', desc) |
| 76 |
sections[current_section].append({'title': title, 'url': url, 'description': desc}) |
| 77 |
|
| 78 |
return [(t, articles) for t, articles in sections.items() if articles] |