Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
titleshowTorrents.php
<?php

$pass=urlencode("xxx_BASE64ENCODED_PASSWORD_xxx");
$baseURL="http://localhost:8080";

$EMAIL_TO="jxxx@yahoo.com";
$EMAIL_FROM="jxxx@yahoo.com";
$EMAIL_SUBJECT="TORRENT DOWNLOADED [NAS] - "$pass=urlencode("xxx_BASE64ENCODED_PASSWORD_xxx");

//login
$resp$vars = file_get_contents($baseURL . '/cgi-bin/authLogin.cgi?user=admin&pwdpass=' . $pass);
//echo $resp;

$sid=getSid($resp);
//echo "sid='" . $sid . "'\n";
 
$resp=file_get_contents($ch = curl_init( $baseURL . '/downloadstation/V4/Misc/Login?sid=' . $sid);
//echo $resp . "\n";


$resp=file_get_contents($baseURL . '/downloadstation/V4/Task/Query?start=0&limit=100&status=all&from=0&to=100&sid=' . $sid);
echo $resp . "\n";


$json = json_decode($resp);
$data = $json->{'data'};
//var_dump($data);

//echo "CWD: " . getCwd() . "\n";

foreach($data as $torrent){
  $name= $torrent->{'source_name'};
  echo "torrent: " . $name . "\n";

}

//***************************************
// endsWith
//***************************************
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }

    return (substr($haystack, -$length) === $needle);
}

//***************************************
// getSid
//***************************************
function getSid($xml){

    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $xml, $values, $tags);
    xml_parser_free($parser);
    //print_r($values);
 
    // loop through the structures
    foreach ($values as $val) {
      $tag=$val['tag'];
      $value=$val['value'];
      if($tag=="authSid"){
        return $value;
      } 
      //echo "tag=" . $tag . ", value=" . $value . "\n";
    }

}


?>

curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec( $ch );
//echo $resp;

$sid = json_decode($resp)->{'sid'};
//echo "sid='" . $sid . "'\n";

$resp=file_get_contents($baseURL . '/downloadstation/V4/Misc/Login?sid=' . $sid);
//echo $resp . "\n";

$resp=file_get_contents($baseURL . '/downloadstation/V4/Task/Query?start=0&limit=100&status=all&from=0&to=100&sid=' . $sid);
echo $resp . "\n";

$json = json_decode($resp);
$data = $json->{'data'};
//var_dump($data);

//echo "CWD: " . getCwd() . "\n";

foreach($data as $torrent){
  $name= $torrent->{'source_name'};
  echo "torrent: " . $name . "\n";

}

?>


removeAllTorrents.php

Code Block
languagephp
titleremoveAllTorrents.php
<?php

$EMAIL_TO="j****@yahoo.com";
$EMAIL_FROM="j****@yahoo.com";
$EMAIL_SUBJECT="TORRENT DOWNLOADED [NAS] - ";

$pass=urlencode("xxx_BASE64ENCODED_PASSWORD_xxx");
$baseURL="http://localhost:8080";

//login
$resp = file_get_contents($baseURL . "/cgi-bin/authLogin.cgi?user=admin&pwd=" . $pass );
$sid=getSid($resp);
 
$resp=file_get_contents($baseURL . '/downloadstation/V4/Misc/Login?sid=' . $sid);

$resp=file_get_contents($baseURL . '/downloadstation/V4/Task/Query?start=0&limit=100&status=all&from=0&to=100&sid=' . $sid);


$json = json_decode($resp);
$data = $json->{'data'};

foreach($data as $torrent){
  $name= $torrent->{'source_name'};
  $hash=$torrent->{'hash'};

  if(endsWith($name,".torrent")){
  //  echo "torrent file - just delete\n";
  }else{
    //echo "file - notify completion\n";
    $subject= "\"" . $EMAIL_SUBJECT . $name . "\""; 
    $cmd= getCwd() . "/sendEmail.sh " . $subject  . " " . $EMAIL_FROM . " " . $EMAIL_TO . " " . $subject;
    //echo "cmd=" . $cmd . "\n";

    //shell_exec($cmd); 
  }

  //remove torrent 
  $resp=file_get_contents($baseURL . '/downloadstation/V4/Task/Remove?clean=0&hash=' . $hash . '&sid=' . $sid);

  //echo $resp ."\n";
  //break;

}

//***************************************
// endsWith
//***************************************
function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }

    return (substr($haystack, -$length) === $needle);
}

//***************************************
// getSid
//***************************************
function getSid($xml){

    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $xml, $values, $tags);
    xml_parser_free($parser);
    //print_r($values);
 
    // loop through the structures
    foreach ($values as $val) {
      $tag=$val['tag'];
      $value=$val['value'];
      if($tag=="authSid"){
        return $value;
      } 
      //echo "tag=" . $tag . ", value=" . $value . "\n";
    }

}
?>

...