3

As the server is using gzip encription I am getting an error torrent while downloading.

<?

$path_parts = pathinfo("http://torcache.com/torrent/56A250DC4CD64F6C304631897F1108D413FE76C7.torrent");
$name= $path_parts['basename'];
$d="torrent/".$name;
if(!copy($f,$d))
{
 echo "not copied";
}
else
{
 echo "copied";
}

?>

Then i used this then also the result is invalid torrent

<?php

/* Tutorial by AwesomePHP.com -> www.AwesomePHP.com */
/* Function: download remote file */
/* Parameters: $url -> to download | $dir -> where to store file |
    $file_name -> store file as this name - if null, use default*/

/* $path_parts = pathinfo("http://torcache.com/torrent/56A250DC4CD64F6C304631897F1108D413FE76C7.torrent");
$name= $path_parts['basename'];
$d="torrent/".$name; */

$f="http://torcache.com/torrent/56A250DC4CD64F6C304631897F1108D413FE76C7.torrent";

downloadRemoteFile($f,"torrent/",$file_name = NULL);

function downloadRemoteFile($url,$dir,$file_name = NULL){
    if($file_name == NULL){ $file_name = basename($url);}
    $url_stuff = parse_url($url);
    $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

    $fp = fsockopen($url_stuff['host'], $port);
    if(!$fp){ return false;}

    $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
    $query .= 'Host: ' . $url_stuff['host'];
    $query .= "\n\n";

    fwrite($fp, $query);

    while ($tmp = fread($fp, 8192))   {
        $buffer .= $tmp;
    }

    preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
    $file = substr($buffer, - $parts[1]);
    $file_binary=($file);
    if($file_name == NULL){
        $temp = explode(".",$url);
        $file_name = $temp[count($temp)-1];
    }
    $file_open = fopen($dir . "/" . $file_name,'w');

    if(!$file_open){ return false;}
    fwrite($file_open,$file_binary);
    fclose($file_open);
    return true;
} 
?> 

python

import urllib2, httplib
httplib.HTTPConnection.debuglevel = 1
request = urllib2.Request('http://torcache.com/torrent/4F78CA71DD8C308F18426F845AFBFF4481633B11.torrent')
request.add_header('Accept-encoding', 'gzip')
opener = urllib2.build_opener()
f = opener.open(request)
compresseddata = f.read()

import StringIO
compressedstream = StringIO.StringIO(compresseddata)
import gzip
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
print data
filename = "633B11.torrent"
FILE = open(filename,"w")
FILE.write(data)

Then i used python wiht gzip compression still i am getting invalid torrent file can anyboy help me to solve the gzip problem in php to download a torrent from a torrent cache server with gzip encoding

1
  • Gzip is not an encryption algorithm, just a compression algorithm.
    – deceze
    Jul 15, 2010 at 3:58

2 Answers 2

3

I encountered the same problem, solution is ya just decode the torrent before saving it.simplezz

function gzdecode($d){
$f=ord(substr($d,3,1));
$h=10;$e=0;
if($f&4){
    $e=unpack('v',substr($d,10,2));
    $e=$e[1];$h+=2+$e;
}
if($f&8){
    $h=strpos($d,chr(0),$h)+1;
}
if($f&16){
    $h=strpos($d,chr(0),$h)+1;
}
if($f&2){
    $h+=2;
}
$u = gzinflate(substr($d,$h));
if($u===FALSE){
    $u=$d;
}
return $u;}


$torrent = file_get_contents('http://URL_PATH_TO_TORRENT.torrent',FILE_BINARY);


$torrent = gzdecode($torrent);
file_put_contents('./torrentname.torrent',$torrent);
1
  • I have tried your code but i am getting ( ! ) Fatal error: Cannot redeclare gzdecode() in on line 22 which is return $u;}
    – user1642018
    Oct 18, 2013 at 20:45
1

Took me a lot of time to understand why i can't just download the torrent file using python. The user agent has to be Mozilla :)

import requests

url = r"https://torcache.net/torrent/6175754C9A5502BA085F8D64F31C8158AB0BE1C5.torrent?title=[kat.cr]paper.towns.2015.1080p.brrip.x264.yify"

s = requests.Session()

s.headers.update({"User-Agent": "Mozilla/4.0"})

answer = s.get(url)

torrent_data = answer.content

print torrent_data[:100]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.