# coding: utf-8 from calibre.web.feeds.recipes import BasicNewsRecipe def classes(classes): q = frozenset(classes.split(' ')) return dict(attrs={ 'class': lambda x: x and frozenset(x.split()).intersection(q)}) class CaravanMagazine(BasicNewsRecipe): title = 'Caravan Magazine' __author__ = 'Kovid Goyal, Gobelinus' description = 'An Indian Journal of politics and culture' language = 'en_IN' timefmt = ' [%b, %Y]' no_stylesheets = True keep_only_tags = [ classes('post-title short-desc author-details cover'), dict(itemprop='articleBody'), ] remove_tags = [ dict(name='meta'), dict(attrs={'class': ['share-with']}), ] # To parse artice toc def parse_index(self): base_url = 'https://www.caravanmagazine.in/' soup = self.index_to_soup('{0}magazine'.format(base_url)) # find current issue cover feeds = [] sections = soup.find(attrs={'class': lambda x: x and 'current-magazine-issue' in x.split()}).find( attrs={'class': lambda x: x and 'sections' in x.split()}) for section in sections.findAll(attrs={'class': lambda x: x and 'section' in x.split()}): a = section.find('a') section_title = self.tag_to_string(a) self.log('\nSection:', section_title) articles = [] for article in section.findAll('article'): details = article.find(attrs={'class': lambda x: x and 'details' in x.split()}) pre = details.find(attrs={'class': lambda x: x and 'pre-heading' in x.split()}) if pre is not None: pre.extract() a = details.find('a') url = base_url + a['href'] title = self.tag_to_string(a) desc = self.tag_to_string(details.find('div')) self.log('\t', title, url) articles.append({'title': title, 'description': desc, 'url': url}) if articles: feeds.append((section_title, articles)) return feeds def preprocess_html(self, soup): for div in soup.findAll(itemprop='image'): for img in div.findAll('img'): img['src'] = div['content'] return soup