#!/usr/bin/env python
#-*- coding: UTF-8 -*-
# Filename: Lyrics_CN
# Version:0.5.1
# Author: Xu Jia (Sanfanling)   E-mail:xujia19@sohu.com
# Lisence: GPL-2.0


import commands,os,sys,re,urllib,baidu,sohu,yahoo,locale,english,s2t

configFilePath = '%s/.kde/share/apps/amarok/scripts-data/' %os.getenv('HOME')
#scriptPath = '%s/.kde/share/apps/amarok/scripts/' %os.getenv('HOME')

def configDialog():
	desktopInfo = os.system('which kdialog')
	if(desktopInfo==0):
		languageType = commands.getoutput('kdialog --radiolist "Choose language" simplified "Simplified Chinese" on traditional "Traditional Chinese" off')
	else:
		languageType = ''
		amarokMessage1 = 'Can\'t find kdialog in your system, script configurate for you automatically. You also could configurate by modifying ~/.kde/share/apps/amarok/scripts-data/Lyrics_CN.conf'
		os.system('dcop amarok playlist popupMessage "%s"' %amarokMessage1)
	if(languageType==''):
		languageInfo = locale.getdefaultlocale()[0]
		if(re.search('zh_cn',languageInfo,re.I)):
			languageType = 'simplified'
		elif(re.search('zh_tw',languageInfo,re.I)):
			languageType = 'traditional'
		elif(re.search('zh_hk',languageInfo,re.I)):
			languageType = 'traditional'
		else:
			languageType = 'simplified'
	configFile = open('%s/Lyrics_CN.conf' %configFilePath,'w')
	configFile.write(languageType+'\n')
	configFile.close()
	return languageType

if(os.path.isfile('%s/Lyrics_CN.conf' %configFilePath)):
	configFile = open('%s/Lyrics_CN.conf' %configFilePath,'r')
	amarokMessage2 = 'Reading config file failed, please configurate script again.'
	try:
		languageType = configFile.readline().strip()
	except:
		configFile.close()
		languageType = configDialog()
		os.system('dcop amarok playlist popupMessage "%s"' %amarokMessage2)
	else:
		configFile.close()
		if(languageType not in ['simplified','traditional']):
			languageType = configDialog()
			os.system('dcop amarok playlist popupMessage "%s"' %amarokMessage2)
		else:
			pass
else:
	languageType = configDialog()
	

localeInfo = locale.getdefaultlocale()[1]
if(re.search('utf',localeInfo,re.I)):
	localeType = 'utf8'
elif(re.search('gb',localeInfo,re.I)):
	localeType = 'gb18030'
elif(re.search('euccn',localeInfo,re.I)):
	localeType = 'gb18030'
elif(re.search('big',localeInfo,re.I)):
	localeType = 'big5'

while True:
	title = commands.getoutput('dcop amarok player title')
	artist = commands.getoutput('dcop amarok player artist')
	originalLyrics = commands.getoutput('dcop amarok player lyrics')
	if(originalLyrics==''):
		switch = True
	else:
		switch = False
	
	while True:
	

		amarokSignal = sys.stdin.readline().strip()
		
		if(re.search('configure',amarokSignal)):
			languageType = configDialog()
			
		elif((re.search('fetchLyrics ',amarokSignal) and switch)):
			switch = False
			p = baidu.baidu(title,artist,localeType,languageType)
			p.showLyrics()
			
		
		elif((re.search('fetchLyrics ',amarokSignal) and not switch)):
			nowLyrics = commands.getoutput('dcop amarok player lyrics')
			if(nowLyrics==''):
				suggestion='''<suggestions page_url='http://mp3.baidu.com'>
				<suggestion artist='from' title='baidu' url='www.baidu.com' />
				<suggestion artist='from' title='sohu' url='www.sohu.com' />
				<suggestion artist='from' title='yahoo' url='www.yahoo.com.cn' />
				<suggestion artist='from' title='lyrc.com.ar' url='http://lyrc.com.ar' />
				</suggestions>'''
				command='dcop amarok contextbrowser showLyrics "%s"' %suggestion
				os.system(command)
			else:
				suggestion='''<suggestions page_url='http://mp3.baidu.com'>
				<suggestion artist='from' title='baidu' url='www.baidu.com' />
				<suggestion artist='from' title='sohu' url='www.sohu.com' />
				<suggestion artist='from' title='yahoo' url='www.yahoo.com.cn' />
				<suggestion artist='from' title='lyrc.com.ar' url='http://lyrc.com.ar' />
				<suggestion artist='simplified' title='>traditinal' url='http://www.ip138.com/gb.htm' />
				</suggestions>'''
				command='dcop amarok contextbrowser showLyrics "%s"' %suggestion
				os.system(command)
		
		elif(re.search('www.baidu.com',amarokSignal)):
			p = baidu.baidu(title,artist,localeType,languageType)
			p.showLyrics()
			
		elif(re.search('www.sohu.com',amarokSignal)):
			p = sohu.sohu(title,artist,localeType,languageType)
			p.showLyrics()
		
		elif(re.search('www.yahoo.com.cn',amarokSignal)):
			p = yahoo.yahoo(title,artist,localeType,languageType)
			p.showLyrics()
		
		elif(re.search('lyrc.com.ar',amarokSignal) and not re.search('tema1en', amarokSignal)):
			p = english.english(title,artist)
			p.showLyricsEnglish()
			
		elif(re.search('fetchLyricsByUrl',amarokSignal) and re.search('http://lyrc.com.ar/',amarokSignal)):
			url = amarokSignal.split()[1]
			p.getlyrics_second_english(url)
		
		elif(re.search('http://www.ip138.com/gb.htm',amarokSignal)):
			#简繁转换
			l = s2t.s2t(unicode(commands.getoutput('dcop amarok player lyrics'),localeType).encode('utf8'))
			transferLyrics = l.transfer()
			command='dcop amarok contextbrowser showLyrics "%s"' %transferLyrics
			os.system(command)
		
		elif(re.search('engineStateChange',amarokSignal)):
			break