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

75
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,30 +871,80 @@ 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
download_folder = Addon().get_setting('download_folder')
if not download_folder:
return 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()
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))
# get song infos
response = connection.getSong(id);
song = response.get('song')
plugin.log_error('Track info :')
plugin.log_error(song)
song_path_relative = song.get("path", None) # 'Radiohead/Kid A/Idioteque.mp3'
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: try:
request = connection.download(id) #get remote file (file-object like)
if request['status'] == 'ok': file_obj = connection.download(68679)
did_action = True
#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: except:
popup("Error while downloading track #%s" % (id))
plugin.log("Error while downloading track #%s" % (id))
pass pass
return did_action if ids_count == ids_downloaded_count:
return True
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 -->