Help with my 1st Python Script
#16
(2012-06-05, 22:27)snavaro Wrote:
(2012-06-05, 22:14)vikjon0 Wrote: I am not sure exactly what you mean but you should call delete_file(file) where file is a s string containing the path.
http://docs.python.org/library/os.html

If it does nothing but do not crash I assume it did not pass the if os.path.exists(file):
I suggest that you insert a few print ("hello") to see what goes on.

If it does not run I would suspect that it cannot import the xbmc stuff and I would remove that for now and start with

import os

def delete_file(file):
if os.path.exists(file):
os.remove(file)

used like
delete_file("C:\movies\mymovie,avi")

Here is what I have so far:

Code:
import xbmc, xbmcgui, os,  sys
file = xbmcgui.ListItem.FilenameAndPath
def delete_file(file):
    if os.path.exists(file):
        os.remove(file)
        xbmc.executebuiltin("XBMC.CleanLibrary(video)")

I presume that the variable named "file" needs to pull a string value from the active window, but I am not at all sure that my syntax is correct. In fact, this does NOT work, as it says that FileNameAndPath is not a proper attribute. I will stick a few print statements in to see where it is getting hung up.

I have tried numerous variations for file=xbmcgui.ListItem.FileNameAndPath code, but am still getting syntax errors. Can anyone help me or point me to documentation that would pull this info from the active window?
Reply
#17
I dont know how to get the path, but if the script crashes it could be because it does not have access to the xbmc components.
Like I say in post 14. If you run the script directly in shell it will crash for sure.
Reply
#18
(2012-06-07, 16:02)vikjon0 Wrote: I dont know how to get the path, but if the script crashes it could be because it does not have access to the xbmc components.
Like I say in post 14. If you run the script directly in shell it will crash for sure.

I presumed that by including the xbmcgui library, that the script would have access to the xbmc functions. Maybe I am wrong. The errors I am getting involve syntax errors for the "file = xbmcgui.ListItem.FilenameAndPath" statement, so I think that is where my problem lies.
Reply
#19
okej, then you are fine for now.
The problem I talk about if it happen will make the import xbmcgui crash. That will be pretty clear.
Reply
#20
Success! While this is very rudimentary, it does delete the file and clean the database. Here is what I have:
Code:
import xbmc, xbmcgui, os,  sys
import time
file = xbmc.getInfoLabel( "ListItem.FilenameAndPath" )

print file
def delete_file(file):
    if os.path.exists(file):
        os.remove(file)

delete_file(file)
# Wait 10 seconds for deletions to finish before cleaning.
time.sleep( 10 )

print file
xbmc.executebuiltin("XBMC.CleanLibrary(video)")

What I would like to do is have some notification that the file is being deleted (maybe with a progress bar?) then make the MyVideoNav window have focus. Not sure yet how to do that.
Reply
#21
You might want to use the xbmcvfs.exists(), xbmcvfs.delete() in place of the os.path.exists() and os.remove() as the os module can not handle XBMC virtual filesystem. Also you should use xbmc.sleep( 10000 ) instead of time.sleep( 10 ). Also if you want to print to the log(ie print file) you might want to change it to 'print repr(file)' so Python still print even if it is a UTF-8, Latin-1 character.
Reply
#22
(2012-06-08, 02:53)giftie Wrote: You might want to use the xbmcvfs.exists(), xbmcvfs.delete() in place of the os.path.exists() and os.remove() as the os module can not handle XBMC virtual filesystem. Also you should use xbmc.sleep( 10000 ) instead of time.sleep( 10 ). Also if you want to print to the log(ie print file) you might want to change it to 'print repr(file)' so Python still print even if it is a UTF-8, Latin-1 character.

Thanks for your pointers giftie. I will try those out. Is there a xbmcvfs library that I need to import for those commands?
Reply
#23
import xbmcvfs will give you access to those.

For notifications, and giving focus to a window, there are builtins for both. Notification and ActivateWindow should get you there: http://wiki.xbmc.org/index.php?title=Lis..._functions
Reply
#24
Two small questions that I could use some help on, and then I think that this will be ready for prime time.
1. What is the easiest way in python to tell whether a file is a movie or tv show? I am trying to pull the Title of the selected video/show and there are 2 choices: ListItem.Title and ListItem.TVShowTitle, depending on the type of video it is.
2. Currently, the notification box appears in the lower right hand corner. How can I move it to the center of the screen?
Reply
#25
2) I think it is skin dependent as nox display the same notification in another place

