Perl script to add files to download queue

Questions regarding modifying the torrent engine or download station may go here.
Forum rules
Please note the disclaimer before modifying your Synology Product.

Perl script to add files to download queue

Postby brett » Mon Sep 10, 2007 9:45 pm

I find using the download redirector to be a bit of a PITA, and was looking for a way to have direct click and queue for download integration with my browser, so I wrote this Perl script.

For my own use on a Windows XP machine I've packaged the script into an executable using the PAR packaging module and then configure Firefox to run .torrent files with it.

Probably this will be useful to others as well.

I've only tested it on the DS101g+ Firmware Version 2.0.3 - 0460, but assuming the web menus are the same, it should work on any other Synology device. (Or can be made to work.)

Code: Select all
#!/usr/bin/perl

use strict;
use warnings;
use WWW::Mechanize;
use Getopt::Long;
use Data::Dumper;
use Pod::Usage;

my $url = undef;
my $login = undef;
my $password = undef;
my $location = undef;
my $is_torrent = undef;
my ($help, $man) = undef;

my $result = GetOptions(
           'url|u=s'       => \$url,   
           'login|l=s'       => \$login,   
           'password|p=s'    => \$password,   
           'file|f=s'       => \$location,
                     'is_torrent|t'   => \$is_torrent,
           'help|h'      => \$help,
           'man|m'      => \$man,
       );

if ($help || $man) {
   pod2usage(-verbose => 1);
}

check_opt();

my $mech = WWW::Mechanize->new();

$mech->get($url);
$mech->get($url.$mech->{'links'}->[0]->[0]);

my $form = $mech->form_name('form');

$form->{'inputs'}->[0]->{'current'} = 1;
$form->{'inputs'}->[1]->{'value'} = $login;
$form->{'inputs'}->[2]->{'value'} = $password;

$mech->submit($form);

$mech->{'content'} =~ /download_management.cgi\?id=([^\"]+)\"/;
my $id = $1;

unless ($id) {
   die('Failed to parse session id');
}

$mech->get($url."download/downloadtaskadd.cgi?id=$id");

$form = $mech->form_name('uploadForm');

if ($is_torrent) {

   $mech->set_visible( [
            'radio' => 'torrent',
             ] );

   $form->{'inputs'}->[3]->file($location);
   
} else {
   
   $mech->set_visible( [
            'radio' => 'url',
             ] );

   $form->{'inputs'}->[2]->value($location);
}

$mech->submit($form);

if ($mech->{'content'} =~ /The settings have been completed successfully/) {
   print "'$login' successfully queued file '$location'\n";
} else {
   if ($mech->{'content'} =~ /Data can not be applied/) {
      if ($mech->{'content'} =~ /The URL should start with http/) {
         print "Adding download '$location' to queue for user '$login' failed\n";
         die("--is_torrent flag must be specified for queueing torrent files");
      } else {
         print "Adding download '$location' to queue for user '$login' failed\n";
         print Dumper $mech;
         die();
      }
   } else {   
         
      print "Adding download '$location' to queue for user '$login' failed\n";
      print Dumper $mech;
      die();
   }
}

sub check_opt {
   unless ($login) {die "Must provide login name with --login flag";}
   unless ($password) {die "Must provide password with --password flag";}
   unless ($url) {die "Must provide URL of diskstation with --url flag";}
   unless ($location) {die "Must provide URL or path to torrent file with --file flag";}

   unless ($url =~ /\/$/) {
      $url .= "/";
   }   
}

__END__

=head1 NAME

diskstation_download.pl - Add files to diskstatin download service queue

=head1 SYNOPSIS

   diskstation_download.pl --login login --password password --url http://diskstation:5000 --file /path/to/some_torrent_file.torrent --is_torrent 1

=head1 DESCRIPTION

This script allows adding download tasks to the diskstation download service queue without using the download redirector by automatic submission through the diskstation's web interface.

It supports queueing of both FTP/HTTP downloads and torrents.

Tested to work with Synology DS101g+
Firmware Version 2.0.3 - 0460

=head1 OPTIONS

=over 4

=item --login STRING

User login

=item --password STRING

User password

=item --url STRING

Diskstation web interface URL (eg: http://diskstation:5000)

=item --file STRING

URL/path to file to download

=item --is_torrent INT

Flag to specify whether file to queue is a torrent file (0 = no, 1 = yes)

=back

=head1 AUTHORS

Brett Whitty E<lt>spambw@gmail.comE<gt>

=cut
[/code]
brett
I'm New!
I'm New!
 
Posts: 5
Joined: Tue Oct 31, 2006 10:07 pm

Can you help me

Postby ingensin » Fri Oct 05, 2007 9:05 pm

Hello I'm trying to create a script that runs all the file in a directory
Code: Select all
#! /opt/bin/perl

$torrentDir = "/volume1/public/torrent";   // Where the torrent is located
$destDir = "/volume1/Download";         // where the files are going to end up


@actualFiles = </volume1/public/torrent/*.torrent>;


foreach $f (@actualFiles)
{


   // can You help my write a script that can but torrent files in the torrent engine?

   // I think of a siple command line here, where this text is. :)

   // I'm going to run this script in crontab...every hour or something.





    unlink($f);

}
ingensin
Beginner
Beginner
 
Posts: 25
Joined: Sat Jan 27, 2007 3:48 pm

Re: Perl script to add files to download queue

Postby BjoernWeber » Sun Sep 28, 2008 7:31 pm

Hi!

I've done a litte command line tool for Windows to add downloads to the queue (for use with FlashGot, etc.):

http://www.synology.com/enu/forum/viewtopic.php?f=38&t=10661&p=44579#p44579

Björn
BjoernWeber
Beginner
Beginner
 
Posts: 20
Joined: Sun Sep 28, 2008 2:49 pm

Re: Perl script to add files to download queue

Postby idaho » Sat Apr 11, 2009 7:26 pm

I tryed the tool today on my new DS-109, it does not seem to work properly (see the output trace below) :

Use of uninitialized value in concatenation (.) or string at Get.pl line 37.
There is no form named "form" at Get.pl line 39
Can't call method "make_request" on an undefined value at /usr/share/perl5/WWW/Mechanize.pm line 1641.
idaho
I'm New!
I'm New!
 
Posts: 3
Joined: Sat Apr 11, 2009 6:02 pm


Return to Torrent Engines/Download Station Mods

Who is online

Users browsing this forum: No registered users and 7 guests