#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# The MIT License (MIT)

# Copyright (c) 2015 daite

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from bs4 import BeautifulSoup as BS
from urlparse import urljoin
import requests
import argparse
import setting
import codecs
import os

class JapTextParser:
	def __init__(self, mode='editorial'):
		'''
		:: init function of basic class
		'''
		self.mode = mode
		self.text_encoding = setting.text_encoding
		self.output_file = setting.output_file
		self.dedupe_output_file = setting.dedupe_output_file

	def analyze_japanese_text(self, search_filter=setting.word_filter):
		'''
		:: analyze japanese text when given search_filter
		:: need to add error handling
		'''
		for sentence in self.get_text_from_url():
			url = setting.app_url(setting.app_id, 
			                      search_filter, 
			                      sentence)
			r = requests.get(url)
			status_code = r.status_code
			if status_code == 200:
				print '[%s] ===> [%s] OK' %(self.mode, status_code)
				self.save_text(r.text)
			else:
				raise RuntimeError("check it")
		self.dedupe() # deduping text 

	def get_text_from_url(self):
		'''
		:: get text from url
		'''
		pass

	def dedupe(self):
		'''
		:: dedupe text data
		'''
		print('deduping.....')
		text_list = set()
		with codecs.open(self.output_file, 'r', 
		                 encoding=self.text_encoding) as f:
			for x in f.readlines() : 
				text_list.add(x) 
			for text in text_list:
				with codecs.open(self.dedupe_output_file, 'a', 
				                 encoding=self.text_encoding) as g:
					g.write(text)
		print('cleaning up...')
		os.remove(self.output_file)

	def save_text(self, res_text):
		'''
		:: save useful information to txt file
		:: returned by yahoo japanese analyze server
		'''
		for word in BS(res_text).findAll('word'):
			category = word.find('pos').text
			kanji = word.find('surface').text
			hiragana = word.find('reading').text
			try:
				with codecs.open(self.output_file, 'a', 
				                 encoding=self.text_encoding) as f:
					text = '%s\t%s\t%s' %(category, kanji, hiragana)
					f.write(text + '\r\n')
			except Exception as e:
				os.remove(self.output_file)
				raise RuntimeError("Error", e)
	@staticmethod
	def get_japanese_meaning(kanji):
		'''
		:: get japanese meaning from kotobank
		'''
		url = 'https://kotobank.jp/word/%s' %japanese_word
		try:
			japanese_meaning = BS(requests.get(url).text).\
			find('meta', {'property':'og:description'})['content']
		except:
			japanese_meaning = 'errors!'
		return japanese_meaning

	@staticmethod
	def get_response(url):
		'''
		:: staticmethod -> get BS response from url
		'''
		return BS(requests.get(url).content)

class AsahiParser(JapTextParser):
	'''
	:: AsahiParser class
	'''
	def get_text_from_url(self):
		'''
		:: override function from base class
		'''
		if self.mode == 'editorial': 
			url = setting.asahi_editorial_url 
		else: 
			url = setting.asahi_tensheng_url
		soup = self.get_response(url)
		div_tag = soup.find('div', {'class': 'ArticleText'})
		for p_tag in div_tag.findAll('p'):
			yield p_tag.text

class NikkeiParser(JapTextParser):
	'''
	:: NikkeiParser class
	'''
	def get_text_from_url(self):
		'''
		:: override function from base class
		:: get the lastest 2 editorial pages
		'''
		nikkei_main_url = setting.nikkei_main_url
		soup_main = self.get_response(nikkei_main_url).\
			    findAll('h4', {'class': 'cmn-article_title'})[:2]
		for s in soup_main:
			nikkei_editorial_url = urljoin(setting.nikkei_host_url, s.find('a')['href'])
			soup_editorial = self.get_response(nikkei_editorial_url).\
				         find('div', {'class': 'cmn-article_text JSID_key_fonttxt'})
			for text in soup_editorial.findAll('p'):
				yield text

if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument('-t','--tensheng', 
			    help='fetch asahi tensheng', 
			    action="store_true")
	parser.add_argument('-e','--editorial', 
			     help='fetch asahi editorial', 
			     action="store_true")
	parser.add_argument('-n','--nikkei', 
			     help='fetch nikkei editorial', 
			     action="store_true")
	args = parser.parse_args()
	if args.tensheng:
		a = AsahiParser(mode='tensheng')
		a.analyze_japanese_text()
	elif args.editorial:
		a = AsahiParser()
		a.analyze_japanese_text()
	elif args.nikkei:
		n = NikkeiParser()
		n.analyze_japanese_text()
	else:
		parser.print_help()
		exit(1)
