Adding cookie or header to picture links?

  Thread Rating:
  • 0 Vote(s) - 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 ( 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...
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.

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: 278
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
fh open(filename'r')

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

='name of your script'

'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: 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()

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()

# Browser options

# 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: Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# The site we will navigate into, handling it's session

# Select the first (index zero) form

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

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

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:
find quote