How to parse the payload ?

If you have a question or need help, this is the place to be.
Post Reply
Kleriker
Posts: 14
Joined: Mon Jan 03, 2011 5:39 pm

How to parse the payload ?

Post by Kleriker » Mon Jan 03, 2011 5:56 pm

Hey guys,

i am not new to eventghost but i am new to scripting with python :lol:. The last days i worked hard on my home-automation and i am now at the point, where i want to get my call monitor (janrufmonitor) involved. I am now able the get this data sent to EG as payload: "MSN / Callername / Callernumer / time / date". But now i reached the end of my skills, because i have no idea how to parse the payload to assign each information to its own variable.

Therefore i really hope for your help.

Best regards,
Alex

PS: Sorry for my bad english skills - it is not my native language :mrgreen:

miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: How to parse the payload ?

Post by miljbee » Mon Jan 03, 2011 9:59 pm

eg.event.payload will give you the payload. You can then parse it with str.find or str.split, …
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: How to parse the payload ?

Post by krambriw » Tue Jan 04, 2011 7:22 am

A sample code I believe should work
Best regards

Code: Select all

# "MSN / Callername / Callernumer / time / date"

p = eg.event.payload
p = p.split('/')
caller_msn = p[0].strip()
caller_name = p[1].strip()
caller_nbr = p[2].strip()
caller_time = p[3].strip()
caller_date = p[4].strip()

Kleriker
Posts: 14
Joined: Mon Jan 03, 2011 5:39 pm

Re: How to parse the payload ?

Post by Kleriker » Tue Jan 04, 2011 8:11 am

Thank you very much for your help :)

The Final Script looks like this (a friend was able to help me):

Code: Select all

s = eg.event.payload
res = s[0].split(',')
msn = res[0].split('/')[0]
name = res[1].strip()
phone = res[2].split('/')[1].strip()
date = res[2].split('/')[2]
time = res[2].split('/')[3]
Best regards,
Alex

Kleriker
Posts: 14
Joined: Mon Jan 03, 2011 5:39 pm

Re: How to parse the payload ?

Post by Kleriker » Tue Jan 04, 2011 12:01 pm

Hi krambriw,

i have noticed that your script is working a bit better, so i want to use it. But there is one problem, because the actual payload looks like this:

Code: Select all

[u'MSN/Name/Number/Date/Time']
The problem are the "[ ]". How do i get rid of them with your script ?

Best regards,
Alex

miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: How to parse the payload ?

Post by miljbee » Tue Jan 04, 2011 2:38 pm

[] is for python lists
["test"] is a list containing one string (which is also a list, but that's another story)

Code: Select all

myList=["test"]
myString=myList[0]
u'test' is a unicode string
you can switch between both if needed :
str(u'myUnicodeString')
unicode("myString")
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.

Kleriker
Posts: 14
Joined: Mon Jan 03, 2011 5:39 pm

Re: How to parse the payload ?

Post by Kleriker » Tue Jan 04, 2011 2:54 pm

Hi miljbee,

thank you for your help, but obviously i am to stupid, to bring your explaination into my script. Can you help me please ?
I want to get rid off the [] and " u' " and the " ' " at the end.


Best regards,
Alex

EDIT:

Never mind - it worked :) Thank you !

Code: Select all

z = eg.event.payload
p = z[0]
p = p.split('/')
msn = p[0].strip()
name = p[1].strip()
phone = p[2].strip()
date = p[3].strip()
time = p[4].strip()

Post Reply