mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Get rid of cssselect from jsnews
This commit is contained in:
parent
f2d44f286b
commit
2dcde3f263
@ -3,14 +3,6 @@ __license__ = 'GPL v3'
|
|||||||
|
|
||||||
from calibre.web.feeds.news import BasicNewsRecipe
|
from calibre.web.feeds.news import BasicNewsRecipe
|
||||||
|
|
||||||
try:
|
|
||||||
from calibre.web.feeds.jsnews import CSSSelect
|
|
||||||
except ImportError:
|
|
||||||
def CSSSelect(expr):
|
|
||||||
from cssselect import HTMLTranslator
|
|
||||||
from lxml.etree import XPath
|
|
||||||
return XPath(HTMLTranslator().css_to_xpath(expr))
|
|
||||||
|
|
||||||
def absurl(url):
|
def absurl(url):
|
||||||
if url.startswith('/'):
|
if url.startswith('/'):
|
||||||
url = 'http://www.scientificamerican.com' + url
|
url = 'http://www.scientificamerican.com' + url
|
||||||
@ -46,8 +38,8 @@ class ScientificAmerican(BasicNewsRecipe):
|
|||||||
def parse_index(self):
|
def parse_index(self):
|
||||||
# Get the cover, date and issue URL
|
# Get the cover, date and issue URL
|
||||||
root = self.index_to_soup('http://www.scientificamerican.com/sciammag/', as_tree=True)
|
root = self.index_to_soup('http://www.scientificamerican.com/sciammag/', as_tree=True)
|
||||||
for a in CSSSelect('.archiveIssues a.cover[href]')(root):
|
for a in root.xpath('''descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' archiveIssues ')]/descendant-or-self::*/a[@class and contains(concat(' ', normalize-space(@class), ' '), ' cover ') and (@href)]'''):
|
||||||
self.cover_url = absurl(CSSSelect('img[src]')(a)[0].get('src'))
|
self.cover_url = absurl(a.xpath('descendant-or-self::img[@src]')[0].get('src'))
|
||||||
root = self.index_to_soup(absurl(a.get('href')), as_tree=True)
|
root = self.index_to_soup(absurl(a.get('href')), as_tree=True)
|
||||||
for a in a.xpath('following-sibling::a[@href]'):
|
for a in a.xpath('following-sibling::a[@href]'):
|
||||||
self.timefmt = self.tag_to_string(a).strip()
|
self.timefmt = self.tag_to_string(a).strip()
|
||||||
@ -58,7 +50,7 @@ class ScientificAmerican(BasicNewsRecipe):
|
|||||||
|
|
||||||
# Now parse the actual issue to get the list of articles
|
# Now parse the actual issue to get the list of articles
|
||||||
feeds = []
|
feeds = []
|
||||||
for i, div in enumerate(CSSSelect('div.toc-features, div.toc-departments')(root)):
|
for i, div in enumerate(root.xpath('''descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' toc-features ')] | descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' toc-departments ')]''')):
|
||||||
if i == 0:
|
if i == 0:
|
||||||
feeds.append(('Features', list(self.parse_sciam_features(div))))
|
feeds.append(('Features', list(self.parse_sciam_features(div))))
|
||||||
else:
|
else:
|
||||||
@ -67,7 +59,7 @@ class ScientificAmerican(BasicNewsRecipe):
|
|||||||
return feeds
|
return feeds
|
||||||
|
|
||||||
def parse_sciam_features(self, div):
|
def parse_sciam_features(self, div):
|
||||||
for h4 in CSSSelect('li a[href] h4')(div):
|
for h4 in div.xpath('''descendant-or-self::li/descendant-or-self::*/a[@href]/descendant-or-self::*/h4'''):
|
||||||
title = self.tag_to_string(h4)
|
title = self.tag_to_string(h4)
|
||||||
a = h4.getparent()
|
a = h4.getparent()
|
||||||
url = absurl(a.get('href'))
|
url = absurl(a.get('href'))
|
||||||
@ -81,7 +73,7 @@ class ScientificAmerican(BasicNewsRecipe):
|
|||||||
|
|
||||||
def parse_sciam_departments(self, div):
|
def parse_sciam_departments(self, div):
|
||||||
section_title, articles = 'Unknown', []
|
section_title, articles = 'Unknown', []
|
||||||
for x in CSSSelect('li a[href] h3, li span.deptTitle a[href]')(div):
|
for x in div.xpath('''descendant-or-self::li/descendant-or-self::*/a[@href]/descendant-or-self::*/h3 | descendant-or-self::li/descendant-or-self::*/span[@class and contains(concat(' ', normalize-space(@class), ' '), ' deptTitle ')]/descendant-or-self::*/a[@href]'''):
|
||||||
if x.tag == 'a':
|
if x.tag == 'a':
|
||||||
if articles:
|
if articles:
|
||||||
yield section_title, list(articles)
|
yield section_title, list(articles)
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
from calibre.web.feeds.jsnews import JavascriptRecipe
|
from calibre.web.feeds.jsnews import JavascriptRecipe
|
||||||
from calibre.web.jsbrowser.browser import NotAFile
|
from calibre.web.jsbrowser.browser import NotAFile
|
||||||
|
|
||||||
try:
|
def CSSSelect(expr):
|
||||||
from calibre.web.feeds.jsnews import CSSSelect
|
expr = {
|
||||||
except ImportError:
|
'div.whatsNews-simple': '''descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-simple ')]''',
|
||||||
def CSSSelect(expr):
|
'a.mjLinkItem[href]': '''descendant-or-self::a[@class and contains(concat(' ', normalize-space(@class), ' '), ' mjLinkItem ') and (@href)]''',
|
||||||
from cssselect import HTMLTranslator
|
'.meta_sectionName': '''descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' meta_sectionName ')]''',
|
||||||
|
'p':'descendant-or-self::p',
|
||||||
|
'div.whatsNews-simple.whatsNews-itp': '''descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-simple ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-itp '))]''',
|
||||||
|
'a[href]': 'descendant-or-self::a[@href]',
|
||||||
|
'span.date-date':"descendant-or-self::span[@class and contains(concat(' ', normalize-space(@class), ' '), ' date-date ')]",
|
||||||
|
'div.itpSectionHeaderPdf a[href]': "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' itpSectionHeaderPdf ')]/descendant-or-self::*/a[@href]",
|
||||||
|
'div.itpHeader ul.tab a[href]': "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' itpHeader ')]/descendant-or-self::*/ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' tab ')]/descendant-or-self::*/a[@href]",
|
||||||
|
|
||||||
|
}[expr]
|
||||||
from lxml.etree import XPath
|
from lxml.etree import XPath
|
||||||
return XPath(HTMLTranslator().css_to_xpath(expr))
|
return XPath(expr)
|
||||||
|
|
||||||
|
|
||||||
class WSJ(JavascriptRecipe):
|
class WSJ(JavascriptRecipe):
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
from calibre.web.feeds.jsnews import JavascriptRecipe
|
from calibre.web.feeds.jsnews import JavascriptRecipe
|
||||||
from calibre.web.jsbrowser.browser import NotAFile
|
from calibre.web.jsbrowser.browser import NotAFile
|
||||||
|
|
||||||
try:
|
def CSSSelect(expr):
|
||||||
from calibre.web.feeds.jsnews import CSSSelect
|
expr = {
|
||||||
except ImportError:
|
'div.whatsNews-simple': '''descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-simple ')]''',
|
||||||
def CSSSelect(expr):
|
'a.mjLinkItem[href]': '''descendant-or-self::a[@class and contains(concat(' ', normalize-space(@class), ' '), ' mjLinkItem ') and (@href)]''',
|
||||||
from cssselect import HTMLTranslator
|
'.meta_sectionName': '''descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' meta_sectionName ')]''',
|
||||||
|
'p':'descendant-or-self::p',
|
||||||
|
'div.whatsNews-simple.whatsNews-itp': '''descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-simple ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' whatsNews-itp '))]''',
|
||||||
|
'a[href]': 'descendant-or-self::a[@href]',
|
||||||
|
'span.date-date':"descendant-or-self::span[@class and contains(concat(' ', normalize-space(@class), ' '), ' date-date ')]",
|
||||||
|
'div.itpSectionHeaderPdf a[href]': "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' itpSectionHeaderPdf ')]/descendant-or-self::*/a[@href]",
|
||||||
|
'div.itpHeader ul.tab a[href]': "descendant-or-self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' itpHeader ')]/descendant-or-self::*/ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' tab ')]/descendant-or-self::*/a[@href]",
|
||||||
|
|
||||||
|
}[expr]
|
||||||
from lxml.etree import XPath
|
from lxml.etree import XPath
|
||||||
return XPath(HTMLTranslator().css_to_xpath(expr))
|
return XPath(expr)
|
||||||
|
|
||||||
|
|
||||||
class WSJ(JavascriptRecipe):
|
class WSJ(JavascriptRecipe):
|
||||||
|
@ -30,17 +30,6 @@ def image_data_to_url(data, base='cover'):
|
|||||||
ans.name = 'cover.' + ext
|
ans.name = 'cover.' + ext
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
css_select_cache = {}
|
|
||||||
|
|
||||||
def CSSSelect(expr):
|
|
||||||
try:
|
|
||||||
return css_select_cache[expr]
|
|
||||||
except KeyError:
|
|
||||||
from cssselect import HTMLTranslator
|
|
||||||
from lxml.etree import XPath
|
|
||||||
ans = css_select_cache[expr] = XPath(HTMLTranslator().css_to_xpath(expr))
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
class JavascriptRecipe(BasicNewsRecipe):
|
class JavascriptRecipe(BasicNewsRecipe):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user