#  Naver Search Workflow for Alfred 2
#  Copyright (C) 2014  Jinuk Baek
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


import sys

from workflow import web, Workflow


def get_dictionary_data(word):
	url = 'http://frdic.naver.com/ac'
	params = dict(st=111, r_lt=111, n_kofrdic=111, q=word)


	r = web.get(url, params)
	r.raise_for_status()
	return r.json()


def main(wf):
	import cgi;

	args = wf.args[0]

	wf.add_item(title = 'Search Naver Frdic for \'%s\'' % args, 
				autocomplete=args, 
				arg=args,
				valid=True)

	def wrapper():
		return get_dictionary_data(args)

	res_json = wf.cached_data("fr_%s" % args, wrapper, max_age=600)

	for item in res_json['items']:
		for ltxt in item:
			if len(ltxt) > 0:
				txt = ltxt[0][0];
				rtxt = cgi.escape(ltxt[1][0]);

				wf.add_item(title = u"%s     %s" % (txt, rtxt) ,
							subtitle = 'Search Naver Frdic for \'%s\'' % txt, 
							autocomplete=txt, 
							arg=txt,
							valid=True);

	wf.send_feedback()
				


if __name__ == '__main__':
	wf = Workflow()
	sys.exit(wf.run(main))



add a french-korean dictionary by Naver