[Question] Writing plugin that requires login info
#1
Smile 
Hi,

I am new to writing XBMC plugins, and have written one successful plugin so far, mostly for learning purposes. I just took on a new project to write a plugin that would first have to login to a site using username and password. I think I have figured that part out, but I think when I try to access the actual page that has the stream, I get the response as if the site did not take my login info, or somehow lost it. I would think, I would probably need to do something along the cookies side, but don't know how... Below is my code so far that I think works. Can someone be kind enough to guide me what else I need to do to make it work?

Below is the code... At the end the link statement should show me MMS links in between if the site logs me in, else it shows the same page source except the MMS link, which is what I am getting...

import os,sys,urllib2,re,mechanize,ClientForm

url='http://asx.jumptv.com/playlist.asx?lang=en&productId=291'
# Login Site
loginurl='http://www.jumptv.com/en/signin?wid=9074'

username='<actual username>'
password='<actual password>'

print 'Login Started'

browser = mechanize.Browser()
lreq = urllib2.Request(loginurl)
lreq.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
lresponse = urllib2.urlopen(lreq)

forms = ClientForm.ParseResponse(lresponse, backwards_compat=False)
form = forms[0]
form['data[SignIn][Account][signInName]'] = username
form['data[SignIn][Account][password]'] = password

browser.open(form.click())

print 'Login Finished'

req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()

link
Reply
#2
Star 
One more thing I just realized was that when I submit the command "browser.open(form.click())" the site responds with unknown browser header. Even though I do specify a header, do I need to do something else to send the proper header as part of the command "browser.open(form.click())"?

I have also tried the same code with urllib2 commands swapped with ClientCookie with similar results... :confused2:
Reply
#3
you might have better luck using HTTP Basic Authentication

Here's some sample code I've used successfully:

Code:
        # password manager handler
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, self.URL_BASE, user, pwd)

    # Basic Auth. handler
    authhandler = urllib2.HTTPBasicAuthHandler(passman)

    # Cookie handler
    cookiejar = cookielib.LWPCookieJar()
    cookiehandler = urllib2.HTTPCookieProcessor(cookiejar)

    # add our handlers
    urlopener = urllib2.build_opener(authhandler, cookiehandler)

    # install urlopener so all urllib2 calls use our handlers
    urllib2.install_opener(urlopener)
Retired from Add-on dev
Reply
#4
Star 
BigBellyBilly Wrote:you might have better luck using HTTP Basic Authentication

Here's some sample code I've used successfully:

Code:
        # password manager handler
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, self.URL_BASE, user, pwd)

    # Basic Auth. handler
    authhandler = urllib2.HTTPBasicAuthHandler(passman)

    # Cookie handler
    cookiejar = cookielib.LWPCookieJar()
    cookiehandler = urllib2.HTTPCookieProcessor(cookiejar)

    # add our handlers
    urlopener = urllib2.build_opener(authhandler, cookiehandler)

    # install urlopener so all urllib2 calls use our handlers
    urllib2.install_opener(urlopener)

Hi,

Thanks for your response. So I changed the code to look like below only for testing... Assuming above will handle the cookies automatically... I tried just the login url...

When I go through the execution, and print the value of 'link' variable, I get the login page source. There is a button for 'Sign In', how do I activate that using this?

import os,sys,urllib2,re,cookielib

# Login Site
loginurl = 'www.jumptv.com/en/signin'

username='USER'
password='PASS'

print 'Login Started'

# password manager handler
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, loginurl, username, password)

# Basic Auth. handler
authhandler = urllib2.HTTPBasicAuthHandler(passman)

# Cookie handler
cookiejar = cookielib.LWPCookieJar()
cookiehandler = urllib2.HTTPCookieProcessor(cookiejar)

# add our handlers
urlopener = urllib2.build_opener(authhandler, cookiehandler)

# install urlopener so all urllib2 calls use our handlers
urllib2.install_opener(urlopener)

print 'Login Finished'

req = urllib2.Request('http://' + loginurl)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
Reply
#5
It wouldve worked if the site allowed HTTP Basic Authorisation calls, but looks like they dont. either that or you've not supplied some required GET and/or POST data in the Request (ie wid=6467)

The authorisation should have been established when you pass the site root url to the handlers. The Request should then be to a specific url after logon, not the logon page again.

Try using a browser addon such as TamperData which might reveal more info to you
Retired from Add-on dev
Reply
#6
Star 
BigBellyBilly Wrote:It wouldve worked if the site allowed HTTP Basic Authorisation calls, but looks like they dont. either that or you've not supplied some required GET and/or POST data in the Request (ie wid=6467)

The authorisation should have been established when you pass the site root url to the handlers. The Request should then be to a specific url after logon, not the logon page again.

Try using a browser addon such as TamperData which might reveal more info to you

So here is what I found so far. I used TamperData and did the Tamper on the url:

https://www.jumptv.com/en/signin?locatio...ignin_link

