# Path: lyric_engine/modules/yahoo.py
# class Yahoo(LyricBase):
#     def parse_page(self):
#         url = self.url
# 
#         query = self.get_xml_parameters(url)
#         if not query:
#             logging.info('Failed to get query of url [%s]', url)
#             return False
# 
#         logging.debug('XML parameters, query[%s]', query)
# 
#         # 3. get xml
#         xml_str = self.get_xml(query)
#         if not xml_str:
#             logging.info('Failed to get xml of url [%s]', url)
#             return False
# 
#         # 4. parse xml
#         if not self.parse_xml(xml_str):
#             logging.info('Failed to parse xml of url [%s]', url)
#             return False
# 
#         return True
# 
#     def get_xml_parameters(self, url):
#         r = requests.get(url)
#         r.encoding = 'utf-8'
#         raw = r.text
# 
#         pattern = "query +: +'([^']+)'"
#         query = common.get_first_group_by_pattern(raw, pattern)
# 
#         return query
# 
#     def get_xml(self, query):
#         # http://rio.yahooapis.jp/RioWebService/V2/getLyrics?appid=7vOgnk6xg64IDggn6YEl3IQxmbj1qqkQzTpAx5nGwl9HnfPX3tZksE.oYhEw3zA-&lyrics_id=Y152097&results=1&multi_htmlspecialchars_flag=1
#         xmlpath = 'http://rio.yahooapis.jp/RioWebService/V2/getLyrics?appid=%s&%s' % (
#             '7vOgnk6xg64IDggn6YEl3IQxmbj1qqkQzTpAx5nGwl9HnfPX3tZksE.oYhEw3zA-', unquote(
#                 query)
#         )
# 
#         r = requests.get(xmlpath)
#         r.encoding = 'utf-8'
#         raw = r.text
# 
#         return raw
# 
#     def xml_get_name_attribute(self, doc, tagname):
#         ret = None
#         elements = doc.getElementsByTagName(tagname)
#         if elements.length == 1:
#             ret = elements.item(0).getAttribute('name')
#         return ret
# 
#     def xml_get_first_child(self, doc, tagname):
#         ret = None
#         elements = doc.getElementsByTagName(tagname)
#         if elements.length == 1:
#             ret = elements.item(0).firstChild.nodeValue
#         return ret
# 
#     def parse_xml(self, xml_str):
#         xml_str = xml_str.encode('utf-8')
#         doc = parseString(xml_str)
# 
#         def get_lyric(doc):
#             lyric = self.xml_get_first_child(doc, 'Lyrics')
#             lyric = str(lyric)
#             lyric = lyric.replace('<br>', '\n')
# 
#             return lyric
# 
#         self.lyric = get_lyric(doc)
#         self.find_song_info(doc)
# 
#         return True
# 
#     def find_song_info(self, doc):
#         ret = True
# 
#         patterns = {
#             'title': 'Title',
#             'artist': 'ArtistName',
#             'lyricist': 'WriterName',
#             'composer': 'ComposerName',
#         }
# 
#         for key in patterns:
#             tag = patterns[key]
# 
#             value = self.xml_get_first_child(doc, tag)
# 
#             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


import unittest
from lyric_engine.modules.yahoo import Yahoo as Lyric

class UtaMapTest(unittest.TestCase):
    def test_url_one_more(self):
        url = 'http://lyrics.gyao.yahoo.co.jp/ly/Y004402/'
        obj = Lyric(url)
        obj.parse()

        self.assertEqual(obj.title, '帰って来たヨッパライ')
        self.assertEqual(obj.artist, 'ザ・フォーク・クルセダーズ')
        self.assertEqual(obj.lyricist, 'ザ･フォーク･パロディ･ギャング')
        self.assertEqual(obj.composer, '加藤和彦')
        self.assertEqual(len(obj.lyric), 495)

    def test_url_02(self):
        url = 'http://lyrics.gyao.yahoo.co.jp/ly/Y160641/'

