Skip to content

Commit

Permalink
fix(music_new, netease_music): make endtime_ms a property; improve ne…
Browse files Browse the repository at this point in the history
…tease_music.py
  • Loading branch information
LucunJi committed Jun 24, 2022
1 parent e5c27a7 commit c39e11a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
15 changes: 4 additions & 11 deletions app/music_new/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MusicPiece:
def __init__(self, platform: Type['Platform'], requestors: list['PropertyRequestor']):
self.platform = platform
self.requestors: dict[str, 'PropertyRequestor'] = {}
self.endtime_ms: Optional[int] = None

for r in requestors:
r.bind(self)
Expand Down Expand Up @@ -49,19 +50,11 @@ async def media_url(self) -> str:
async def duration_ms(self) -> int:
pass

@property
@abstract
async def endtime_ms(self) -> int:
pass

@property
@abstract
async def cover_url(self) -> str:
pass

def __x(self):
return asyncio.gather(self.name, self.artists)

def __repr__(self):
return f'<{self.__class__.__name__} with attributes {self.__dict__}>'

Expand All @@ -72,7 +65,7 @@ def __init__(self, name: str = None, expiration_time_sec: float = math.inf):
self.owner: Optional[MusicPiece] = None
self.lock = Lock()
self.cache = None
self.counter = 0
self.__counter = 0 # for profiling
self.lastrun_sec = -math.inf

def bind(self, music: 'MusicPiece'):
Expand All @@ -84,8 +77,8 @@ async def invoke(self, *args, **kwargs) -> any:

async def __call__(self, *args, **kwargs) -> any:
async with self.lock:
if time.time() - self.lastrun_sec > self.expiration_time_sec or self.counter == 0:
self.counter += 1
if time.time() - self.lastrun_sec > self.expiration_time_sec or self.__counter == 0:
self.__counter += 1
self.lastrun_sec = time.time()
self.result = await self.invoke(*args, **kwargs)
return self.result
Expand Down
24 changes: 17 additions & 7 deletions app/music_new/netease/netease_music.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional

import aiohttp

Expand All @@ -23,7 +24,7 @@ async def invoke(self, *args, **kwargs):
}) as resp:
text = await resp.text()
data = json.loads(text)
return data.get('data', [{}])[0].get('url')
return data


class DetailRequestor(PropertyRequestor):
Expand All @@ -38,8 +39,7 @@ async def invoke(self, *args, **kwargs):
}) as resp:
text = await resp.text()
data = json.loads(text)
song = data.get('songs', [{}])[0]
return song
return data


class NeteaseMusic(MusicPiece):
Expand All @@ -49,16 +49,26 @@ def __init__(self, song_id):
DetailRequestor(song_id)
])
self.song_id = song_id
self.__name: Optional[str] = None
self.__artists: Optional[list[str]] = None
self.__playable: Optional[bool] = None
self.__duration_ms: Optional[int] = None
self.__cover_url: Optional[str] = None

@property
async def name(self) -> str:
song = await self.requestors['DetailRequestor']('name')
return song['name']
if self.__name is None:
data = await self.requestors['DetailRequestor']('name')
self.__name = data.get('songs', [{}])[0].get('name', 'N/A')
return self.__name

@property
async def artists(self) -> list[str]:
song = await self.requestors['DetailRequestor']('artist')
return [artist['name'] for artist in song['artists']]
if self.artists is None:
data = await self.requestors['DetailRequestor']('name')
artists = data.get('songs', [{}])[0].get('artists', [])
self.__artists = [artists.get('name', 'Unknown') for artist in artists]
return self.__artists

@property
async def media_url(self) -> str:
Expand Down

0 comments on commit c39e11a

Please sign in to comment.