Adding cookie or header to picture links?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
vidmard Offline
Junior Member
Posts: 3
Joined: Jan 2011
Reputation: 0
Wink  Adding cookie or header to picture links?
Post: #1
I'm fairly new to both XBMC and Python and I have started working on Zenfolio (http://www.zenfolio.com) add-on. I have basic funcionality in place and I can browse galleries and view images

The problem I'm facing now is related to security. I can only browse public photos. Zenfolio has really nice JSON API which lets me login and browse through hierarchy and so on. But when I need to download original sized photo, request should have a custom http header or cookie. Here is a quote from Zenfolio docs...
[INDENT]
If access to a photo is restricted, the application needs to provide the authentication token or the keyring along with the GET request in a manner similar to the Zenfolio API calls. To specify the authentication token, the application should include an X-Zenfolio-Token header or a zf_token cookie. To specify the keyring, the application should include an X-Zenfolio-Keyring header or a zf_keyring cookie. See the Authentication and Using the Keyring chapters for more information.
http://www.zenfolio.com/zf/help/api/guide/download
[/INDENT]

Currently I'm accessing photos by adding a link to ListItem and marking it as isFolder = False. I don't see a way to add cookie/header. Should I download image and cache it and providing link to local image? I would rather not do that, if possible.

Any examples, links or suggestions for a solution are welcome!
find quote
anarchintosh Offline
Senior Member
Posts: 282
Joined: Jul 2010
Reputation: 4
Post: #2
here are some the methods you need, might be a little messy:

PHP Code:
import cookielib,sys,os,urllib,urllib2

def openfile
(filename):
     
fh open(filename'r')
     
contents=fh.read()
     
fh.close()
     return 
contents

def xbmcpath
(path,filename):
     
translatedpath os.path.join(xbmc.translatePathpath ), ''+filename+'')
     return 
translatedpath

scriptname
='name of your script'
scriptdatapath='special://profile/addon_data/'+scriptname



mycookie 
xbmcpath(scriptdatapath,'cookies.lwp')
print 
'Cookie path: '+mycookie 


then this is what you use when you want to load a page with a cookie.
PHP Code:
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')
#if you need to add a referrer user        req.add_header('Referer', 'http://page-that-you-want-to-refer')
cj cookielib.LWPCookieJar()
cooky=cj.load(mycookie)
print 
cooky
opener 
urllib2.build_opener(urllib2.HTTPCookieProcessor(cooky))
response opener.open(req

i've forgotten exactly how to save a cookie from a url with urllib, but heres the code i use to log into megaupload and save the cookie. it uses the mechanize module....
PHP Code:
# Browser
               
br mechanize.Browser()

               
# Cookie Jar
               
cj cookielib.LWPCookieJar()
               
br.set_cookiejar(cj)

               
# Browser options
               
br.set_handle_equiv(True)
               
br.set_handle_gzip(True)
               
br.set_handle_redirect(True)
               
br.set_handle_referer(True)
               
br.set_handle_robots(False)

               
# Follows refresh 0 but not hangs on refresh > 0
               
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

               
# User-Agent
               
br.addheaders = [('User-agent''Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

               
# The site we will navigate into, handling it's session
               
br.open('http://www.megaupload.com/?c=login')

               
# Select the first (index zero) form
               
br.select_form('loginfrm')

               
#User credentials
               
br.form['username'] = megauser
               br
.form['password'] = megapass
               br
.submit()

               
#check if login worked
               
loginerror="Username and password do not match" in br.response().read()
               
cj.save(megacookie

it's all mostly a google away, this page might help
find quote
vidmard Offline
Junior Member
Posts: 3
Joined: Jan 2011
Reputation: 0
Post: #3
Tnx, anarchintosh. But if I read your code snippets correctly, this solution is helpful when pulling stuff locally for pasing or storing. What I was asking if I couple somehow "inject" cookie or customer http header when XBMC is downloading JPG image when user clicks on listitem/folder/item created my my addon.

In meanwhile, support from Zenfolio did great job and told me how to URL encode authentication, so my problem is solved without cookies/header.
find quote
Unbehagen Offline
Skilled Python Coder
Posts: 347
Joined: Jul 2007
Reputation: 4
Location: Bremen, Germany
Post: #4
This thing might help you out: http://forum.xbmc.org/showthread.php?tid=95369
find quote