# Path: lyric_engine/modules/yahoo.py
# class Yahoo(LyricBase):
#     def parse_page(self):
#         url = self.url
# 
#         query = self.get_xml_parameters(url)
#         if not query:
#             logging.info('Failed to get query of url [%s]', url)
#             return False
# 
#         logging.debug('XML parameters, query[%s]', query)
# 
#         # 3. get xml
#         xml_str = self.get_xml(query)
#         if not xml_str:
#             logging.info('Failed to get xml of url [%s]', url)
#             return False
# 
#         # 4. parse xml
#         if not self.parse_xml(xml_str):
#             logging.info('Failed to parse xml of url [%s]', url)
#             return False
# 
#         return True
# 
#     def get_xml_parameters(self, url):
#         r = requests.get(url)
#         r.encoding = 'utf-8'
#         raw = r.text
# 
#         pattern = "query +: +'([^']+)'"
#         query = common.get_first_group_by_pattern(raw, pattern)
# 
#         return query
# 
#     def get_xml(self, query):
#         # http://rio.yahooapis.jp/RioWebService/V2/getLyrics?appid=7vOgnk6xg64IDggn6YEl3IQxmbj1qqkQzTpAx5nGwl9HnfPX3tZksE.oYhEw3zA-&lyrics_id=Y152097&results=1&multi_htmlspecialchars_flag=1
#         xmlpath = 'http://rio.yahooapis.jp/RioWebService/V2/getLyrics?appid=%s&%s' % (
#             '7vOgnk6xg64IDggn6YEl3IQxmbj1qqkQzTpAx5nGwl9HnfPX3tZksE.oYhEw3zA-', unquote(
#                 query)
#         )
# 
#         r = requests.get(xmlpath)
#         r.encoding = 'utf-8'
#         raw = r.text
# 
#         return raw
# 
#     def xml_get_name_attribute(self, doc, tagname):
#         ret = None
#         elements = doc.getElementsByTagName(tagname)
#         if elements.length == 1:
#             ret = elements.item(0).getAttribute('name')
#         return ret
# 
#     def xml_get_first_child(self, doc, tagname):
#         ret = None
#         elements = doc.getElementsByTagName(tagname)
#         if elements.length == 1:
#             ret = elements.item(0).firstChild.nodeValue
#         return ret
# 
#     def parse_xml(self, xml_str):
#         xml_str = xml_str.encode('utf-8')
#         doc = parseString(xml_str)
# 
#         def get_lyric(doc):
#             lyric = self.xml_get_first_child(doc, 'Lyrics')
#             lyric = str(lyric)
#             lyric = lyric.replace('<br>', '\n')
# 
#             return lyric
# 
#         self.lyric = get_lyric(doc)
#         self.find_song_info(doc)
# 
#         return True
# 
#     def find_song_info(self, doc):
#         ret = True
# 
#         patterns = {
#             'title': 'Title',
#             'artist': 'ArtistName',
#             'lyricist': 'WriterName',
#             'composer': 'ComposerName',
#         }
# 
#         for key in patterns:
#             tag = patterns[key]
# 
#             value = self.xml_get_first_child(doc, tag)
# 
#             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


# Path: lyric_engine/tests/test_yahoo.py
import unittest
from lyric_engine.modules.yahoo import Yahoo as Lyric


class UtaMapTest(unittest.TestCase):
    def test_url_one_more(self):
        url = 'http://lyrics.gyao.yahoo.co.jp/ly/Y004402/'
        obj = Lyric(url)
        obj.parse()

        self.assertEqual(obj.title, '帰って来たヨッパライ')
        self.assertEqual(obj.artist, 'ザ・フォーク・クルセダーズ')
        self.assertEqual(obj.lyricist, 'ザ･フォーク･パロディ･ギャング')
        self.assertEqual(obj.composer, '加藤和彦')
        self.assertEqual(len(obj.lyric), 495)

    def test_url_02(self):
        url = 'http://lyrics.gyao.yahoo.co.jp/ly/Y160641/'

        obj = Lyric(url)