now able to download tracks !

This commit is contained in:
gordielachance 2016-10-03 21:18:43 +02:00
parent 52c2c2d2d7
commit 3b9642ea85
2 changed files with 70 additions and 18 deletions

83
main.py
View File

@ -12,6 +12,7 @@ import os
import json import json
from datetime import datetime from datetime import datetime
import dateutil.parser import dateutil.parser
import shutil
# Create plugin instance # Create plugin instance
plugin = Plugin() plugin = Plugin()
@ -847,17 +848,17 @@ def download_item(params):
type= params.get('type'); type= params.get('type');
#validate path #validate path
path = Addon().get_setting('subsonic_url') download_folder = Addon().get_setting('download_folder')
if path is None: #TO FIX better statement ? Check if it exists if not download_folder:
popup('No directory set for your downloads, please review the addon settings') popup("Please set a directory for your downloads")
#validate capability #validate capability
if not can_download(type,id): if not can_download(type,id):
return; return;
if type == 'track': if type == 'track':
did_action = download_track(id) did_action = download_tracks(id)
elif type == 'album': elif type == 'album':
did_action = download_album(id) did_action = download_album(id)
@ -870,10 +871,26 @@ def download_item(params):
return did_action return did_action
def download_track(id): def download_tracks(ids):
popup('Downloading tracks is not yet implemented !') #popup is fired before, in download_item
return download_folder = Addon().get_setting('download_folder')
if not download_folder:
return
if not ids:
return False
#make list
if not isinstance(ids, list) or isinstance(ids, tuple):
ids = [ids]
ids_count = len(ids)
#check if empty
if ids_count == 0:
return False
# get connection # get connection
connection = get_connection() connection = get_connection()
@ -881,19 +898,53 @@ def download_track(id):
if connection is False: if connection is False:
return return
### ids_downloaded_count = 0
for id in ids:
did_action = False # debug
plugin.log('Trying to download track #'+str(id))
try: # get song infos
request = connection.download(id) response = connection.getSong(id);
if request['status'] == 'ok': song = response.get('song')
did_action = True plugin.log_error('Track info :')
plugin.log_error(song)
except: song_path_relative = song.get("path", None) # 'Radiohead/Kid A/Idioteque.mp3'
pass song_path = os.path.join(download_folder, song_path_relative) # 'C:/users/.../Radiohead/Kid A/Idioteque.mp3'
song_directory = os.path.dirname(os.path.abspath(song_path)) # 'C:/users/.../Radiohead/Kid A'
#check if file exists
if os.path.isfile(song_path):
plugin.log("File '%s' already exists" % (id))
continue
try:
#get remote file (file-object like)
file_obj = connection.download(68679)
#create directory if it does not exists
if not os.path.exists(song_directory):
os.makedirs(song_directory)
#create blank file
file = open(song_path, 'a') #create a new file but don't erase the existing one if it exists
#fill blank file
shutil.copyfileobj(file_obj, file)
file.close()
ids_downloaded_count+=1
popup("Track #%s' has been downloaded" % (id))
except:
popup("Error while downloading track #%s" % (id))
plugin.log("Error while downloading track #%s" % (id))
pass
if ids_count == ids_downloaded_count:
return True
return did_action
def download_album(id): def download_album(id):

View File

@ -12,6 +12,7 @@
<setting label="Streaming" type="lsep" /> <setting label="Streaming" type="lsep" />
<setting id="transcode_format_streaming" type="labelenum" label="Transcode format" values="mp3|raw|flv|ogg"/> <setting id="transcode_format_streaming" type="labelenum" label="Transcode format" values="mp3|raw|flv|ogg"/>
<setting id="bitrate_streaming" type="labelenum" label="Bitrate" values="320|256|224|192|160|128|112|96|80|64|56|48|40|32"/> <setting id="bitrate_streaming" type="labelenum" label="Bitrate" values="320|256|224|192|160|128|112|96|80|64|56|48|40|32"/>
<setting id="download_folder" type="folder" label="Download folder" source="auto" option="writeable"/>
</category> </category>
<!-- ADVANCED --> <!-- ADVANCED -->