urllib2.urlopen issue - Please help
#1
Hi everyone,

I am VERY new to python development, I have also searched the web high and low for some direction on the issue I am having. Any help or a point in the right direction is greatly appreciated.

All I am trying to do is to trigger the URL (btw, this works perfectly in any web browser) to trigger a scene using my INSTEON Hub (turn lights on when movie starts playing etc). The best I can find is I may need to add header information to the urllib2.urlopen url? Not too sure, tried everything, any help would be GREATLY appreciated. Thank you.

Python script:

PHP Code:
import xbmc,xbmcgui                                                                                                                                                                      
import subprocess
,os
import urllib2
import xbmcaddon
import json
import time

#Initialize ADDON
settings xbmcaddon.Addon(id='insteon.addon')

#Initialize ADDON INFORMATION
ip      =  settings.getSetting"hub_ip" )
user    =  settings.getSetting"hub_user" )
pword   =  settings.getSetting"hub_pword" )


__addonname__     settings.getAddonInfo('name')
__icon__          settings.getAddonInfo('icon')
plugin_start "Plugin started"
plugin_error "Please insert your INSTEON Hub information in the addon setting"
time1        5000
time2        
3000

url 
'http://admin:[email protected]:25105/0?1115=I=0'

class MyPlayer(xbmc.Player) :

        
def __init__ (self):
            
xbmc.Player.__init__(self)

        
def onPlayBackStarted(self):
            if 
xbmc.Player().isPlayingVideo():
               
request urllib2.urlopen('http://admin:[email protected]:25105/0?1115=I=0')


player=MyPlayer()

VIDEO 0

while(1):
    if 
xbmc.Player().isPlaying():
      if 
xbmc.Player().isPlayingVideo():
        
VIDEO 1

      
else:
        
VIDEO 0

    xbmc
