How Do You Download & Save Image to Skin Directory
#1
Hi there,

Well, i've finally installed the latest xbmc nightly and for some reason or another (lack of knowlege, really), the code that i've used before is not working anymore to dowload an image and saving it to a skin directory. I'm sure it is because I'm not passing the correct directory path... the code i use is below:
PHP Code:
def downloadImage(urldestfolddest=None):

    
rawfilename destfold dest
    filename 
os.path.abspath(rawfilename)
    
#TODO: test if the image already exists
    
    
try:
        
urllib.urlretrieve(urlfilename)
        return 
1
    except
:
        return 

What is the proper path syntax for a folder located in a skin folder (addon) "skin.myskin\newimages"?

Thank you in advance.
Reply
#2
hi, you can't save to the add-on's directory, you must save files to the add-on's folder in the add-on directory. it's a housekeeping rule imposed by XBMC dev team.

here is a code snippet example of how you can construct the path to the add-on data directory.

PHP Code:
import os

#important! imports a wrapper around os.path to deal with 'special://'
import xbmc

#change addonname to match your addon
__addonname__ 'script.module.recaptcha'

__datapath__ os.path.join('special://profile/addon_data/',__addonname__)

#important! deals with a bug where errors are thrown if directory does not exist.
if not os.path.existsos.makedirs(__datapath__

then just use __datapath__ as the folder to which you save and access files
Reply
#3
thank you kindly, much appreciated... that worked! now i'm back in business Smile
Reply
#4
anarchintosh Wrote:hi, you can't save to the add-on's directory, you must save files to the add-on's folder in the add-on directory. it's a housekeeping rule imposed by XBMC dev team.

here is a code snippet example of how you can construct the path to the add-on data directory.

PHP Code:
import os

#important! imports a wrapper around os.path to deal with 'special://'
import xbmc

#change addonname to match your addon
__addonname__ 'script.module.recaptcha'

__datapath__ os.path.join('special://profile/addon_data/',__addonname__)

#important! deals with a bug where errors are thrown if directory does not exist.
if not os.path.existsos.makedirs(__datapath__

then just use __datapath__ as the folder to which you save and access files

i think it might be better using the built in stuff to find that directory?
Code:
import xbmc
import xbmcaddon

#you've probably already done this to get other useful xbmc stuff
addon = xbmcaddon.Addon(id='plugin.video.myaddon')

profile_path = xbmc.translatePath(addon.getAddonInfo('profile'))
(not entirely sure if the translatePath() is needed but it doesn't seem to hurt Wink i have a feeling i had trouble with eden pre if i left it out as it doesn't intercept os.path like dharma used to)

also i think the line to test the directory existing is not correct (the test is wrong):
Code:
try:
    os.makedirs(profile_path)
except:
    pass
will work, and is probably less prone to race conditions.

t0mm0
Reply
#5
Yep, if you are running a nightly version and do any os.path type calls you will see lots of msgs in your logs regarding switching to xbmcaddon.Addon
Reply
#6
I ended up using the code below and all seems to work nicely with no error messages or addon messages in the debug log:
PHP Code:
import urllibos,re
import xbmc

# general variables
__addOnName__ "skin.xbmc.nxe"
__dataPath__ os.path.join('special://profile/addon_data/'__addOnName__)

__GamerAvatarFolder__ __dataPath__ "/gamertag/avatars/"
__GamerPictureFolder__ __dataPath__ "/gamertag/pictures/"
__GameIconFolder__ __dataPath__ "/gamertag/icons/"

__XbmcCurrentUser__ xbmc.getInfoLabel('System.ProfileName')
__XboxGamertag__ __XbmcCurrentUser__.replace(" ","%20")

def downloadImage(urldestfolddest=None):
    
# check if folder exist; otherwise attempt to create it
    
if not os.path.exists(destfold):
        
log('FOLDER TO BE CREATED: ' destfold)
        
os.makedirs(destfold)
    
    
# create full path w/ file name
    
filename destfold dest
    
    
try:
        
# check if the file exists; if it does, there's no need to redownloaded; otherwise download and save
        
if not os.path.isfile(filename):
            
urllib.urlretrieve(urlfilename)
            
log('FILE DOWNLOAD COMPLETED: ' filename)
        else:
            
log('FILE DOWNLOAD SKIPPED: ' filename)
            
        return 
1
            
    except
:
        
log('FILE DOWNLOAD FAILED: ' filename)
        return 

I call the function using the following:
PHP Code:
    # enumerate each item and store recently played data
    
for each in range(len(RecentPlayTitles)):
        
# download & save game icon image; storage path to xbmc custom settings
        
downloadImage(RecentPlayGameIconUrl[each], xbmc.translatePath__GameIconFolder__ ), RecentGameIconName
As previously stated, all works wonderfully Smile

Many thanks to all.
Reply

Logout Mark Read Team Forum Stats Members Help
How Do You Download & Save Image to Skin Directory0