deemix-py/deemix/__init__.py

64 lines
2.4 KiB
Python
Raw Normal View History

2020-02-17 15:46:18 +00:00
#!/usr/bin/env python3
2021-03-19 13:31:32 +00:00
import re
from urllib.request import urlopen
from deemix.itemgen import generateTrackItem, generateAlbumItem, generatePlaylistItem, generateArtistItem, generateArtistDiscographyItem, generateArtistTopItem
__version__ = "2.0.16"
USER_AGENT_HEADER = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " \
"Chrome/79.0.3945.130 Safari/537.36"
2021-03-19 13:31:32 +00:00
# Returns the Resolved URL, the Type and the ID
def parseLink(link):
2021-04-10 09:53:52 +00:00
if 'deezer.page.link' in link: link = urlopen(link).url # Resolve URL shortner
2021-03-19 13:31:32 +00:00
# Remove extra stuff
if '?' in link: link = link[:link.find('?')]
if '&' in link: link = link[:link.find('&')]
if link.endswith('/'): link = link[:-1] # Remove last slash if present
2021-04-10 09:53:52 +00:00
link_type = None
link_id = None
2021-03-19 13:31:32 +00:00
2021-04-10 09:53:52 +00:00
if not 'deezer' in link: return (link, link_type, link_id) # return if not a deezer link
2021-03-19 13:31:32 +00:00
if '/track' in link:
2021-04-10 09:53:52 +00:00
link_type = 'track'
link_id = re.search(r"/track/(.+)", link).group(1)
2021-03-19 13:31:32 +00:00
elif '/playlist' in link:
2021-04-10 09:53:52 +00:00
link_type = 'playlist'
link_id = re.search(r"/playlist/(\d+)", link).group(1)
2021-03-19 13:31:32 +00:00
elif '/album' in link:
2021-04-10 09:53:52 +00:00
link_type = 'album'
link_id = re.search(r"/album/(.+)", link).group(1)
elif re.search(r"/artist/(\d+)/top_track", link):
link_type = 'artist_top'
link_id = re.search(r"/artist/(\d+)/top_track", link).group(1)
elif re.search(r"/artist/(\d+)/discography", link):
link_type = 'artist_discography'
link_id = re.search(r"/artist/(\d+)/discography", link).group(1)
2021-03-19 13:31:32 +00:00
elif '/artist' in link:
2021-04-10 09:53:52 +00:00
link_type = 'artist'
link_id = re.search(r"/artist/(\d+)", link).group(1)
2021-03-19 13:31:32 +00:00
2021-04-10 09:53:52 +00:00
return (link, link_type, link_id)
2021-03-19 13:31:32 +00:00
2021-03-24 16:41:03 +00:00
def generateDownloadObject(dz, link, bitrate):
2021-04-10 09:53:52 +00:00
(link, link_type, link_id) = parseLink(link)
if link_type is None or link_id is None:
return None
if link_type == "track":
return generateTrackItem(dz, link_id, bitrate)
if link_type == "album":
return generateAlbumItem(dz, link_id, bitrate)
if link_type == "playlist":
return generatePlaylistItem(dz, link_id, bitrate)
if link_type == "artist":
return generateArtistItem(dz, link_id, bitrate)
if link_type == "artist_discography":
return generateArtistDiscographyItem(dz, link_id, bitrate)
if link_type == "artist_top":
return generateArtistTopItem(dz, link_id, bitrate)
2021-03-19 13:31:32 +00:00
return None