Posts: 463
Joined: Mar 2005
Reputation:
0
i need to send cookies to a website that i'm connecting to and i was told that i could use cachedhttp.py to accomplish this. i've tried searching and looking at the documentation but i still can't figure it out.
can anybody help me? all i want is to send some cookies (like 4) every time i connect with urllib. (or i could switch to urllib2 or whatever....)
Posts: 211
Joined: Mar 2005
Reputation:
0
Bernd
Senior Member
Posts: 211
i encourage every xbmc script author to use cachedhttp.py!!!
its an easy and ready to use lib which provides lots of functionality ready to use incl cookies, user agent and timeouts.
bernd
Posts: 463
Joined: Mar 2005
Reputation:
0
is there any documentation for this? (or really basic examples?)
i've taken a look at it but couldn't figure out how to use it...
Posts: 843
Joined: Mar 2008
Try this :
Code:
import urllib2
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
page = urllib2.urlopen(req);response=page.read();page.close()
cookie=page.info()['Set-Cookie']
# top is for obtaining the cookie via the info get.
req = urllib2.Request(url)#send the new url with the cookie.
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header('Cookie',cookie)
page = urllib2.urlopen(req)
response=page.read();page.close()
Posts: 63
Joined: Nov 2005
Reputation:
1
2008-12-12, 08:24
(This post was last modified: 2008-12-12, 09:13 by hattmall.)
Now Have it working I think sending the cookie, but I need to be able to Retrieve the URL while sending the cookie.
This way I can store the file, or play it.
Like the urllib.retrieve
which will work, but will not pass the cookies.
So I need the Urllib2 way to retrieve a file or whatever the method is.
Posts: 843
Joined: Mar 2008
2008-12-12, 14:49
(This post was last modified: 2008-12-12, 14:52 by Voinage.)
Have you read my plugin tutorial ?
The set-cookie only gets the true cookie sent by the site, if it isnt there it will not be able to find it and cause an error.
Copy the addlink def from one of my plugins then addlink(name you want to display, url of file, thumbnail)
when you press (A) xbox or click(windows/other) it will stream the file.
Posts: 63
Joined: Nov 2005
Reputation:
1
2008-12-13, 00:47
(This post was last modified: 2008-12-13, 02:06 by hattmall.)
** Resolved - Explaining In Next Post, with new questions.
I have read your Tutorial and the documentation in the wiki multiple times, wouldn't have gotten this far without it by any means, and search the forums as best I can.
Thank you to all who provide such information.
Ok, I think I understand, here is my issue. I need to send a cookie. That I already have on my computer. The site does not set a cookie. The cookie actually includes the username and an authorization code. I copy this information from my Windows computer to the script. Each time I access something on the server I have to send this cookie otherwise I can't get the file. (It seems)
This is the line that is allowing it to work.
req.add_header('Cookie','user=XXXXXXX-%59%44%73%45%46%65%4B%4C%76%53')
XXXX = My Username for Rapidshare.com
So far, I think I am only able to do this with urllib2, which does not support the retrieve method.
I am also not linking to direct video files, but to RAR Archives. So I believe I will need to download the first file completely before it can begin playing.
I am able to do this for files less than 64MB using.
page = urllib2.urlopen(req)
fout = open(destination, "wb") #destination is location to save file
fout.write(page.read())
fout.close()
But, if the file is larger than 64MB the XBOX runs out of Ram and locks up.
As of now I am making a script, because I plan to add more features. I haven't made it a plugin. If I was to have a plugin, and a direct link to video/audio files. Is there a way to pass the cookie before getting the file to play.
As best I can tell the addlink def uses urllib.urlretrieve which I do not think can send the cookie header?
Thank you Voinage for all your help, please let me know where I am right and wrong and what I can do.
I am looking for a way to download the file in increments of perhaps 20% write it, and then append another 20% etc.
Thanks
Posts: 843
Joined: Mar 2008
Code:
This spilts the file into 3 you can split it more if you want the sends the correct range requests to get each part of the file and download it.
def DAP(url):
name=url.split('/')
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
response = urllib2.urlopen(req)
size=int(response.info().get('Content-Length'));split=size/3
p1='bytes=0-'+str(split)
p2='bytes='+str((split+1))+'-'+str((split*2))
p3='bytes='+str((split*2+1))+'-'+str(size)
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header('Range',p1)
response1 = urllib2.urlopen(req)
buf1=response.read()
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header('Range',p2)
response2 = urllib2.urlopen(req)
buf2=response.read()
req = urllib2.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14')
req.add_header('Range',p3)
response3 = urllib2.urlopen(req)
buf3=response.read()
print time.asctime(),'Downloading: bytes= %s of %s' % (str(len(buf1)), str(size))
fileout=open(name[-1],'wb')
fileout.write(buf1)
if len (buf1)>=split-1:
buf1=buf1+buf2
if len(buf1)>=split*2-1:
buf1=buf1+buf3
if len(buf1)>=size:
response1.close()
response2.close()
response3.close()
fileout.flush()
fileout.close()