2016-09-18 04:33:46 +02:00
|
|
|
import re
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
SONG = "song"
|
|
|
|
ARTIST = "artist"
|
|
|
|
PLAYLIST = "playlist"
|
|
|
|
ALBUM = "album"
|
|
|
|
DIRECTORY = "directory"
|
|
|
|
VDIR = "vdir"
|
|
|
|
PREFIX = "subidy"
|
|
|
|
SEARCH = "search"
|
2020-11-03 20:36:28 +01:00
|
|
|
RANDOM = "random"
|
2020-03-08 12:30:34 +01:00
|
|
|
|
|
|
|
regex = re.compile(r"(\w+?):(\w+?)(?::|$)(.+?)?$")
|
2016-09-18 04:33:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_type_result_valid(result):
|
|
|
|
return result is not None and result.group(1) == PREFIX
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def is_id_result_valid(result, type):
|
2020-03-08 12:30:34 +01:00
|
|
|
return (
|
|
|
|
is_type_result_valid(result)
|
|
|
|
and result.group(1) == PREFIX
|
|
|
|
and result.group(2) == type
|
|
|
|
)
|
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
|
2016-09-18 18:19:08 +02:00
|
|
|
def is_uri(uri):
|
|
|
|
return regex.match(uri) is not None
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_song_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, SONG):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_artist_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, ARTIST):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_playlist_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, PLAYLIST):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_album_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, ALBUM):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2017-02-12 15:50:41 -05:00
|
|
|
def get_directory_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, DIRECTORY):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2017-02-28 01:45:17 -05:00
|
|
|
def get_vdir_id(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_id_result_valid(result, VDIR):
|
|
|
|
return None
|
|
|
|
return result.group(3)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_type(uri):
|
|
|
|
result = regex.match(uri)
|
|
|
|
if not is_type_result_valid(result):
|
|
|
|
return None
|
|
|
|
return result.group(2)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_type_uri(type, id):
|
2020-03-08 12:30:34 +01:00
|
|
|
return f"{PREFIX}:{type}:{id}"
|
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
|
|
|
|
def get_artist_uri(id):
|
|
|
|
return get_type_uri(ARTIST, id)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_album_uri(id):
|
|
|
|
return get_type_uri(ALBUM, id)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 04:33:46 +02:00
|
|
|
def get_song_uri(id):
|
|
|
|
return get_type_uri(SONG, id)
|
2016-09-18 18:19:08 +02:00
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2017-02-12 15:50:41 -05:00
|
|
|
def get_directory_uri(id):
|
|
|
|
return get_type_uri(DIRECTORY, id)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2017-02-28 01:45:17 -05:00
|
|
|
def get_vdir_uri(id):
|
|
|
|
return get_type_uri(VDIR, id)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 18:19:08 +02:00
|
|
|
def get_playlist_uri(id):
|
|
|
|
return get_type_uri(PLAYLIST, id)
|
|
|
|
|
2020-03-08 12:30:34 +01:00
|
|
|
|
2016-09-18 18:19:08 +02:00
|
|
|
def get_search_uri(query):
|
|
|
|
return get_type_uri(SEARCH, query)
|