# coding: utf-8
import os
import sys
include_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'include')
sys.path.append(include_dir)

import logging
import common
from lyric_base import LyricBase

site_class = 'UtaMap'
site_index = 'utamap'
site_keyword = 'utamap'
site_url = 'http://www.utamap.com/'
test_url = 'http://www.utamap.com/showkasi.php?surl=70380'

class UtaMap(LyricBase):
    def parse_page(self):
        url = self.url

        if not self.find_lyric(url):
            logging.info('Failed to get lyric of url [%s]', url)
            return False

        if not self.find_song_info(url):
            logging.info('Failed to get song info of url [%s]', url)

        return True

    def find_lyric(self, url):
        pattern = 'surl=([^&=]+)'

        song_id = common.get_first_group_by_pattern(url, pattern)

        if not song_id:
            logging.error('Failed to get id of url [%s]', url)
            return False

        song_url = 'http://www.utamap.com/phpflash/flashfalsephp.php?unum=' + song_id
        data = common.get_url_content(song_url)
        if not data:
            logging.error('Failed to get content of url [%s]', song_url)
            return False

        prefix = 'test2='
        pos = data.find(prefix)
        if pos == -1:
            logging.error('Failed to find lyric position of url [%s]', url)
            return False

        lyric = data[pos + len(prefix):]
        lyric = lyric.decode('utf-8').strip()

        # test for half to full
        lyric = common.half2full(lyric)

        self.lyric = lyric

        return True

    def find_song_info(self, url):
        ret = True
        html = common.get_url_content(url)

        encoding = 'sjis'
        encoding2 = 'euc_jp'
        html = html.decode(encoding, 'ignore')

        if html.find(u'無料検索') == -1:
            html = html.decode(encoding2, 'ignore')

        patterns = {
            'title': 'title',
            'artist': 'artist',
            'lyricist': 'sakusi',
            'composer': 'sakyoku',
        }

        for key in patterns:
            key_for_pattern  = patterns[key]

            pattern = u'<INPUT type="hidden" name=%s value="([^"]*)">' % (key_for_pattern, )
            value = common.get_first_group_by_pattern(html, pattern)

            if not value:
                logging.info('Failed to get %s of url [%s]', key, url)
                ret = False
            else:
                value = common.htmlspecialchars_decode(value).strip()
                setattr(self, key, value)

        return ret

def get_lyric(url):
    obj = UtaMap(url)

    return obj.get()

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)

    url = test_url
    url = 'http://www.utamap.com/showkasi.php?surl=k-131226-001'

    full = get_lyric(url)
    if not full:
        print('Failed to get lyric')
        exit()
    print(full.encode('utf-8', 'ignore'))

@@ -1665,20 +1665,19 @@
         
-html
+raw
  = commo
@@ -1724,12 +1724,14 @@
  = '
-sjis
+euc_jp
 '%0A  
@@ -1745,30 +1745,28 @@
 ncoding2 = '
-euc_jp
+sjis
 '%0A        ht
@@ -1762,36 +1762,35 @@
 %0A        html = 
-html
+raw
 .decode(encoding
@@ -1829,12 +1829,13 @@
 d(u'
-%E7%84%A1%E6%96%99%E6%A4%9C%E7%B4%A2
+%E3%81%86%E3%81%9F%E3%81%BE%E3%81%A3%E3%81%B7
 ') =
@@ -1856,19 +1856,71 @@
     
-html = html
+# failed to decode, try another encoding%0A            html = raw
 .dec

fix utamap encoding code