utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import re
  2. RELEASE_TYPE = {0:"single", 1:"album", 2:"compile", 3:"ep", 4:"bundle"}
  3. # maps gw-light api user/tracks to standard api
  4. def map_user_track(track):
  5. return {
  6. 'id': track['SNG_ID'],
  7. 'title': track['SNG_TITLE'],
  8. 'link': 'https://www.deezer.com/track/'+str(track['SNG_ID']),
  9. 'duration': track['DURATION'],
  10. 'rank': track['RANK_SNG'],
  11. 'explicit_lyrics': int(track['EXPLICIT_LYRICS']) > 0,
  12. 'explicit_content_lyrics': track['EXPLICIT_TRACK_CONTENT']['EXPLICIT_COVER_STATUS'],
  13. 'explicit_content_cover': track['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'],
  14. 'time_add': track['DATE_ADD'],
  15. 'album': {
  16. 'id': track['ALB_ID'],
  17. 'title': track['ALB_TITLE'],
  18. 'cover': 'https://api.deezer.com/album/'+str(track['ALB_ID'])+'/image',
  19. 'cover_small': 'https://e-cdns-images.dzcdn.net/images/cover/'+str(track['ALB_PICTURE'])+'/56x56-000000-80-0-0.jpg',
  20. 'cover_medium': 'https://e-cdns-images.dzcdn.net/images/cover/'+str(track['ALB_PICTURE'])+'/250x250-000000-80-0-0.jpg',
  21. 'cover_big': 'https://e-cdns-images.dzcdn.net/images/cover/'+str(track['ALB_PICTURE'])+'/500x500-000000-80-0-0.jpg',
  22. 'cover_xl': 'https://e-cdns-images.dzcdn.net/images/cover/'+str(track['ALB_PICTURE'])+'/1000x1000-000000-80-0-0.jpg',
  23. 'tracklist': 'https://api.deezer.com/album/'+str(track['ALB_ID'])+'/tracks',
  24. 'type': 'album'
  25. },
  26. 'artist': {
  27. 'id': track['ART_ID'],
  28. 'name': track['ART_NAME'],
  29. 'picture': 'https://api.deezer.com/artist/'+str(track['ART_ID'])+'/image',
  30. 'picture_small': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(track['ART_PICTURE'])+'/56x56-000000-80-0-0.jpg',
  31. 'picture_medium': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(track['ART_PICTURE'])+'/250x250-000000-80-0-0.jpg',
  32. 'picture_big': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(track['ART_PICTURE'])+'/500x500-000000-80-0-0.jpg',
  33. 'picture_xl': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(track['ART_PICTURE'])+'/1000x1000-000000-80-0-0.jpg',
  34. 'tracklist': 'https://api.deezer.com/artist/'+str(track['ART_ID'])+'/top?limit=50',
  35. 'type': 'artist'
  36. },
  37. 'type': 'track'
  38. }
  39. # maps gw-light api user/artists to standard api
  40. def map_user_artist(artist):
  41. return {
  42. 'id': artist['ART_ID'],
  43. 'name': artist['ART_NAME'],
  44. 'link': 'https://www.deezer.com/artist/'+str(artist['ART_ID']),
  45. 'picture': 'https://api.deezer.com/artist/'+str(artist['ART_ID'])+'/image',
  46. 'picture_small': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(artist['ART_ID'])+'/56x56-000000-80-0-0.jpg',
  47. 'picture_medium': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(artist['ART_ID'])+'/250x250-000000-80-0-0.jpg',
  48. 'picture_big': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(artist['ART_ID'])+'/500x500-000000-80-0-0.jpg',
  49. 'picture_xl': 'https://e-cdns-images.dzcdn.net/images/artist/'+str(artist['ART_ID'])+'/1000x1000-000000-80-0-0.jpg',
  50. 'nb_fan': artist['NB_FAN'],
  51. 'tracklist': 'https://api.deezer.com/artist/'+str(artist['ART_ID'])+'/top?limit=50',
  52. 'type': 'artist'
  53. }
  54. # maps gw-light api user/albums to standard api
  55. def map_user_album(album):
  56. return {
  57. 'id': album['ALB_ID'],
  58. 'title': album['ALB_TITLE'],
  59. 'link': 'https://www.deezer.com/album/'+str(album['ALB_ID']),
  60. 'cover': 'https://api.deezer.com/album/'+str(album['ALB_ID'])+'/image',
  61. 'cover_small': 'https://e-cdns-images.dzcdn.net/images/cover/'+album['ALB_PICTURE']+'/56x56-000000-80-0-0.jpg',
  62. 'cover_medium': 'https://e-cdns-images.dzcdn.net/images/cover/'+album['ALB_PICTURE']+'/250x250-000000-80-0-0.jpg',
  63. 'cover_big': 'https://e-cdns-images.dzcdn.net/images/cover/'+album['ALB_PICTURE']+'/500x500-000000-80-0-0.jpg',
  64. 'cover_xl': 'https://e-cdns-images.dzcdn.net/images/cover/'+album['ALB_PICTURE']+'/1000x1000-000000-80-0-0.jpg',
  65. 'tracklist': 'https://api.deezer.com/album/'+str(album['ALB_ID'])+'/tracks',
  66. 'explicit_lyrics': album['EXPLICIT_ALBUM_CONTENT']['EXPLICIT_LYRICS_STATUS'] > 0,
  67. 'artist': {
  68. 'id': album['ART_ID'],
  69. 'name': album['ART_NAME'],
  70. 'picture': 'https://api.deezer.com/artist/'+str(album['ART_ID'])+'image',
  71. 'tracklist': 'https://api.deezer.com/artist/'+str(album['ART_ID'])+'/top?limit=50'
  72. },
  73. 'type': 'album'
  74. }
  75. # maps gw-light api user/playlists to standard api
  76. def map_user_playlist(playlist, default_user_name=""):
  77. return {
  78. 'id': playlist['PLAYLIST_ID'],
  79. 'title': playlist['TITLE'],
  80. 'nb_tracks': playlist['NB_SONG'],
  81. 'link': 'https://www.deezer.com/playlist/'+str(playlist['PLAYLIST_ID']),
  82. 'picture': 'https://api.deezer.com/playlist/'+str(playlist['PLAYLIST_ID'])+'/image',
  83. 'picture_small': 'https://e-cdns-images.dzcdn.net/images/'+playlist['PICTURE_TYPE']+'/'+playlist['PLAYLIST_PICTURE']+'/56x56-000000-80-0-0.jpg',
  84. 'picture_medium': 'https://e-cdns-images.dzcdn.net/images/'+playlist['PICTURE_TYPE']+'/'+playlist['PLAYLIST_PICTURE']+'/250x250-000000-80-0-0.jpg',
  85. 'picture_big': 'https://e-cdns-images.dzcdn.net/images/'+playlist['PICTURE_TYPE']+'/'+playlist['PLAYLIST_PICTURE']+'/500x500-000000-80-0-0.jpg',
  86. 'picture_xl': 'https://e-cdns-images.dzcdn.net/images/'+playlist['PICTURE_TYPE']+'/'+playlist['PLAYLIST_PICTURE']+'/1000x1000-000000-80-0-0.jpg',
  87. 'tracklist': 'https://api.deezer.com/playlist/'+str(playlist['PLAYLIST_ID'])+'/tracks',
  88. 'creator': {
  89. 'id': playlist['PARENT_USER_ID'],
  90. 'name': playlist.get('PARENT_USERNAME', default_user_name)
  91. },
  92. 'type': 'playlist'
  93. }
  94. # maps gw-light api albums to standard api
  95. def map_album(album):
  96. return {
  97. 'id': album['ALB_ID'],
  98. 'title': album['ALB_TITLE'],
  99. 'upc': "", # TODO: Needs to be checked
  100. 'link': f"https://www.deezer.com/album/{album['ALB_ID']}",
  101. 'share': "", # TODO: Needs to be checked
  102. 'cover': f"https://api.deezer.com/album/{album['ALB_ID']}/image",
  103. 'cover_small': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/56x56-000000-80-0-0.jpg",
  104. 'cover_medium': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/250x250-000000-80-0-0.jpg",
  105. 'cover_big': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/500x500-000000-80-0-0.jpg",
  106. 'cover_xl': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/1000x1000-000000-80-0-0.jpg",
  107. 'md5_image': album['ALB_PICTURE'],
  108. 'genre_id': album['GENRE_ID'],
  109. 'genres': [], # TODO: Needs to be checked
  110. 'label': "", # TODO: Needs to be checked
  111. 'nb_tracks': album['NUMBER_TRACK'],
  112. 'duration': 0, # TODO: Needs to be checked
  113. 'fans': album['RANK'],
  114. 'rating': 0, # TODO: Needs to be checked
  115. 'release_date': album['PHYSICAL_RELEASE_DATE'],
  116. 'record_type': RELEASE_TYPE.get(int(album['TYPE']), "unknown"),
  117. 'available': True, # TODO: Needs to be checked
  118. 'alternative': None, # TODO: Needs to be checked
  119. 'tracklist': f"https://api.deezer.com/album/{album['ALB_ID']}/tracks",
  120. 'explicit_lyrics': int(album['EXPLICIT_LYRICS']) > 0,
  121. 'explicit_content_lyrics': 2, # TODO: Needs to be checked
  122. 'explicit_content_cover': 2, # TODO: Needs to be checked
  123. 'contributors': [], # TODO: Needs to be checked
  124. 'artist': None, # TODO: Needs to be checked
  125. 'tracks': [], # TODO: Needs to be checked
  126. 'type': album['__TYPE__'],
  127. # Extras
  128. 'nb_disk': album['NUMBER_DISK']
  129. }
  130. # maps gw-light api artist/albums to standard api
  131. def map_artist_album(album):
  132. return {
  133. 'id': album['ALB_ID'],
  134. 'title': album['ALB_TITLE'],
  135. 'link': f"https://www.deezer.com/album/{album['ALB_ID']}",
  136. 'cover': f"https://api.deezer.com/album/{album['ALB_ID']}/image",
  137. 'cover_small': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/56x56-000000-80-0-0.jpg",
  138. 'cover_medium': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/250x250-000000-80-0-0.jpg",
  139. 'cover_big': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/500x500-000000-80-0-0.jpg",
  140. 'cover_xl': f"https://cdns-images.dzcdn.net/images/cover/{album['ALB_PICTURE']}/1000x1000-000000-80-0-0.jpg",
  141. 'genre_id': album['GENRE_ID'],
  142. 'fans': album['RANK'],
  143. 'release_date': album['PHYSICAL_RELEASE_DATE'],
  144. 'record_type': RELEASE_TYPE.get(int(album['TYPE']), "unknown"),
  145. 'tracklist': f"https://api.deezer.com/album/{album['ALB_ID']}/tracks",
  146. 'explicit_lyrics': int(album['EXPLICIT_LYRICS']) > 0,
  147. 'type': album['__TYPE__'],
  148. # Extras
  149. 'nb_tracks': album['NUMBER_TRACK'],
  150. 'nb_disk': album['NUMBER_DISK']
  151. }
  152. # maps gw-light api playlists to standard api
  153. def map_playlist(playlist):
  154. return {
  155. 'id': playlist['PLAYLIST_ID'],
  156. 'title': playlist['TITLE'],
  157. 'description': playlist['DESCRIPTION'],
  158. 'duration': playlist['DURATION'],
  159. 'public': playlist['STATUS'] == 1,
  160. 'is_loved_track': playlist['TYPE'] == 4,
  161. 'collaborative': playlist['STATUS'] == 2,
  162. 'nb_tracks': playlist['NB_SONG'],
  163. 'fans': playlist['NB_FAN'],
  164. 'link': "https://www.deezer.com/playlist/"+playlist['PLAYLIST_ID'],
  165. 'share': "https://www.deezer.com/playlist/"+playlist['PLAYLIST_ID'],
  166. 'picture': "https://api.deezer.com/playlist/"+playlist['PLAYLIST_ID']+"/image",
  167. 'picture_small': "https://cdns-images.dzcdn.net/images/"+playlist['PICTURE_TYPE']+"/"+playlist['PLAYLIST_PICTURE']+"/56x56-000000-80-0-0.jpg",
  168. 'picture_medium': "https://cdns-images.dzcdn.net/images/"+playlist['PICTURE_TYPE']+"/"+playlist['PLAYLIST_PICTURE']+"/250x250-000000-80-0-0.jpg",
  169. 'picture_big': "https://cdns-images.dzcdn.net/images/"+playlist['PICTURE_TYPE']+"/"+playlist['PLAYLIST_PICTURE']+"/500x500-000000-80-0-0.jpg",
  170. 'picture_xl': "https://cdns-images.dzcdn.net/images/"+playlist['PICTURE_TYPE']+"/"+playlist['PLAYLIST_PICTURE']+"/1000x1000-000000-80-0-0.jpg",
  171. 'checksum': playlist['CHECKSUM'],
  172. 'tracklist': "https://api.deezer.com/playlist/"+playlist['PLAYLIST_ID']+"/tracks",
  173. 'creation_date': playlist['DATE_ADD'],
  174. 'creator': {
  175. 'id': playlist['PARENT_USER_ID'],
  176. 'name': playlist['PARENT_USERNAME'],
  177. 'tracklist': "https://api.deezer.com/user/"+playlist['PARENT_USER_ID']+"/flow",
  178. 'type': "user"
  179. },
  180. 'type': "playlist"
  181. }
  182. # Cleanup terms that can hurt search results
  183. def clean_search_query(term):
  184. term = str(term)
  185. term = re.sub(r' feat[\.]? ', " ", term)
  186. term = re.sub(r' ft[\.]? ', " ", term)
  187. term = re.sub(r'\(feat[\.]? ', " ", term)
  188. term = re.sub(r'\(ft[\.]? ', " ", term)
  189. term = term.replace('&', " ").replace('–', "-").replace('—', "-")
  190. return term