mirror of
https://github.com/gordielachance/plugin.audio.subsonic
synced 2025-02-02 10:26:48 +01:00
tracks and albums download works
+ progress bar !
This commit is contained in:
parent
c0a7386ff1
commit
8d3d425089
76
main.py
76
main.py
@ -13,6 +13,7 @@ import json
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import dateutil.parser
|
import dateutil.parser
|
||||||
import shutil
|
import shutil
|
||||||
|
import xbmcgui
|
||||||
|
|
||||||
# Create plugin instance
|
# Create plugin instance
|
||||||
plugin = Plugin()
|
plugin = Plugin()
|
||||||
@ -893,74 +894,103 @@ def download_tracks(ids):
|
|||||||
if ids_count == 0:
|
if ids_count == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
plugin.log('download_tracks IDs:')
|
||||||
|
plugin.log(json.dumps(ids))
|
||||||
|
|
||||||
# get connection
|
# get connection
|
||||||
connection = get_connection()
|
connection = get_connection()
|
||||||
|
|
||||||
if connection is False:
|
if connection is False:
|
||||||
return
|
return
|
||||||
|
|
||||||
ids_downloaded_count = 0
|
#progress...
|
||||||
|
pc_step = 100/ids_count
|
||||||
|
pc_progress = 0
|
||||||
|
ids_parsed = 0
|
||||||
|
progressdialog = xbmcgui.DialogProgress()
|
||||||
|
progressdialog.create("Downloading tracks...") #Title
|
||||||
|
|
||||||
for id in ids:
|
for id in ids:
|
||||||
|
|
||||||
|
if (progressdialog.iscanceled()):
|
||||||
|
return False
|
||||||
|
|
||||||
# debug
|
# debug
|
||||||
plugin.log('Trying to download track #'+str(id))
|
plugin.log('Trying to download track #'+str(id))
|
||||||
|
|
||||||
# get song infos
|
# get track infos
|
||||||
response = connection.getSong(id);
|
response = connection.getSong(id);
|
||||||
song = response.get('song')
|
track = response.get('song')
|
||||||
plugin.log('Track info :')
|
plugin.log('Track info :')
|
||||||
plugin.log(song)
|
plugin.log(track)
|
||||||
|
|
||||||
song_path_relative = song.get("path", None) # 'Radiohead/Kid A/Idioteque.mp3'
|
# progress bar
|
||||||
song_path = os.path.join(download_folder, song_path_relative) # 'C:/users/.../Radiohead/Kid A/Idioteque.mp3'
|
pc_progress = ids_parsed * pc_step
|
||||||
song_directory = os.path.dirname(os.path.abspath(song_path)) # 'C:/users/.../Radiohead/Kid A'
|
progressdialog.update(pc_progress, 'Getting track informations...',"%s - %s" % (track.get('artist','<Unknown>'),track.get('title','<Unknown>')))
|
||||||
|
|
||||||
|
track_path_relative = track.get("path", None) # 'Radiohead/Kid A/Idioteque.mp3'
|
||||||
|
track_path = os.path.join(download_folder, track_path_relative) # 'C:/users/.../Radiohead/Kid A/Idioteque.mp3'
|
||||||
|
track_directory = os.path.dirname(os.path.abspath(track_path)) # 'C:/users/.../Radiohead/Kid A'
|
||||||
|
|
||||||
#check if file exists
|
#check if file exists
|
||||||
if os.path.isfile(song_path):
|
if os.path.isfile(track_path):
|
||||||
|
|
||||||
|
progressdialog.update(pc_progress, 'Track has already been downloaded!')
|
||||||
plugin.log("File '%s' already exists" % (id))
|
plugin.log("File '%s' already exists" % (id))
|
||||||
continue
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
progressdialog.update(pc_progress, "Downloading track...",track_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
#get remote file (file-object like)
|
#get remote file (file-object like)
|
||||||
file_obj = connection.download(68679)
|
file_obj = connection.download(id)
|
||||||
|
|
||||||
#create directory if it does not exists
|
#create directory if it does not exists
|
||||||
if not os.path.exists(song_directory):
|
if not os.path.exists(track_directory):
|
||||||
os.makedirs(song_directory)
|
os.makedirs(track_directory)
|
||||||
|
|
||||||
#create blank file
|
#create blank file
|
||||||
file = open(song_path, 'a') #create a new file but don't erase the existing one if it exists
|
file = open(track_path, 'a') #create a new file but don't erase the existing one if it exists
|
||||||
|
|
||||||
#fill blank file
|
#fill blank file
|
||||||
shutil.copyfileobj(file_obj, file)
|
shutil.copyfileobj(file_obj, file)
|
||||||
file.close()
|
file.close()
|
||||||
|
|
||||||
ids_downloaded_count+=1
|
|
||||||
popup("Track #%s' has been downloaded" % (id))
|
|
||||||
|
|
||||||
except:
|
except:
|
||||||
popup("Error while downloading track #%s" % (id))
|
popup("Error while downloading track #%s" % (id))
|
||||||
plugin.log("Error while downloading track #%s" % (id))
|
plugin.log("Error while downloading track #%s" % (id))
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if ids_count == ids_downloaded_count:
|
ids_parsed += 1
|
||||||
return True
|
|
||||||
|
|
||||||
|
progressdialog.update(100, "Done !","Enjoy !")
|
||||||
|
xbmc.sleep(1000)
|
||||||
|
progressdialog.close()
|
||||||
|
|
||||||
def download_album(id):
|
def download_album(id):
|
||||||
|
|
||||||
popup('Downloading albums is not yet implemented !')
|
|
||||||
return
|
|
||||||
|
|
||||||
# get connection
|
# get connection
|
||||||
connection = get_connection()
|
connection = get_connection()
|
||||||
|
|
||||||
if connection is False:
|
if connection is False:
|
||||||
return
|
return
|
||||||
|
|
||||||
###
|
# get album infos
|
||||||
|
response = connection.getAlbum(id);
|
||||||
|
album = response.get('album')
|
||||||
|
tracks = album.get('song')
|
||||||
|
|
||||||
did_action = False
|
plugin.log('getAlbum:')
|
||||||
|
plugin.log(json.dumps(album))
|
||||||
|
|
||||||
|
ids = [] #list of track IDs
|
||||||
|
|
||||||
|
for i, track in enumerate(tracks):
|
||||||
|
track_id = track.get('id')
|
||||||
|
ids.append(track_id)
|
||||||
|
|
||||||
|
download_tracks(ids)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user