[Release] Service to pass cookies/HTTP headers to XBMC video player

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
Unbehagen Offline
Skilled Python Coder
Posts: 342
Joined: Jul 2007
Reputation: 3
Location: Bremen, Germany
Post: #1
Update: There might be a better solution - I just found this AFTER writing this script tonight. Just for correctness, please check this thing first: http://forum.xbmc.org/showthread.php?tid=77500

Hi,
I did not find a way to pass cookies and other HTTP headers to XBMC for video playback, so I wrote a small python script that can be installed as an XBMC service. It works a bit like a proxy server (remember VeohProxy?) and allows you to specify:
- the URL to download and
- the HTTP headers to send with each request XBMC makes to that URL
It will just pass through the request headers that XBMC sends to the local webserver and add the ones you specified. It will then send back all the data it receives from the real server.

The script might be useful for streaming directly from one-click-hosting services like Rapidshare, FileServe and others. These services typically use an HTML form or some kind of HTTP API to validate the user's credentials and sets a cookie with a session key - without this cookie, there is no way to download the file.

It provides a webserver on 127.0.0.1:64653. To make a request, first encode the URL you want to retrieve in Base64 and do the same with the HTTP headers to be sent. The HTTP headers need to be in the format a web server would expect:
Code:
Key: Value\r\n
Key2: Value2\r\n
The URL is formed like this:
Code:
http://127.0.0.1:64653/withheaders/[urlBase64]/[HeadersBase64]
An example:
if the video URL is
Code:
http://my.test.com/testfile_needs_cookies.avi
and the header needs to contain the following information:
Code:
Cookie: AuthorizedUser0815
, the resulting URL is:
Code:
http://127.0.0.1:64653/withheaders/aHR0cDovL215LnRlc3QuY29tL3Rlc3RmaWxlX25lZWRzX2Nvb2tpZXMuYXZp/Q29va2llOiBBdXRob3JpemVkVXNlcjA4MTUNCg==
You can now pass this URL to XBMC as the video URL.

You can check in your addon if the service is running by querying
Code:
http://127.0.0.1:64653/version
, which should return:
Code:
Proxy: Running
Version: 0.1

Here's the script:
http://pastebin.com/b1mJzTbL

I wrote this only for testing purposes, but figured it might help other people to create better streaming plugins.
(This post was last modified: 2011-02-26 09:23 by Unbehagen.)
find quote
hippojay Offline
Fan
Posts: 301
Joined: Mar 2008
Reputation: 13
Location: Sheffield, UK
Post: #2
Cheers - I was actually researching gateway proxy servers in python for use in my plugin - so this has come along at the right time!

For mine I only need to send a couple of headers for authetication, and this script looks perfect. I'll give it a bash and let you know the outcome..
find quote
Unbehagen Offline
Skilled Python Coder
Posts: 342
Joined: Jul 2007
Reputation: 3
Location: Bremen, Germany
Post: #3
hippojay Wrote:Cheers - I was actually researching gateway proxy servers in python for use in my plugin - so this has come along at the right time!

For mine I only need to send a couple of headers for authetication, and this script looks perfect. I'll give it a bash and let you know the outcome..

Hey, please see this post first: http://forum.xbmc.org/showthread.php?tid=77500 - the functionality of manipulating headers seems to be implemented natively in XBMC. Just use the pipe character after the url and set the headers using key=value touples.
find quote
hippojay Offline
Fan
Posts: 301
Joined: Mar 2008
Reputation: 13
Location: Sheffield, UK
Post: #4
Wow - thanks for finding that. I shall be giving that test later then...
find quote
anarchintosh Offline
Fan
Posts: 550
Joined: Jul 2010
Reputation: 4
Post: #5
neat thanks for this...
find quote
hippojay Offline
Fan
Posts: 301
Joined: Mar 2008
Reputation: 13
Location: Sheffield, UK
Post: #6
Just to confirm that the | and headers has fixed my authtication issue when playing back media and getting thumbnails.

That was a great find, I spent ages going over the various XBMC documents/functions to see if adding headers was possible - for some reason I didn't think to search in thie forum...
find quote
spiff Offline
Grumpy Bastard Developer
Posts: 12,180
Joined: Nov 2003
Reputation: 82
Post: #7
note that you can achieve this without any proxy as well.

http://some/url|Cookie=<urlencodedcookie> - set a cookie
http://some/url|Something=<urlencodedvalue> - set a value in the request header

and of course the combination
http://some/url|Cookie=<urlencodedcookie>&Something=<urlencodedvalue> - set a cookie

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.
find quote
hippojay Offline
Fan
Posts: 301
Joined: Mar 2008
Reputation: 13
Location: Sheffield, UK
Post: #8
Unbehagen - I was just wondering - could this proxy be altered so that it concatenates file requests into a single stream?

I want to take a playlist that serves out .TS file segments: get the proxy to retrieve the data in the correct order and serve out a continous stream of data. I guess all I need to do is add some extra logic in for parsing the playlist and then a routine to take the extra data - somewhere in lines 106 -112?
find quote
Unbehagen Offline
Skilled Python Coder
Posts: 342
Joined: Jul 2007
Reputation: 3
Location: Bremen, Germany
Post: #9
That should be doable. You have to think about something for the Content-Length HTTP header though and think about something for range requests. Without one, XBMC is usually not able to skip parts. You can have a look at the slightly chaotic VeohProxy - it does something like that for Veoh/Ninjavideo file parts. Sorry for the messy code in that project.
find quote
Unbehagen Offline
Skilled Python Coder
Posts: 342
Joined: Jul 2007
Reputation: 3
Location: Bremen, Germany
Post: #10
@spiff - I know. I found that function AFTER I wrote that thing and updated the top post with that info and a link to a thread discussing the pipe character in XBMC URLs.
find quote
Post Reply