| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 6 |
|
| 7 |
from collections import defaultdict |
| 8 |
|
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 10 |
|
| 11 |
|
| 12 |
def classes(classes): |
| 13 |
q = frozenset(classes.split(' ')) |
| 14 |
return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)}) |
| 15 |
|
| 16 |
|
| 17 |
class ThePrint(BasicNewsRecipe): |
| 18 |
title = 'The Print' |
| 19 |
__author__ = 'Kovid Goyal' |
| 20 |
description = 'News and current affairs in India' |
| 21 |
language = 'en_IN' |
| 22 |
|
| 23 |
no_stylesheets = True |
| 24 |
ignore_duplicate_articles = {'url'} |
| 25 |
remove_attributes = ['style'] |
| 26 |
|
| 27 |
keep_only_tags = [classes('td-post-title td-post-content')] |
| 28 |
|
| 29 |
remove_tags = [ |
| 30 |
dict(name='button'), |
| 31 |
classes('fontsize_Btn postBtm'), |
| 32 |
] |
| 33 |
|
| 34 |
def parse_index(self): |
| 35 |
soup = self.index_to_soup('https://theprint.in/') |
| 36 |
current_section = 'Unknown' |
| 37 |
ans = defaultdict(list) |
| 38 |
secs = [] |
| 39 |
for x in soup.findAll(['h3', 'h4'], attrs={'class': True}): |
| 40 |
cls = x['class'] |
| 41 |
if 'td-block-title' in cls: |
| 42 |
current_section = self.tag_to_string(x) |
| 43 |
secs.append(current_section) |
| 44 |
self.log(current_section) |
| 45 |
elif 'entry-title' in cls: |
| 46 |
h3 = x |
| 47 |
title = self.tag_to_string(h3) |
| 48 |
url = h3.find('a')['href'] |
| 49 |
desc = '' |
| 50 |
dnode = h3.parent.find(**classes('td-excerpt')) |
| 51 |
if dnode: |
| 52 |
desc = self.tag_to_string(dnode) |
| 53 |
self.log('\t', title) |
| 54 |
ans[current_section].append({'title': title, 'url': url, 'description': desc}) |
| 55 |
ret = [] |
| 56 |
for sec in secs: |
| 57 |
if sec == 'On Camera': |
| 58 |
continue |
| 59 |
arts = ans[sec] |
| 60 |
if arts: |
| 61 |
ret.append((sec, arts)) |
| 62 |
return ret |