If there is no better answer on 1) I guess you need to pull both and have the script lock at the result and determine what is what.
If one crash you need to use a try cluase and when you catch the error you know it is the other one.
Reply
#26
(2012-06-08, 19:02)vikjon0 Wrote: 2) I think it is skin dependent as nox display the same notification in another place

If there is no better answer on 1) I guess you need to pull both and have the script lock at the result and determine what is what.
If one crash you need to use a try cluase and when you catch the error you know it is the other one.

I think I figured out a way to solve the 1st question. Still would like to find a way to move the notification in Confluence. Here is my latest script:

Code:
import xbmc, xbmcgui, os,  sys
import xbmcvfs
file = xbmc.getInfoLabel( "ListItem.FilenameAndPath" )
title1 = xbmc.getInfoLabel( "ListItem.TVShowTitle" )
title2 = xbmc.getInfoLabel( "ListItem.Title" )
#either title1 or title2 will be a null string
title = title1 + title2

def delete_file(file):
    if xbmcvfs.exists(file):
        xbmcvfs.delete(file)
# Screen notification of file deletion process
xbmc.executebuiltin("XBMC.Notification(%s, %s, %i)" % ('Deleting File',title + ' is being deleted.',10000))
delete_file(file)
#comment out above line while testing
# Wait 5 seconds for deletions to finish before cleaning.
xbmc.sleep( 5000 )

# Check if the library is being updated before cleaning up
while (xbmc.getCondVisibility("Library.IsScanningVideo")):
    xbmc.sleep(5000)
    xbmc.executebuiltin("Notification(Pausing,Waiting Until Library Scan Completes...,5000)")
    
xbmc.executebuiltin("XBMC.CleanLibrary(video)")

#Refresh video picklist to show list without deleted video
xbmc.executebuiltin("ActivateWindow(10025)")

Also, I am unable to get the last line to execute, though I am not getting an error.
In order to implement the script, I have done the following:
1. Changed the "Refresh" label to "Delete" in strings.xml
2. Added a Settings option to Delete Show After Watching in Custom Skin Settings_1111 with default as off
3. Added a Delete Button to DialogVideoInfo (visible only if #2 above is set to on) and bound this script to the button via onclick.
4. Added additional onclick to VideoOSD Stop button to bring up DialogVideoInfo screen.
Reply
#27
Awesome work snavaro, I'll try it out when I get home tonight. Any chance you can create a unified diff file, so we can use "patch" to install your script?
Reply
#28
teaguecl,
Your question went right over my head. Confused

Remember I am a newbie at python scripts (and most other things as well).
Reply
#29
The notification location is skin dependent so you can't move it without editing the skin

ActivateWindow isn't working because it's cached so it's showing the last known state (so technically it's working). If you're just trying to update the last window you were in before you started playback, use the builtin "Container.Refresh"

Also, instead of waiting 5 seconds for the delete to finish, use something like:
Code:
while xbmcvfs.exists(file):
    xbmc.sleep(1000)

That way you can account for things like deleting items from a busy NAS drive that might take more that 5sec.

Finally, you can also check the Season and Episode infolabels to be more certain it's a TV Show vs a movie
Reply
#30
(2012-06-09, 21:28)Bstrdsmkr Wrote: The notification location is skin dependent so you can't move it without editing the skin

ActivateWindow isn't working because it's cached so it's showing the last known state (so technically it's working). If you're just trying to update the last window you were in before you started playback, use the builtin "Container.Refresh"

Also, instead of waiting 5 seconds for the delete to finish, use something like:
Code:
while xbmcvfs.exists(file):
    xbmc.sleep(1000)

That way you can account for things like deleting items from a busy NAS drive that might take more that 5sec.

Finally, you can also check the Season and Episode infolabels to be more certain it's a TV Show vs a movie

You don't want any while loop without xbmc.abortRequested in it.
So it should be:
Code:
while xbmcvfs.exists(file or xbmc.abortRequested):
    xbmc.sleep(1000)

It should always listen if XBMC wants to shutdown or else it could lock you HTPC when something goes wrong
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply

Logout Mark Read Team Forum Stats Members Help
Help with my 1st Python Script0