The pywebview app for deemix-webui
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

186 lines
6.0 KiB

  1. #!/usr/bin/env python3
  2. from deemix.api.deezer import Deezer
  3. from deemix.app.settings import Settings
  4. from deemix.app.queuemanager import QueueManager
  5. from deemix.app.spotifyhelper import SpotifyHelper, emptyPlaylist as emptySpotifyPlaylist
  6. from deemix.utils import getTypeFromLink, getIDFromLink
  7. from deemix.utils.localpaths import getConfigFolder
  8. import os.path as path
  9. import json
  10. class deemix:
  11. def __init__(self, portable):
  12. self.configFolder = portable or getConfigFolder()
  13. self.set = Settings(self.configFolder)
  14. self.sp = SpotifyHelper(self.configFolder)
  15. self.qm = QueueManager(self.sp)
  16. self.chartsList = []
  17. self.homeCache = None
  18. def shutdown(self, interface=None):
  19. if self.set.settings['saveDownloadQueue']:
  20. self.qm.saveQueue(self.configFolder)
  21. self.qm.cancelAllDownloads(interface)
  22. if interface:
  23. interface.send("toast", {'msg': "Server is closed."})
  24. def getConfigArl(self):
  25. tempDeezer = Deezer()
  26. arl = None
  27. if path.isfile(path.join(self.configFolder, '.arl')):
  28. with open(path.join(self.configFolder, '.arl'), 'r') as f:
  29. arl = f.readline().rstrip("\n")
  30. if not arl or not tempDeezer.login_via_arl(arl):
  31. while True:
  32. arl = input("Paste here your arl:")
  33. if tempDeezer.login_via_arl(arl):
  34. break
  35. with open(path.join(self.configFolder, '.arl'), 'w') as f:
  36. f.write(arl)
  37. return arl
  38. def restoreDownloadQueue(self, dz, interface=None):
  39. self.qm.loadQueue(self.configFolder, self.set.settings, interface)
  40. def queueRestored(self, dz, interface=None):
  41. self.qm.nextItem(dz, interface)
  42. def get_charts(self, dz):
  43. if len(self.chartsList) == 0:
  44. temp = dz.get_charts_countries()
  45. countries = []
  46. for i in range(len(temp)):
  47. countries.append({
  48. 'title': temp[i]['title'].replace("Top ", ""),
  49. 'id': temp[i]['id'],
  50. 'picture_small': temp[i]['picture_small'],
  51. 'picture_medium': temp[i]['picture_medium'],
  52. 'picture_big': temp[i]['picture_big']
  53. })
  54. self.chartsList = countries
  55. return self.chartsList
  56. def get_home(self, dz):
  57. if not self.homeCache:
  58. self.homeCache = dz.get_charts()
  59. return self.homeCache
  60. def getDownloadFolder(self):
  61. return self.set.settings['downloadLocation']
  62. def getUserFavorites(self, dz):
  63. user_id = dz.user['id']
  64. result = {}
  65. try:
  66. result['playlists'] = dz.get_user_playlists(user_id)['data']
  67. result['albums'] = dz.get_user_albums(user_id)['data']
  68. result['artists'] = dz.get_user_artists(user_id)['data']
  69. result['tracks'] = dz.get_user_tracks(user_id)['data']
  70. except:
  71. result['playlists'] = dz.get_user_playlists_gw(user_id)
  72. result['albums'] = dz.get_user_albums_gw(user_id)
  73. result['artists'] = dz.get_user_artists_gw(user_id)
  74. result['tracks'] = dz.get_user_tracks_gw(user_id)
  75. return result
  76. def updateUserSpotifyPlaylists(self, user):
  77. if user == "" or not self.sp.spotifyEnabled:
  78. return []
  79. try:
  80. return self.sp.get_user_playlists(user)
  81. except:
  82. return []
  83. def updateUserPlaylists(self, dz):
  84. user_id = dz.user['id']
  85. try:
  86. return dz.get_user_playlists(user_id)['data']
  87. except:
  88. return dz.get_user_playlists_gw(user_id)
  89. def updateUserAlbums(self, dz):
  90. user_id = dz.user['id']
  91. try:
  92. return dz.get_user_albums(user_id)['data']
  93. except:
  94. return dz.get_user_albums_gw(user_id)
  95. def updateUserArtists(self, dz):
  96. user_id = dz.user['id']
  97. try:
  98. return dz.get_user_artists(user_id)['data']
  99. except:
  100. return dz.get_user_artists_gw(user_id)
  101. def updateUserTracks(self, dz):
  102. user_id = dz.user['id']
  103. try:
  104. return dz.get_user_tracks(user_id)['data']
  105. except:
  106. return dz.get_user_tracks_gw(user_id)
  107. def getSpotifyPlaylistTracklist(self, id):
  108. if id == "" or not self.sp.spotifyEnabled:
  109. return emptySpotifyPlaylist
  110. return self.sp.get_playlist_tracklist(id)
  111. # Search functions
  112. def mainSearch(self, dz, term):
  113. return dz.search_main_gw(term)
  114. def search(self, dz, term, type, start, nb):
  115. return dz.search(term, type, nb, start)
  116. # Queue functions
  117. def addToQueue(self, dz, url, bitrate=None, interface=None, ack=None):
  118. if ';' in url:
  119. url = url.split(";")
  120. self.qm.addToQueue(dz, url, self.set.settings, bitrate, interface, ack)
  121. def removeFromQueue(self, uuid, interface=None):
  122. self.qm.removeFromQueue(uuid, interface)
  123. def cancelAllDownloads(self, interface=None):
  124. self.qm.cancelAllDownloads(interface)
  125. def removeFinishedDownloads(self, interface=None):
  126. self.qm.removeFinishedDownloads(interface)
  127. def initDownloadQueue(self):
  128. (queue, queueComplete, queueList, currentItem) = self.qm.getQueue()
  129. return (queue, queueComplete, queueList, currentItem)
  130. def analyzeLink(self, dz, link):
  131. type = getTypeFromLink(link)
  132. relID = getIDFromLink(link, type)
  133. if type in ["track", "album"]:
  134. data = getattr(dz, 'get_' + type)(relID)
  135. else:
  136. data = {}
  137. return (type, data)
  138. # Settings functions
  139. def getDefaultSettings(self):
  140. return self.set.defaultSettings
  141. def getSettings(self):
  142. return self.set.settings
  143. def saveSettings(self, newSettings, dz=None):
  144. return self.set.saveSettings(newSettings, dz)
  145. def getSpotifyCredentials(self):
  146. return self.sp.getCredentials()
  147. def setSpotifyCredentials(self, newCredentials):
  148. return self.sp.setCredentials(newCredentials)