Update General Knowledge Today

This commit is contained in:
Kovid Goyal 2021-12-31 17:23:19 +05:30
parent 21ee73763a
commit 422258bcce
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,4 +1,4 @@
from calibre.web.feeds.news import BasicNewsRecipe from calibre.web.feeds.news import BasicNewsRecipe, classes
class GKT(BasicNewsRecipe): class GKT(BasicNewsRecipe):
@ -12,33 +12,36 @@ class GKT(BasicNewsRecipe):
no_javascript = True no_javascript = True
auto_cleanup = True auto_cleanup = True
def parse_gkt_section(self, url, ignore_error=False):
try:
root = self.index_to_soup(url, as_tree=True)
except Exception:
if ignore_error:
return
raise
for a in root.xpath('//div[@class="posts-listing"]/h1/a[@href]'):
title = self.tag_to_string(a).strip()
url = a.get('href')
if title and url:
self.log('\tFound article:', title, 'at', url)
yield {'title': title, 'url': url}
def parse_index(self): def parse_index(self):
url = 'http://www.gktoday.in/' securl = 'https://www.gktoday.in/current-affairs/'
root = self.index_to_soup(url, as_tree=True) ans = {}
ans = []
h3 = root.xpath('//h3[@class="widget-title"]')[1] def p_tags(h1):
for a in h3.getparent().xpath('descendant::li/a[@href]'): for sib in h1.next_siblings:
category = self.tag_to_string(a).strip() if sib.name == 'h1':
if 'PDF' in category or not category: break
if sib.name == 'p':
yield sib
def find_cat(ps):
for p in ps:
for a in p.findAll('a', rel='tag'):
return self.tag_to_string(a)
for i in range(1, 6):
page = '' if i == 1 else 'page/' + str(i)
self.log('Trying:', securl + page)
soup = self.index_to_soup(securl + page)
container = soup.find(**classes('left_middle_content'))
for h1 in container.findAll('h1'):
title = self.tag_to_string(h1)
a = h1.find('a')
if a is None:
continue continue
url = a.get('href') url = a['href']
self.log('Found section:', category, 'at', url) ps = tuple(p_tags(h1))
articles = list(self.parse_gkt_section(url)) + \ category = find_cat(ps) or 'Unknown'
list(self.parse_gkt_section(url + '/page/2', ignore_error=True)) ans.setdefault(category, []).append({
if articles: 'title': title, 'url': url, 'description': self.tag_to_string(ps[0])})
ans.append((category, articles)) self.log('\t' + title + ' ' + url)
return ans return list(ans.items())