| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
from __future__ import print_function |
| 4 |
|
| 5 |
import re |
| 6 |
import unicodedata |
| 7 |
|
| 8 |
from calibre.web.feeds.recipes import BasicNewsRecipe |
| 9 |
|
| 10 |
# import pprint, sys |
| 11 |
# pp = pprint.PrettyPrinter(indent=4) |
| 12 |
|
| 13 |
|
| 14 |
class NikkeiNet_paper_subscription(BasicNewsRecipe): |
| 15 |
title = '\u65e5\u672c\u7d4c\u6e08\u65b0\u805e\uff08\u671d\u520a\u30fb\u5915\u520a\uff09' |
| 16 |
__author__ = 'Ado Nishimura' |
| 17 |
description = '\u65e5\u7d4c\u96fb\u5b50\u7248\u306b\u3088\u308b\u65e5\u672c\u7d4c\u6e08\u65b0\u805e\u3002\u671d\u520a\u30fb\u5915\u520a\u306f\u53d6\u5f97\u6642\u9593\u306b\u3088\u308a\u5207\u308a\u66ff\u308f\u308a\u307e\u3059\u3002\u8981\u8cfc\u8aad' # noqa: E501 |
| 18 |
needs_subscription = True |
| 19 |
oldest_article = 1 |
| 20 |
max_articles_per_feed = 30 |
| 21 |
language = 'ja' |
| 22 |
no_stylesheets = True |
| 23 |
# cover_url = 'http://parts.nikkei.com/parts/ds/images/common/logo_r1.svg' |
| 24 |
cover_url = 'http://cdn.nikkei.co.jp/parts/ds/images/common/st_nikkei_r1_20101003_1.gif' |
| 25 |
# masthead_url = 'http://parts.nikkei.com/parts/ds/images/common/logo_r1.svg' |
| 26 |
masthead_url = 'http://cdn.nikkei.co.jp/parts/ds/images/common/st_nikkei_r1_20101003_1.gif' |
| 27 |
cover_margins = (10, 188, '#ffffff') |
| 28 |
|
| 29 |
remove_tags_before = {'class': 'cmn-indent'} |
| 30 |
remove_tags = [ |
| 31 |
# {'class':"cmn-article_move"}, |
| 32 |
# {'class':"cmn-pr_list"}, |
| 33 |
# {'class':"cmnc-zoom"}, |
| 34 |
{'class': 'cmn-hide'}, |
| 35 |
{'name': 'form'}, |
| 36 |
{'class': 'cmn-print_headline cmn-clearfix'}, |
| 37 |
{'id': 'ABOUT_NIKKEI'}, |
| 38 |
] |
| 39 |
remove_tags_after = {'class': 'cmn-indent'} |
| 40 |
|
| 41 |
def get_browser(self): |
| 42 |
br = BasicNewsRecipe.get_browser(self) |
| 43 |
|
| 44 |
if self.username is not None and self.password is not None: |
| 45 |
print('-------------------------open top page-------------------------------------') |
| 46 |
br.open('http://www.nikkei.com/') |
| 47 |
print('-------------------------open first login form-----------------------------') |
| 48 |
try: |
| 49 |
url = list(br.links(url_regex='www.nikkei.com/etc/accounts/login'))[0].url |
| 50 |
except IndexError: |
| 51 |
print('Found IndexError') |
| 52 |
url = 'http://www.nikkei.com/etc/accounts/login?dps=3&pageflag=top&url=http%3A%2F%2Fwww.nikkei.com%2F' |
| 53 |
except StopIteration: |
| 54 |
url = 'http://www.nikkei.com/etc/accounts/login?dps=3&pageflag=top&url=http%3A%2F%2Fwww.nikkei.com%2F' |
| 55 |
br.open(url) |
| 56 |
print('-------------------------JS redirect(send autoPostForm)--------------------') |
| 57 |
br.select_form(name='autoPostForm') |
| 58 |
br.submit() |
| 59 |
# response = br.response() |
| 60 |
print('-------------------------got login form------------------------------------') |
| 61 |
br.select_form(name='LA7010Form01') |
| 62 |
br['LA7010Form01:LA7010Email'] = self.username |
| 63 |
br['LA7010Form01:LA7010Password'] = self.password |
| 64 |
br.submit(id='LA7010Form01:submitBtn') |
| 65 |
print('-------------------------JS redirect---------------------------------------') |
| 66 |
br.select_form(nr=0) |
| 67 |
br.submit() |
| 68 |
|
| 69 |
return br |
| 70 |
|
| 71 |
def cleanup(self): |
| 72 |
print('-------------------------logout--------------------------------------------') |
| 73 |
self.browser.open('https://regist.nikkei.com/ds/etc/accounts/logout') |
| 74 |
|
| 75 |
def parse_index(self): |
| 76 |
print('-------------------------get index of paper--------------------------------') |
| 77 |
result = [] |
| 78 |
soup = self.index_to_soup('http://www.nikkei.com/paper/') |
| 79 |
sections = soup.findAll(attrs={'class': re.compile(r'.*cmn-article_title.*')}) |
| 80 |
|
| 81 |
for sect in sections: |
| 82 |
sect_title = sect.find(attrs={'class': re.compile(r'.*cmnc-((large)|(middle)|(small)).*')}) |
| 83 |
if sect_title is None: |
| 84 |
continue |
| 85 |
sect_title = sect_title.contents[0] |
| 86 |
sect_result = [] |
| 87 |
url = sect.a['href'] |
| 88 |
url = url.replace('/article/', '/print-article/') |
| 89 |
url = 'http://www.nikkei.com' + url |
| 90 |
sect_result.append(dict(title=sect_title, url=url, date='', description='', content='')) |
| 91 |
result.append([sect_title, sect_result]) |
| 92 |
return result |
| 93 |
|
| 94 |
def populate_article_metadata(self, article, soup, first): |
| 95 |
try: |
| 96 |
elms = soup.findAll('div', {'class': 'cmn-article_text JSID_key_fonttxt'}) |
| 97 |
elm_text = '◆'.join([self.tag_to_string(elm).strip() for elm in elms]) |
| 98 |
elm_text = unicodedata.normalize('NFKC', elm_text) |
| 99 |
article.summary = article.text_summary = elm_text |
| 100 |
except Exception: |
| 101 |
self.log('Error: Failed to get article summary.') |
| 102 |
return |