It gave me below data. Couple of things I noticed here were Status 302 ('Found', 'Object moved temporarily -- see URI list'), and Location=/en/signin/success?wid=8062

Hence I tried the login url: https://www.jumptv.com/en/signin?wid=8062 with HTTP Basic Authorisation calls with no success. Ofcourse I scraped the wid from the original page because that keeps changing...

Any other advise would be appreciated.

Status=Found - 302
Date=Thu, 02 Apr 2009 02:09:05 GMT
Server=Apache
X-Powered-By=PHP/5.1.2
P3P=CP="CAO IVD OUR BUS PUR"
Set-Cookie=PHPSESSID=ce8e3642ee2069b7c566a93db6f13ba1; expires=Thu, 26-Mar-2009 02:09:05 GMT
PHPSESSID=ce8e3642ee2069b7c566a93db6f13ba1; path=/; domain=.jumptv.com
PHPSESSID=6d67c2d6b4b23be05ef0350b1c37607c; expires=Thu, 26-Mar-2009 02:09:05 GMT
PHPSESSID=6d67c2d6b4b23be05ef0350b1c37607c; path=/; domain=.jumptv.com
PHPSESSID=6d67c2d6b4b23be05ef0350b1c37607c; expires=Thu, 26-Mar-2009 02:09:05 GMT
PHPSESSID=6d67c2d6b4b23be05ef0350b1c37607c; path=/; domain=.jumptv.com
Location=/en/signin/success?wid=8062
Vary=Accept-Encoding
Content-Encoding=gzip
Content-Length=20
Keep-Alive=timeout=15, max=100
Connection=Keep-Alive
Content-Type=text/html
Reply
#7
Star 
So here I just tried something different with mechanize and ClientCookie and that seems to work fine. Now my testcase works and I only need to convert that into a plugin...

Thanks for all your pointers Smile
Reply
#8
HTTP Basic Auth. may have been the wrong approach for your login site, but glad you got it working anyway.
Retired from Add-on dev
Reply
#9
Smile 
BigBellyBilly Wrote:HTTP Basic Auth. may have been the wrong approach for your login site, but glad you got it working anyway.

True but its good to know that HTTP Basic Auth also exists Smile
Reply
#10
Star 
A different problem now... I am trying to import mechanize library in XBMC, but I get the below error. While the same import command works fine inside Python shell in Windows. Something to do with _winreg... When I import _winreg in python shell it imports fine...

23:11:44 T:2088 M:536698880 NOTICE: -->Python Interpreter Initialized<--
23:11:44 T:2088 M:536698880 NOTICE:
23:11:44 T:2088 M:533811200 NOTICE: Traceback (most recent call last):
23:11:44 T:2088 M:533811200 NOTICE: File "C:\Program Files\XBMC\plugins\video\JumpTV\default.py", line 18, in ?
23:11:44 T:2088 M:533811200 NOTICE:
23:11:44 T:2088 M:533811200 NOTICE: from JumpTV import xbmcplugin_list as plugin
23:11:44 T:2088 M:533811200 NOTICE: File "C:\Program Files\XBMC\plugins\video\JumpTV\JumpTV\xbmcplugin_list.py", line 7, in ?
23:11:44 T:2088 M:533811200 NOTICE:
23:11:44 T:2088 M:533811200 NOTICE: from mechanize import Browser
23:11:44 T:2088 M:533811200 NOTICE: File "C:\Program Files\XBMC\plugins\video\JumpTV\mechanize\__init__.py", line 131, in ?
23:11:44 T:2088 M:533807104 NOTICE:
23:11:44 T:2088 M:533807104 NOTICE: from _msiecookiejar import MSIECookieJar
23:11:44 T:2088 M:533807104 NOTICE: File "C:\Program Files\XBMC\plugins\video\JumpTV\mechanize\_msiecookiejar.py", line 16, in ?
23:11:44 T:2088 M:533803008 NOTICE:
23:11:44 T:2088 M:533803008 NOTICE: import _winreg
23:11:44 T:2088 M:533803008 NOTICE: ImportError
23:11:44 T:2088 M:533803008 NOTICE: :
23:11:44 T:2088 M:533803008 NOTICE: No module named _winreg
23:11:44 T:2088 M:533803008 NOTICE:
23:11:44 T:2088 M:533803008 ERROR: Scriptresult: Error
23:11:46 T:604 M:537554944 ERROR: CGUIMediaWindow::GetDirectory(plugin://video/JumpTV/) failed
Reply
#11
is it simply that it can't find module _winreg in the current path ?
Retired from Add-on dev
Reply
#12
Star 
BigBellyBilly Wrote:is it simply that it can't find module _winreg in the current path ?

Nevermind I got the plugin good to go now Smile thanks for all your help...
Reply
#13
Did you get this working? Can you share the code for the plugin?
Reply

Logout Mark Read Team Forum Stats Members Help
[Question] Writing plugin that requires login info0