Skip to end of metadata
Go to start of metadata

This script will remove completed torrents and send an email.


removeTorrents.php

<?php

//custom variables
$EMAIL_TO="XXX@yahoo.com";
$EMAIL_FROM="XXX@yahoo.com";

//default variables
$BASE_URL="http://192.168.1.50:3000";
$DOWNLOAD_FOLDER="/root/containers/cloud-torrent/downloads";
$MOVIES_FOLDER="/media/nas0/movies";
$TVSHOWS_FOLDER="/media/nas0/incoming";
$EMAIL_SUBJECT_DOWNLOADED="DOWNLOADED - ";
$EMAIL_SUBJECT_ERROR="FAILED TO REMOVE - ";

removeCompleted();

//done
exit(0);


//***************************************
// sendNotification
//***************************************
function sendNotification($torrent,$status){
    global $EMAIL_SUBJECT_DOWNLOADED, $EMAIL_SUBJECT_ERROR, $EMAIL_FROM, $EMAIL_TO;

    if($status == "downloaded"){
      $subject= "\"" . $EMAIL_SUBJECT_DOWNLOADED . $torrent  . "\"";
    }else if($status == "error"){
      $subject= "\"" . $EMAIL_SUBJECT_ERROR . $torrent . "\"";
    }

    $cmd= getCwd() . "/sendEmail.sh " . $subject  . " " . $EMAIL_FROM . " " . $EMAIL_TO . " " . $subject;
    shell_exec($cmd);

}

//***************************************
// removeCompleted
//***************************************
function removeCompleted(){
  global $BASE_URL, $DOWNLOAD_FOLDER, $MOVIES_FOLDER, $TVSHOWS_FOLDER, $EMAIL_TO, $EMAIL_FROM, $EMAIL_SUBJECT_DOWNLOADED;

  $resp=file_get_contents($BASE_URL . '/api/torrents' );
  //echo $resp . "\n";

  $json = json_decode($resp);
  foreach ($json as $key=>$val) {
    $name = $json->$key->{'Name'};
    $completed = $json->$key->{'Done'};

    if($completed){
      //echo "removing torrent: " . $name ."\n";

      $result = performTorrentAction($key,"stop",$name);
      if(!$result){
        sendNotification($name,"error");
        continue;
      }

      $result = performTorrentAction($key,"delete",$name);
      if(!$result){
        sendNotification($name,"error");
        continue;
      }

      // move to folder
      echo "Moving torrent ....\n";
      $source = $DOWNLOAD_FOLDER . "/" . $name;
      $destination = $MOVIES_FOLDER . "/" . $name;
      moveFolder($source,$destination);
      
      sendNotification($name,"downloaded");

      echo "Done\n";

    }//completed

  }//foreach
  echo "Run Complete\n";

}

//******************************************
// performTorrentAction
//******************************************
function performTorrentAction($hash,$action,$name){
  global $BASE_URL;

      $url = $BASE_URL . '/api/torrent';
      $data = $action . ":" . $hash;

      $options = array(
        'http' => array(
        'header'  => "Content-type: text/plain\r\n",
        'method'  => 'POST',
        'content' => $data
        )
      );

      $context  = stream_context_create($options);
      $result = @file_get_contents($url, false, $context);
      if ($result === FALSE) {
        echo "Failed to " . $action . " torrent  " . $name ."\n";
      }else{
        echo "Success performing " . $action . " for torrent  " . $name . "\n";
      }

      return $result;

}

//******************************************
// moveFolder
//******************************************
function moveFolder($source, $target){

    exec("mv ".escapeshellarg($source)." ".escapeshellarg($target));
}

?>  


sendEmail.sh

#!/bin/sh

SUBJECT=$1
TO=$2
FROM=$3
CONTENT=$4

echo "Subject: $SUBJECT"
echo "From: $FROM"
echo "To: $TO"
echo "CONTENT $CONTENT"

echo "Subject: $SUBJECT" > mail.txt
echo "From: $FROM" >> mail.txt
echo "To: $TO" >> mail.txt
echo "" >> mail.txt
echo $CONTENT >> mail.txt

cat mail.txt |sendmail -t
rm mail.txt