Beginner need help
#1
I've been trying to put something together from other threads i found via google.
I wanted to do some basic communication between skin, python and http.

Type in a domain and use a button to send the string
Code:
<onclick>RunScript(script.myscript, skinstring1=$INFO[Skin.String(my_domain)])</onclick>

Then the script
Code:
import xbmc
import xbmcgui
import socket

# make param out of arg.
try:    
    # parse sys.argv for params    
    try:params = dict( arg.split( "=" ) for arg in sys.argv[ 1 ].split( "&" ) )    
    except:params =  dict( sys.argv[ 1 ].split( "=" ))
except:    
    # no params passed    
    params = {}

# make s short for socket.socket.
s = socket.socket(socket.af_inet,socket.sock_dgram)

# connect to domain on port 80 and toggle setting.
def my_timer():
    try:
    s.connect((skinstring1,80))
    s.close()
    xbmc.executebuiltin("Skin.ToggleSetting(Site_Alive)")
    except:
    xbmc.executebuiltin("Skin.Reset(Site_Alive)")

# keep repeating this every x until xbmc closes.
gobject.timeout_add( 60*1000, my_timer )

So what things am i doing wrong?
Reply
#2
Is there a script error? Those are usually the first place to start.

I dont know what it is you are trying to do, but this is what I think looks wrong.

1. I dont think you need the second level of try: and except:
2. when calling your function my_timer, you need to follow if by (). So it should be my_timer()
3. It might just be the formatting here, but your indentation is wrong.
4. You dont have any loop.
Reply
#3
I'm also suspicions about that last gobject line. There's nothing in the imports about this.

As Karnagious said, posting the errors would be helpful.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#4
Tried to apply your feedbacks (thnx btw).
It's just an exercise but i was trying to change a skin setting if a (game) server is down.
Why don't i have a loop?
Current error: Error Contents: No module named gobject

Code:
import xbmc
import xbmcgui
import socket
import gobject

s = socket.socket(socket.af_inet,socket.sock_dgram)

def my_domain():
    try:
        params = dict( arg.split( "=" ) for arg in sys.argv[ 1 ].split( "&" ) )    
    except:
        params =  dict( sys.argv[ 1 ].split( "=" ))
  
def my_timer():
    try:
        s.connect((skinstring1,80))
        s.close()
        xbmc.executebuiltin("Skin.ToggleSetting(Site_Alive)")
    except:
        xbmc.executebuiltin("Skin.Reset(Site_Alive)")

my_domain()
gobject.timeout_add( 60*1000, my_timer() )
Reply
#5
you need to have a python module "gobject".
if you don't have that installed it won't work.
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
#6
So you can't really use that in an addon then, because not everyone has it?
Reply
#7
Why do you need it to repeat every 60 seconds if you trigger the script by clicking a button?

If you want it to run all the time, until xbmc closes, then have a look at service addons. You could then use
Code:
xbmc.sleep(60000)
to sleep for 1 minute. Put this and the call to the myTimer function in a loop (see http://wiki.xbmc.org/index.php?title=How...g_services) and that should do it.

I'd also think about including a settings file to set/store the value of your server, but then I'm not sure how you're creating the my_domain skin string value.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#8
(2013-12-22, 05:24)Ineedsah4wk Wrote: So you can't really use that in an addon then, because not everyone has it?

You can include it in your add-on but, the preferred method is to make a script.module add-on for it, if it would be useful for others.
Reply
#9
(2013-12-22, 10:56)el_Paraguayo Wrote: Why do you need it to repeat every 60 seconds if you trigger the script by clicking a button?

If you want it to run all the time, until xbmc closes, then have a look at service addons. You could then use
Code:
xbmc.sleep(60000)
to sleep for 1 minute. Put this and the call to the myTimer function in a loop (see http://wiki.xbmc.org/index.php?title=How...g_services) and that should do it.

I'd also think about including a settings file to set/store the value of your server, but then I'm not sure how you're creating the my_domain skin string value.
Your link does not have any content. But following another service addon i'm using the following which seems to work.
Code:
<extension point="xbmc.service" library="default.py" start="startup" />

I made a custom xml window in the skin where i pop the keyboard for input
Code:
<onclick>Skin.SetString(domain)</onclick>
And i had a button to send the string to the script as arg. I thought this would be good so it would not be running if there was no string.
But I rewrote to just get the string from the skin. Check for http so it at least is not empty. And use property set to true or false to prevent error in toggle.

Code:
import xbmc
import xbmcgui
import time
import socket

my_domain = xbmc.executebuiltin('skin.string(domain)')
s = socket.socket(socket.af_inet,socket.sock_dgram)

def my_function():
    if "http" in my_domain:
        try:
            s.connect((my_domain,80))
            s.close()
            xbmcgui.Window(10000).setProperty('site_Alive', 'True')
            time.sleep(60000)
            my_function()
        except:
            xbmcgui.Window(10000).setProperty('site_Alive', 'False')
            time.sleep(60000)
            my_function()
    else:
        time.sleep(60000)
        my_function()
        
my_function()

divingmule i think that would still be a bit over my head.

Current error:
AttributeError: 'module' object has no attribute 'af_inet'

Is socket even the best way to go? Would it be easier to ping?
Reply
#10
Weird. That link was fine when I posted it. I checked.

As for your error, I think it should be socket.AF_INET and socket.SOCK_DGRAM ie they need to be in capitals.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#11
As for that link, the software has added a closing bracket to the link. If you remove the final ')' the link should work.
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#12
Great, that seems to fix it. Now in the following line i'm getting:
Code:
if "http" in my_domain:
gives the following error:
TypeError: argument of type 'NoneType' is not iterable

If i try to resolve this with either of the most common suggestions on google
Code:
if my_domain is not None and "http" in my_domain:
if my_domain and "http" in my_domain:
I have trouble closing XBMC and the error is
script didn't stop within x

Even if i manually set the string in the skin it doesn't want to continue and set the window property.
Reply
#13
Looked at your link. Can't seem to make abort work.
Also, nothing seems to actually do anything...
Code:
import xbmc
import xbmcgui
import time
import socket

def my_function():
    while (not xbmc.abortRequested):
        my_domain = xbmc.executebuiltin('skin.string(Current.VPN)')
        s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        if (xbmc.abortRequested):
            xbmc.log("Aborting...")
            break
        elif my_domain and "http" in my_domain:
            xbmc.log("domain exists and has http")
            try:
                s.connect((my_domain,80))
                s.close()
                xbmc.log("site is up")
                xbmcgui.Window(10000).setProperty('site_Alive', 'True')
                time.sleep(60000)
                my_function()
            except:
                xbmc.log("site is down")
                xbmcgui.Window(10000).setProperty('site_Alive', 'False')
                time.sleep(60000)
                my_function()
        else:
            time.sleep(60000)
            xbmc.log("retry")
            my_function()

my_function()
Reply
#14
I'm not an expert but I suspect looping by having the function call itself might be an issue. Wouldnt it just keep adding a new instance of the function?

The 'while' will take care of the looping for you. I recommend taking out each time.sleep(60000)* and the my_function calls, and simply add xbmc.sleep(60000) to the end of the function (within the 'while' loop).

You also dont need the lines "if (xbmc.abortRequested):". You can simply add the log entry "xbmc.log("Aborting...")" to the very bottom of your script.

*there is no need to have both time and xbmc imported. The xbmc module has its own sleep function.
Reply
#15
You are correct.
Aaaand that is also not how you get a skin string.. duh.
Which is what was causing the if "http" in my_domain: to fail

thnx guys
Reply

Logout Mark Read Team Forum Stats Members Help
Beginner need help0