# Path: lyric_engine/modules/joysound.py
# class JoySound(LyricBase):
#     def parse_page(self):
#         url = self.url
# 
#         song_id = self.get_song_id(url)
#         if not song_id:
#             logging.info('Failed to song id from [%s]', url)
#             return False
# 
#         logging.debug('Song ID is %s', song_id)
# 
#         json_obj = self.get_song_json(song_id)
#         if not json_obj:
#             logging.info('Failed to get song JSON of url [%s]', url)
#             return False
# 
#         if not self.parse_lyric(json_obj):
#             logging.info('Failed to get lyric of url [%s]', url)
#             return False
# 
#         if not self.parse_song_info(json_obj):
#             logging.info('Failed to get song info of url [%s]', url)
# 
#         return True
# 
#     def get_song_id(self, url):
#         pattern = '/song/([0-9]+)'
#         song_id = common.get_first_group_by_pattern(url, pattern)
# 
#         return song_id
# 
#     def get_song_json(self, song_id):
#         json_url = 'https://mspxy.joysound.com/Common/Lyric'
#         payload = {
#             'kind': 'naviGroupId',
#             'selSongNo': song_id,
#             'interactionFlg': '0',
#             'apiVer': '1.0',
#         }
#         headers = {
#             'X-JSP-APP-NAME': '0000800'
#         }
# 
#         r = requests.post(json_url, data=payload, headers=headers)
#         return r.json()
# 
#     def parse_lyric(self, json_obj):
#         if 'lyricList' not in json_obj:
#             logging.info('No "lyricList" in song JSON')
#             return False
# 
#         lyricList = json_obj['lyricList']
#         if len(lyricList) < 1:
#             logging.info('"lyricList" length < 1')
#             return False
# 
#         value = lyricList[0]['lyric']
#         value = value.strip()
# 
#         self.lyric = value
# 
#         return True
# 
#     def parse_song_info(self, json_obj):
#         patterns = {
#             'title': 'songName',
#             'artist': 'artistName',
#             'lyricist': 'lyricist',
#             'composer': 'composer',
#         }
# 
#         for key in patterns:
#             json_key = patterns[key]
# 
#             if json_key in json_obj:
#                 value = json_obj[json_key]
# 
#                 if value:
#                     setattr(self, key, value)
#         return True


import unittest
from lyric_engine.modules.joysound import JoySound as Lyric

class LyricTest(unittest.TestCase):
    def test_url_saved(self):
        url = 'https://www.joysound.com/web/search/song/402005'
        obj = Lyric(url)
        obj.parse()

        self.assertEqual(obj.title, 'SAVED.')
        self.assertEqual(obj.artist, '坂本真綾')
        self.assertEqual(obj.lyricist, '鈴木祥子')
        self.assertEqual(obj.composer, '鈴木祥子')
        self.assertEqual(len(obj.lyric), 397)

    def test_url_id(self):
        url = 'https://www.joysound.com/web/search/song/96127'

# Path: lyric_engine/modules/joysound.py
# class JoySound(LyricBase):
#     def parse_page(self):
#         url = self.url
# 
#         song_id = self.get_song_id(url)
#         if not song_id:
#             logging.info('Failed to song id from [%s]', url)
#             return False
# 
#         logging.debug('Song ID is %s', song_id)
# 
#         json_obj = self.get_song_json(song_id)
#         if not json_obj:
#             logging.info('Failed to get song JSON of url [%s]', url)
#             return False
# 
#         if not self.parse_lyric(json_obj):
#             logging.info('Failed to get lyric of url [%s]', url)
#             return False
# 
#         if not self.parse_song_info(json_obj):
#             logging.info('Failed to get song info of url [%s]', url)
# 
#         return True
# 
#     def get_song_id(self, url):
#         pattern = '/song/([0-9]+)'
#         song_id = common.get_first_group_by_pattern(url, pattern)
# 
#         return song_id
# 
#     def get_song_json(self, song_id):
#         json_url = 'https://mspxy.joysound.com/Common/Lyric'
#         payload = {
#             'kind': 'naviGroupId',
#             'selSongNo': song_id,
#             'interactionFlg': '0',
#             'apiVer': '1.0',
#         }
#         headers = {
#             'X-JSP-APP-NAME': '0000800'
#         }
# 
#         r = requests.post(json_url, data=payload, headers=headers)
#         return r.json()
# 
#     def parse_lyric(self, json_obj):
#         if 'lyricList' not in json_obj:
#             logging.info('No "lyricList" in song JSON')
#             return False
# 
#         lyricList = json_obj['lyricList']
#         if len(lyricList) < 1:
#             logging.info('"lyricList" length < 1')
#             return False
# 
#         value = lyricList[0]['lyric']
#         value = value.strip()
# 
#         self.lyric = value
# 
#         return True
# 
#     def parse_song_info(self, json_obj):
#         patterns = {
#             'title': 'songName',
#             'artist': 'artistName',
#             'lyricist': 'lyricist',
#             'composer': 'composer',
#         }
# 
#         for key in patterns:
#             json_key = patterns[key]
# 
#             if json_key in json_obj:
#                 value = json_obj[json_key]
# 
#                 if value:
#                     setattr(self, key, value)
#         return True


# Path: lyric_engine/tests/test_joysound.py
import unittest
from lyric_engine.modules.joysound import JoySound as Lyric


class LyricTest(unittest.TestCase):
    def test_url_saved(self):
        url = 'https://www.joysound.com/web/search/song/402005'
        obj = Lyric(url)
        obj.parse()

        self.assertEqual(obj.title, 'SAVED.')
        self.assertEqual(obj.artist, '坂本真綾')
        self.assertEqual(obj.lyricist, '鈴木祥子')
        self.assertEqual(obj.composer, '鈴木祥子')
        self.assertEqual(len(obj.lyric), 397)

    def test_url_id(self):
        url = 'https://www.joysound.com/web/search/song/96127'

        obj = Lyric(url)