-
Notifications
You must be signed in to change notification settings - Fork 17
/
async_demaster.py
74 lines (57 loc) · 2.12 KB
/
async_demaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Track name demastering script.
Removes nonsense like "Remastered" and "Live at".
modified from original at https://github.com/hankhank10/demaster
"""
import logging
import re
import aiohttp
API_URL = "http://demaster.hankapi.com/demaster"
OFFLINE_PATTERN = re.compile(
r"(\s(\(|-\s+))((199\d|20[0-2]\d)\s+)?(Remast|Live|Mono|From|Feat|Original|Motion|Deluxe).*", re.IGNORECASE
)
_LOGGER = logging.getLogger(__name__)
def strip_name_offline(full_song_name):
"""Use an offline regex to shorten the track name."""
match = OFFLINE_PATTERN.search(full_song_name)
if match:
short_name = full_song_name[: match.start()]
_LOGGER.debug("Demastered to %s", short_name)
return short_name
return full_song_name
async def strip_name_api(session, full_song_name):
"""Call the demaster API to retrieve a short track name."""
if session is None:
local_session = True
session = aiohttp.ClientSession()
else:
local_session = False
params = {
"format": "simple",
"long_track_name": full_song_name,
}
short_name = None
try:
async with session.get(API_URL, params=params) as response:
if response.status == 200:
short_name = await response.text()
if short_name != full_song_name:
_LOGGER.debug("Demastered to %s", short_name)
except aiohttp.ClientError as err:
_LOGGER.error("Problem connecting to demaster API [%s]", err)
except Exception as err: # pylint: disable=broad-except
_LOGGER.error("Unknown issue connecting to demaster API [%s]", err)
if local_session:
await session.close()
if short_name is None:
raise ConnectionError
return short_name
async def strip_name(full_song_name, session=None, offline=False):
"""Main entry point."""
if offline:
return strip_name_offline(full_song_name)
try:
return await strip_name_api(session, full_song_name)
except ConnectionError:
_LOGGER.debug("Online API failed, returning offline version")
return strip_name_offline(full_song_name)