.sleep(1000

This is the part I need help with (I think)...

PHP Code:
request urllib2.urlopen('http://admin:[email protected]:25105/0?1115=I=0'


I also tried

PHP Code:
urllib2.urlopen('http://admin:[email protected]:25105/0?1115=I=0'

The error log...

Code:
15:14:50 T:4500549632   ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
                                             - NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
                                            Error Type: <class 'urllib2.URLError'>
                                            Error Contents: <urlopen error [Errno 8] nodename nor servname provided, or not known>
                                            Traceback (most recent call last):
                                              File "/Users/Brad/Library/Application Support/XBMC/addons/insteon.addon/default.py", line 33, in onPlayBackStarted
                                                request = urllib2.urlopen(url)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 126, in urlopen
                                                return _opener.open(url, data, timeout)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 391, in open
                                                response = self._open(req, data)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 409, in _open
                                                '_open', req)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 369, in _call_chain
                                                result = func(*args)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 1161, in http_open
                                                return self.do_open(httplib.HTTPConnection, req)
                                              File "/Applications/XBMC.app/Contents/Frameworks/lib/python2.6/urllib2.py", line 1136, in do_open
                                                raise URLError(err)
                                            URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>
                                            -->End of Python script error report<--
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#2
I found that you can do better with the requests module.

Code:
import requests
from requests.auth import HTTPBasicAuth

    def insteon_direct( self, ip = "0.0.0.0", port = "25105", username = "", password = "", command = "" ):
        auth=HTTPBasicAuth( username, password )
        url = u'http://%s:%s/3?%s=I=3' % ( ip, port, command )
        utils.log( url )
        r = requests.post(url=url, auth=auth)
Reply
#3
thank you for your replay. Now it seems I am in way over my head. It would appear that I need to somehow install the requests module (http://docs.python-requests.org/en/latest/). I am unsure how to do this with XBMC or even if it is wise in using this to create an XBMC Addon? It appears there is a package for XBMC (https://github.com/beenje/script.module.requests) however I am assuming that someone would need to have this installed in order to use this addon. Anyone who could help with urllib2.urlopen it would be greatly appreciated. I am sure this has successfully been used in other scripts (I have seen it working!).

Thanks.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#4
There's some info on basic authentication with the urllib2 module in the python docs.

Have a look here http://docs.python.org/2/howto/urllib2.html#id6

However, experience also tells me that whatever Giftie says is usually the right way to go!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#5
(2014-01-25, 09:29)Bradles73 Wrote: thank you for your replay. Now it seems I am in way over my head. It would appear that I need to somehow install the requests module (http://docs.python-requests.org/en/latest/). I am unsure how to do this with XBMC or even if it is wise in using this to create an XBMC Addon? It appears there is a package for XBMC (https://github.com/beenje/script.module.requests) however I am assuming that someone would need to have this installed in order to use this addon. Anyone who could help with urllib2.urlopen it would be greatly appreciated. I am sure this has successfully been used in other scripts (I have seen it working!).

Thanks.
No, it's wise to use it. Just add <import addon="script.module.requests" version="1.1.0"/> to your addon.xml
Reply
#6
To clarify what takoi said, if you add that to your addon.xml in the requires section, it'll the requests lib will be automatically downloaded and installed from the official repo. Here's a list of what's available there: http://mirrors.xbmc.org/addons/frodo/
Keep in mind that dependencies like that are only installed when you use the "Install from zip" feature or download the addon from within xbmc, not if you just copy your addon into the addons folder
Reply
#7
Very wise indeed, thank you giftie and takoi. And a thank you to you Bstrdsmkr for addressing my concern in using this solution. All of your kind assistance is sincerely appreciated.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#8
Hi,

I have been trying to this to work for the past 24 hours and I am now officially at a dead end.

Based on the advice above, I have included the requests module. The code in question (which is simply trying to send an http post command to an INSTEON Hub looks like this:

Code:
def onPlayBackStarted(self):
                if xbmc.Player().isPlayingVideo():
                        def insteon_direct( self, ip = "192.168.2.107", port = "25105", username = "admin", password = "xxxx", command = "1115" ):
                                  auth=HTTPBasicAuth( username, password )
                                  url = 'http://%s:%s/0?%s=I=0' % ( ip, port, command )
                                  utils.log( url )
                                  r = requests.post(url=url, auth=auth)

I have tried lots (and I mean lots) of variations, searched the web and I simply cannot get this to work (there do not seem to be any errors in the xbmc log).

Any help is greatly appreciated. Thank you.[/quote]
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#9
Is your addon running in a loop? Your script has to still be running when playback starts in order to receive the event. You'll likely want this to be running as a service to make that easy.
Reply
#10
Yes, the addon is running in a loop. If I replace the Request with a Notification, it works fine. I am going to do a bit more study and brush up on some Python Wink It has been a long time (at least 10 years) since I used Python (and only in the Zope environment). If (rather: when) I figure it out I will be sure to post the answer here in case it can help someone else. Thanks Bstrdsmkr, your help is appreciated.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#11
(2014-01-27, 07:38)Bradles73 Wrote: Hi,

I have been trying to this to work for the past 24 hours and I am now officially at a dead end.

Based on the advice above, I have included the requests module. The code in question (which is simply trying to send an http post command to an INSTEON Hub looks like this:

Code:
def onPlayBackStarted(self):
                if xbmc.Player().isPlayingVideo():
                        def insteon_direct( self, ip = "", port = "25105", username = "admin", password = "xxxx", command = "1115" ):
                                  auth=HTTPBasicAuth( username, password )
                                  url = 'http://%s:%s/0?%s=I=0' % ( ip, port, command )
                                  utils.log( url )
                                  r = requests.post(url=url, auth=auth)

I have tried lots (and I mean lots) of variations, searched the web and I simply cannot get this to work (there do not seem to be any errors in the xbmc log).

Any help is greatly appreciated. Thank you.

First you need to move the insteon_direct function out of onPlayBackStarted.

Code:
def insteon_direct( ip = "192.168.2.107", port = "25105", username = "", password = "", command = "" ):
    auth=HTTPBasicAuth( username, password )
    url = 'http://%s:%s/0?%s=I=0' % ( ip, port, command )
    #utils.log( url )
    r = requests.post(url=url, auth=auth)
Then call it from your onPlayBackStarted function:
Code:
class MyPlayer( xbmc.Player ):
    def __init__(self, *args, **kwargs):
        xbmc.Player.__init__( self )
        self.enabled = kwargs['enabled']
    
    def onPlayBackStarted( self ):
        # Will be called when xbmc starts playing a file
        if xbmc.Player().isPlayingVideo():
            insteon_direct( ip = "192.168.2.107", port = "25105", username = "admin", password = "xxxx", command = "1115" )

    def onPlayBackEnded( self ):
        # Will be called when xbmc stops playing a file
        
    def onPlayBackStopped( self ):
        # Will be called when user stops xbmc playing a file
        
    def onPlayBackPaused( self ):
        # Will be called when user pauses xbmc playing a file
You will also need a daemon function for the loop(there are other ways to do this as well)
Code:
def _daemon( ):
    while ( not xbmc.abortRequested ):
        xbmc.sleep( 250 )
Then you need the start of the script, or the end.. Smile
Code:
if (__name__ == "__main__"):
    Player = MyPlayer( enabled = True )
    _daemon()
    del Player

Then as Bstrdsmkr said, you will need this as a service addon so it will start with XBMC.
Reply
#12
Hi all,

Well after a few sleepless days - my first XBMC Addon is ready! Thank you for everyones help > I am looking for further feedback and input.

I have started a new thread http://forum.xbmc.org/showthread.php?tid=185129

I am looking forward to feedback and improving the addon.

Thanks again.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#13
In you addon you should change the While clause to this:
Code:
while( not xbmc.abortRequested ):

This allows XBMC to end the script when it is trying to quit.

Also, you can reduce the repetitive code by separating it out into a separate function(all the Insteon core communication). Just reduces your code and helps troubleshooting..
Reply
#14
Thanks giftie, I have implemented the while loop change Smile

With regard to the separate functions - will give this a go. This makes a lot of sense as it is only really the INSTEON Scene ID that changes for each call - it would certainly clean up the code a lot. Thanks again.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply
#15
I have moved this question to its own post (this post is getting a bit off-topic)

Ok - getting somewhere. I am now looking to create a popup window dialog for my addon

Code:
import xbmc,xbmcgui

#get actioncodes from https://github.com/xbmc/xbmc/blob/master/xbmc/guilib/Key.h
ACTION_PREVIOUS_MENU = 10

class MyClass(xbmcgui.WindowDialog):
    def __init__(self):
        background = xbmcgui.ControlImage(40, 40, 270, 154, 'ContentPanel.png')
        self.addControl(background)
        self.strActionInfo = xbmcgui.ControlLabel(100, 400, 200, 200, '', 'font13', '0xFFFF00FF')
        self.addControl(self.strActionInfo)
        self.strActionInfo.setLabel('')
        self.button0 = xbmcgui.ControlButton(42, 42, 270, 30, shortName1)
        self.addControl(self.button0)
        self.button1 = xbmcgui.ControlButton(42, 72, 270, 30, shortName2)
        self.addControl(self.button1)
        self.button2 = xbmcgui.ControlButton(42, 102, 270, 30, shortName3)
        self.addControl(self.button2)
        self.button3 = xbmcgui.ControlButton(42, 132, 270, 30, shortName4)
        self.addControl(self.button3)
        self.button4 = xbmcgui.ControlButton(42, 162, 270, 30, shortName5)
        self.addControl(self.button4)
        self.setFocus(self.button0)
        self.button0.controlDown(self.button1)
        self.button0.controlUp(self.button4)
        self.button1.controlUp(self.button0)
        self.button1.controlDown(self.button2)
        self.button2.controlUp(self.button1)
        self.button2.controlDown(self.button3)
        self.button3.controlUp(self.button2)
        self.button3.controlDown(self.button4)
        self.button4.controlUp(self.button3)
        self.button4.controlDown(self.button0)

    def onAction(self, action):
        if action == ACTION_PREVIOUS_MENU:
            self.close()

    def onControl(self, control):
        if control == self.button0:
            self.close()
        if control == self.button1:
            self.close()
        if control == self.button2:
            self.close()
        if control == self.button3:
            self.close()
        if control == self.button4:
            self.close()

mydisplay = MyClass()
mydisplay .doModal()
del mydisplay

The above code works nicely. However I would like to activate this window by using a keypress combination (something not used by XBMC - eg Ctrl + U).

As best I can tell I need to give the window dialog an ID, I then then need to create a custom keymap that opens the window ID (I may be way off here, I think this is how I would do it).

I would like to achieve the above programatically, as in I do not want to have to go in and change keymap.xml manually.

You can see what I am talking about by downloading the latest version of my addon here < you will notice that this window is displayed as soon as the addon is started (this is not what I want, I only want it to display on a keypress combination).

Any pointers would be greatly appreciated. Thank you.
The Internet isn't free. It just has an economy that makes no sense to capitalism.
Reply

Logout Mark Read Team Forum Stats Members Help
urllib2.urlopen issue - Please help0