mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-02-07 11:33:30 -05:00
102 lines
4.5 KiB
Python
102 lines
4.5 KiB
Python
#!/usr/bin/env python
|
|
|
|
__license__ = 'GPL v3'
|
|
__copyright__ = '2008, Kovid Goyal <kovid@kovidgoyal.net>'
|
|
'''
|
|
time.com
|
|
'''
|
|
|
|
import re
|
|
from calibre.web.feeds.news import BasicNewsRecipe
|
|
|
|
class Time(BasicNewsRecipe):
|
|
title = u'Time'
|
|
__author__ = 'Kovid Goyal and Sujata Raman'
|
|
description = 'Weekly magazine'
|
|
encoding = 'utf-8'
|
|
no_stylesheets = True
|
|
language = 'en'
|
|
|
|
extra_css = ''' h1 {font-family:Arial,Sans-serif;}
|
|
h2 {font-family:Arial,Sans-serif;}
|
|
.name{font-family:Arial,Sans-serif; font-size:x-small; }
|
|
.date{font-family:Arial,Sans-serif; font-size:x-small ;color:#999999;}
|
|
.byline{font-family:Arial,Sans-serif; font-size:x-small ;}
|
|
.photoBkt{ font-size:x-small ;}
|
|
.vertPhoto{font-size:x-small ;}
|
|
.credits{font-family:Arial,Sans-serif; font-size:x-small ;color:gray;}
|
|
.artTxt{font-family:georgia,serif;}
|
|
#article{font-family:georgia,serif;}
|
|
.caption{font-family:georgia,serif; font-size:x-small;color:#333333;}
|
|
.credit{font-family:georgia,serif; font-size:x-small;color:#999999;}
|
|
a:link{color:#CC0000;}
|
|
'''
|
|
|
|
# remove_tags_before = dict(id="artHd")
|
|
# remove_tags_after = {'class':"ltCol"}
|
|
# remove_tags = [
|
|
# {'class':['articleTools', 'enlarge', 'search','socialtools','blogtools','moretools','page','nextUp','next','subnav','RSS','line2','first','ybuzz','articlePagination','chiclets','imgcont','createListLink','rlinks','tabsWrap','pagination']},
|
|
# {'id':['quigoArticle', 'contentTools', 'articleSideBar', 'header', 'navTop','articleTools','feedmodule','feedmodule3','promos','footer','linksFooter','timeArchive','belt','relatedStories','packages','Features']},
|
|
# {'target':'_blank'},
|
|
# ]
|
|
|
|
keep_only_tags = [ dict(name ="div",attrs = {"id" :["article",]}) ,
|
|
dict(name ="div",attrs = {"class" :["artHd","artTxt","photoBkt","vertPhoto","image","copy"]}) ,]
|
|
remove_tags = [ dict(name ="div",attrs = {'class':['articlePagination','nextUp',"rtCol","pagination","enlarge",]}),
|
|
dict(name ="span",attrs = {'class':['see']}),
|
|
dict(name ="div",attrs = {'id':['articleSideBar',"articleTools","articleFooter","cmBotLt","quigoPackage"]}),
|
|
dict(name ="a",attrs = {'class':['listLink']}),
|
|
]
|
|
recursions = 1
|
|
match_regexps = [r'/[0-9,]+-(2|3|4|5|6|7|8|9)(,\d+){0,1}.html']
|
|
|
|
|
|
def parse_index(self):
|
|
soup = self.index_to_soup('http://www.time.com/time/magazine')
|
|
img = soup.find('a', title="View Large Cover", href=True)
|
|
if img is not None:
|
|
cover_url = 'http://www.time.com'+img['href']
|
|
try:
|
|
nsoup = self.index_to_soup(cover_url)
|
|
img = nsoup.find('img', src=re.compile('archive/covers'))
|
|
if img is not None:
|
|
self.cover_url = img['src']
|
|
except:
|
|
self.log.exception('Failed to fetch cover')
|
|
|
|
|
|
feeds = []
|
|
parent = soup.find(id='tocGuts')
|
|
for seched in parent.findAll(attrs={'class':'toc_seched'}):
|
|
section = self.tag_to_string(seched).capitalize()
|
|
articles = list(self.find_articles(seched))
|
|
feeds.append((section, articles))
|
|
|
|
return feeds
|
|
|
|
def find_articles(self, seched):
|
|
for a in seched.findNextSiblings('a', href=True, attrs={'class':'toc_hed'}):
|
|
yield {
|
|
'title' : self.tag_to_string(a),
|
|
'url' : 'http://www.time.com'+a['href'],
|
|
'date' : '',
|
|
'description' : self.article_description(a)
|
|
}
|
|
|
|
def article_description(self, a):
|
|
ans = []
|
|
while True:
|
|
t = a.nextSibling
|
|
if t is None:
|
|
break
|
|
a = t
|
|
if getattr(t, 'name', False):
|
|
if t.get('class', '') == 'toc_parens' or t.name == 'br':
|
|
continue
|
|
if t.name in ('div', 'a'):
|
|
break
|
|
ans.append(self.tag_to_string(t))
|
|
else:
|
|
ans.append(unicode(t))
|
|
return u' '.join(ans).replace(u'\xa0', u'').strip()
|