# Path: fuocore/netease/models.py
# class NSongModel(SongModel, NBaseModel):
#     @classmethod
#     def get(cls, identifier):
#         data = cls._api.song_detail(int(identifier))
#         song, _ = NeteaseSongSchema(strict=True).load(data)
#         return song
# 
#     @classmethod
#     def list(cls, identifiers):
#         song_data_list = cls._api.songs_detail(identifiers)
#         songs = []
#         for song_data in song_data_list:
#             song, _ = NeteaseSongSchema(strict=True).load(song_data)
#             songs.append(song)
#         return songs
# 
#     def _refresh_url(self):
#         """刷新获取 url，失败的时候返回空而不是 None"""
#         songs = self._api.weapi_songs_url([int(self.identifier)])
#         if songs and songs[0]['url']:
#             self.url = songs[0]['url']
#         else:
#             self.url = ''
# 
#     def _find_in_local(self):
#         # TODO: make this a API in SongModel
#         path = os.path.join(MUSIC_LIBRARY_PATH, self.filename)
#         if os.path.exists(path):
#             logger.debug('find local file for {}'.format(self))
#             return path
#         return None
# 
#     # NOTE: if we want to override model attribute, we must
#     # implement both getter and setter.
#     @property
#     def url(self):
#         """
#         We will always check if this song file exists in local library,
#         if true, we return the url of the local file.
# 
#         .. note::
# 
#             As netease song url will be expired after a period of time,
#             we can not use static url here. Currently, we assume that the
#             expiration time is 20 minutes, after the url expires, it
#             will be automaticly refreshed.
#         """
#         local_path = self._find_in_local()
#         if local_path:
#             return local_path
# 
#         if not self._url:
#             self._refresh_url()
#         elif time.time() > self._expired_at:
#             logger.info('song({}) url is expired, refresh...'.format(self))
#             self._refresh_url()
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._expired_at = time.time() + 60 * 20 * 1  # 20 minutes
#         self._url = value
# 
#     @property
#     def lyric(self):
#         if self._lyric is not None:
#             assert isinstance(self._lyric, LyricModel)
#             return self._lyric
#         data = self._api.get_lyric_by_songid(self.identifier)
#         lrc = data.get('lrc', {})
#         lyric = lrc.get('lyric', '')
#         self._lyric = LyricModel(
#             identifier=self.identifier,
#             content=lyric
#         )
#         return self._lyric
# 
#     @lyric.setter
#     def lyric(self, value):
#         self._lyric = value


import logging
from fuocore.netease.models import NSongModel
#! /usr/bin/env python3
# -*- coding: utf-8 -*-



logging.basicConfig()
logger = logging.getLogger('fuocore')
logger.setLevel(logging.DEBUG)


def test_model_display():
    song = NSongModel.create_by_display(
            identifier=254548,
            title='成全',
            artists_name='刘若英')
    assert song.album_name_display == ''
    assert song.title_display == '成全'

# Path: fuocore/netease/models.py
# class NSongModel(SongModel, NBaseModel):
#     @classmethod
#     def get(cls, identifier):
#         data = cls._api.song_detail(int(identifier))
#         song, _ = NeteaseSongSchema(strict=True).load(data)
#         return song
# 
#     @classmethod
#     def list(cls, identifiers):
#         song_data_list = cls._api.songs_detail(identifiers)
#         songs = []
#         for song_data in song_data_list:
#             song, _ = NeteaseSongSchema(strict=True).load(song_data)
#             songs.append(song)
#         return songs
# 
#     def _refresh_url(self):
#         """刷新获取 url，失败的时候返回空而不是 None"""
#         songs = self._api.weapi_songs_url([int(self.identifier)])
#         if songs and songs[0]['url']:
#             self.url = songs[0]['url']
#         else:
#             self.url = ''
# 
#     def _find_in_local(self):
#         # TODO: make this a API in SongModel
#         path = os.path.join(MUSIC_LIBRARY_PATH, self.filename)
#         if os.path.exists(path):
#             logger.debug('find local file for {}'.format(self))
#             return path
#         return None
# 
#     # NOTE: if we want to override model attribute, we must
#     # implement both getter and setter.
#     @property
#     def url(self):
#         """
#         We will always check if this song file exists in local library,
#         if true, we return the url of the local file.
# 
#         .. note::
# 
#             As netease song url will be expired after a period of time,
#             we can not use static url here. Currently, we assume that the
#             expiration time is 20 minutes, after the url expires, it
#             will be automaticly refreshed.
#         """
#         local_path = self._find_in_local()
#         if local_path:
#             return local_path
# 
#         if not self._url:
#             self._refresh_url()
#         elif time.time() > self._expired_at:
#             logger.info('song({}) url is expired, refresh...'.format(self))
#             self._refresh_url()
#         return self._url
# 
#     @url.setter
#     def url(self, value):
#         self._expired_at = time.time() + 60 * 20 * 1  # 20 minutes
#         self._url = value
# 
#     @property
#     def lyric(self):
#         if self._lyric is not None:
#             assert isinstance(self._lyric, LyricModel)
#             return self._lyric
#         data = self._api.get_lyric_by_songid(self.identifier)
#         lrc = data.get('lrc', {})
#         lyric = lrc.get('lyric', '')
#         self._lyric = LyricModel(
#             identifier=self.identifier,
#             content=lyric
#         )
#         return self._lyric
# 
#     @lyric.setter
#     def lyric(self, value):
#         self._lyric = value


# Path: examples/model_display.py
import logging
from fuocore.netease.models import NSongModel

#! /usr/bin/env python3
# -*- coding: utf-8 -*-



logging.basicConfig()
logger = logging.getLogger('fuocore')
logger.setLevel(logging.DEBUG)


def test_model_display():
    song = NSongModel.create_by_display(
            identifier=254548,
            title='成全',
            artists_name='刘若英')
    assert song.album_name_display == ''
    assert song.title_display == '成全'

    print(song.url, song.title)