Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

How to parse event.payload to get filename without the path?

If you have a question or need help, this is the place to be.
Post Reply
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

How to parse event.payload to get filename without the path?

Post by Flook »

I have eventghost sending me an email whenever I've completed downloading a new TV episode using the email plugin. It works great but I would like to add the episode name in the subject line of the email and am able to use the expressions like {eg.event.payload} to do it. The problem is that I get the entire path along with the filename and it's a long path.

Just wondering if there is an easy way to subtract the entire path from it.

Example of the subject line of my email.
New Episode of {eg.event.payload}
That gives me:
New Episode of (u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi',)
I would like New Episode of exampleFileName

Thanks
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to parse event.payload to get filename without the path?

Post by Pako »

Code: Select all

from os import path
#fp = eg.event.payload
fp = u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi'
head,tail = path.split(fp)
print tail
exampleFileName.avi
name,ext = path.splitext(tail)
print name
exampleFileName
Pako
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Thanks for the reply.
Pako wrote:

Code: Select all

from os import path
#fp = eg.event.payload
fp = u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi'
head,tail = path.split(fp)
print tail
exampleFileName.avi
name,ext = path.splitext(tail)
print name
exampleFileName
Pako
I'm trying to do some reading on python to understand this better but I'm very new to python. If I copy and paste this code into a python script action item in eventghost I get a line 6 name error that examplefile.avi is not defined. The script seams to be specific to the example name I gave but it needs to be able to find the file name itself within the eg.event.payload. It needs to take away the u'\\\\HTPC\\Torrent-Finished\\TV\\ and just give me the file name that could be anything. In my example it's exampleFileName.avi but the script cannot be specific to that one file name. Again I know nothing of python and could be just missing everything.

I also don't understand why line 3, line 6 and line 9 even exist. Line 2 looks like it sets the variable called fp (short for file path) and then line 3 spells out a static path for fp. Why would there be a static path with the file name included that would need to change for every new file that enters the directory. fp = eg.event.payload looks perfect. I see print tail in line 5 and it needs to get the tail based on the constantly changing event.payload path. Have no clue about line 7. Line 8 looks like would give me my file name and line 9 for some reason spells out a static one. I was thinking you put 3, 6, and 9 there to show me what each line would give me so I deleted them. Then I got an error saying fp variable was not set so I added fp = eg.event.payload after #fp = eg.event.payload. Then I get a type error related to the head, tail line. So as you can see I have no idea what I'm doing but really appreciate the help.

Thanks
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to parse event.payload to get filename without the path?

Post by Pako »

I apologize. I had no idea that you know nothing about Python.
To understand my contribution is important to know that when a line starts with "#", it's just a comment.
Correctly it must be as follows:

Code: Select all

from os import path
fp = eg.event.payload
#fp = u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi'
head, tail = path.split(fp)
#print tail
name, ext = path.splitext(tail)
#print name
entireSentence  = "New Episode of %s" % name
The file name is of course in the variable "name".
Result (variable entireSentence) in this case New Episode of exampleFileName.
You can of course instead of "entireSentence" write "eg.result" and with the string eg.result
then you can work on other actions that follow after the Python script.
Pako
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Looks good. This is exactly what I ended up with except the last line. In my other post I said when I put fp = eg.event.payload then there is an error on the next line. Here is the error I get from my eventghost log:

Code: Select all

DirectoryWatcher3.Created (u'\\\\HTPC\\Torrent-Finished\\TV\\New File',)
Python Script
   Traceback (most recent call last):
     Python script "31", line 4, in <module>
       head, tail = path.split(fp)
     File "ntpath.pyc", line 181, in split
   TypeError: cannot concatenate 'str' and 'tuple' objects
One thing I was thinking was that could the eg.event.payload be pulling the event of the python script. In the log I have the creation of the tv show then I execute this python script which creates its own log and then does eg.event.payload. Do I need to make it pull from two events back?

Thanks
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to parse event.payload to get filename without the path?

Post by Pako »

It's clear. The problem is the character "," (comma) at the end of the path.
Solutions should be easy:
Instead fp = eg.event.payload write fp = eg.event.payload[: -1].
Pako
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Getting this error still

DirectoryWatcher3.Updated (u'\\\\HTPC\\Torrent-Finished\\TV\\New File_3',)
Python Script
Traceback (most recent call last):
Python script "42", line 4, in <module>
head, tail = path.split(fp)
File "ntpath.pyc", line 181, in split
TypeError: cannot concatenate 'str' and 'tuple' objects

Here is the python script

Code: Select all

from os import path
fp = eg.event.payload[: -1]
#fp = u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi'
head, tail = path.split(fp)
#print tail
name, ext = path.splitext(tail)
#print name
entireSentence  = "New Episode of %s" % name
It doesn't seam to like what's going on in line 4.
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Code: Select all

from os import path
fp = eg.event.payload[: -1]
print
#fp = u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName.avi'
head, tail = path.split(fp)
#print tail
name, ext = path.splitext(tail)
#print name
entireSentence  = "New Episode of %s" % name
Gives me this in eg log:

DirectoryWatcher3.Updated (u'\\\\HTPC\\Torrent-Finished\\TV\\New File_2',)
Python Script
#This line is blank with a little balloon with the letter i inside of it on the left. With the print command shouldn't this give me what fp=.
Traceback (most recent call last):
Python script "68", line 5, in <module>
head, tail = path.split(fp)
File "ntpath.pyc", line 181, in split
TypeError: cannot concatenate 'str' and 'tuple' objects
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to parse event.payload to get filename without the path?

Post by Pako »

I'm sorry, but I lack information.
I need to know what exactly is in a variable payload.
At the beginning of the script type the following line: print "payload =", eg.event.payload (including the quotes).
Then post here the entire line from logger (the line, that starts: payload =).
Pako
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

That was the print command I was looking for. Here is the result.
Python Script
payload = (u'\\\\HTPC\\Torrent-Finished\\TV\\New File',)
Traceback (most recent call last):
Python script "75", line 5, in <module>
head, tail = path.split(fp)
File "ntpath.pyc", line 181, in split
TypeError: cannot concatenate 'str' and 'tuple' objects

I was thinking the head, tail stuff handles all of the slashes in the network path differently than a normal system path but I don't really know. I'm currently trying to find a debugger of some sort or something to visually show me how each line is parsing the path.
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Got this from the python forums.

change: head, tail = path.split(fp)
to:head, tail = path.split(fp[0])

Tried it but then get another error:
Python Script
Traceback (most recent call last):
Python script "77", line 4, in <module>
head, tail = path.split(fp[0])
IndexError: tuple index out of range

I also tried to change:
fp = eg.event.payload[: -1] to fp = eg.event.payload

No error after that but my output is empty. No exampleFileName.avi. Just says python script in the log.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: How to parse event.payload to get filename without the path?

Post by Pako »

Do this:

Code: Select all

from os import path
print "payload =",eg.event.payload
fp = eg.event.payload[0]
print "fp =", fp
head, tail = path.split(fp)
print "tail =", tail
name, ext = path.splitext(tail)
print "name", name
eg.result  = "New Episode of %s" % name
And post here logger content.
Pako
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Oh yea! Looks good with the [0]

DirectoryWatcher3.Updated (u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName_2.avi',)
Python Script
payload = (u'\\\\HTPC\\Torrent-Finished\\TV\\exampleFileName_2.avi',)
fp = \\HTPC\Torrent-Finished\TV\exampleFileName_2.avi
tail = exampleFileName_2.avi
name exampleFileName_2
lol you posted your reply seconds after someone at the python forums and I think he was trying to same the same thing. Here is what he put:

OK. I think I missed this: fp = eg.event.payload[: -1]
I'm not familiar with this module but it seems that fp is empty:

Code: Select all
>>> a=("a",)
>>> a[:-1]
()
>>> a[0]
'a'

Thanks a lot for the help. I do ok with most scripting and programing but python looks really foreign when I look at it. More practice needed.

edit: so when I deleted [: -1] earlier I think it was working. I just didn't know without the print commands.
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Spent forever now trying to get this in my email subject line of my email.

Is there a way to get this code in one python line or a way to refer to the result of this script or can I save this as a .py file and load it in a python expression that I run in the subject line?

Code: Select all

from os import path
fp = eg.event.payload[0]
head, tail = path.split(fp)
name, ext = path.splitext(tail)
eg.result  = "New Episode of %s" % name
When you hover over the subject line of the email plugin it says "Here can be expression as {eg.event.payload} too!
How do I get all of that code that works in a python script in one line. After parsing code has run in a python script action item I've tried to use eg.result in the subject line of the email action item and I get "none" as a result.

Thanks for the help and sorry about all the questions.
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: How to parse event.payload to get filename without the path?

Post by Flook »

Maybe I need set a global variable in the python script based on the result and then access that variable in the expression. I'll try that out and see what happens.

edit: Hell yea! It worked.

Code: Select all

from os import path
fp = eg.event.payload[0]
head, tail = path.split(fp)
name, ext = path.splitext(tail)
eg.result  = "New Episode of %s" % name
eg.globals.testVar = eg.result
Then put {eg.globals.testVar} in my subject line of the email plugin configuration and then BAM! Worked like a charm. Perfect adition to my fully automated HTPC virtual machine. It now automatically:
1. downloads tv shows, emails me what show I just got, copies, renames, moves, downloads library data, seeds for two days then deletes original files.
2. un packs movies, renames, moves, downloads library data, seeds for 2 days then deletes original files.

A more detailed look at the setup here. http://forum.xbmc.org/showthread.php?t=70530

Thanks a lot for your help Pako
Post Reply