Aria2c - how to download a lot of files and ouput as 0.pdf, 1.pdf, 2.pdf ...?

3k Views Asked by At

I'm having a little problem here. I'm using the famous Aria2C file downloader and I have a list of links that I need to download. I know I can use:

aria2c -o myOutputFile.exe http://www.fooBar.com/fooBarVirus.exe

to download a single file and name it as myOutputFile.exe. The problem is that I must download some files and name it as 0.pdf, 1.pdf, 2.pdf, 3.pdf... because I will assemble all this pdf's later.

I'll try explain better. If I give the following links to aria2c:

http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
...
http://www.fooBar.com/myLastPdf.pdf

I must download these files and name it as a sequence of integers FOLLOWING THE LINKS INSERTION ORDER. With that I mean the filenames is related to the links order, but I don't need to download them in order.

Let's suppose I give Aria2C the previous links. The following output would be a valid output for my problem:

 Time: 0
 Link: http://www.fooBar.com/crazyPdf.pdf
 Filename: 2.pdf

 Time: 1
 Link: http://www.fooBar.com/myPDF.pdf
 Filename: 0.pdf

 Time: 2
 Link: http://www.fooBar.com/kindness.pdf
 Filename: 1.pdf

 ...

 Time: N
 Link: http://www.fooBar.com/myLastPdf.pdf
 Filename: N.pdf

I know this is a confusing question, so if you have any doubt please make a comment and I'll try answer as soon as possible. Thanks!

1

There are 1 best solutions below

0
On

Ok, let's start with a pdfs.txt file holding the URLs for the PDFs:

http://www.fooBar.com/myPDF.pdf
http://www.fooBar.com/kindness.pdf
http://www.fooBar.com/crazyPdf.pdf
http://www.fooBar.com/myLastPdf.pdf

I throw in a little helper script to turn the URLs from the pdfs.txt file into the file format Aria2c accepts. The script uses the numerical index of an URL in the array as the new filename for the PDF.

<?php
$urls = __DIR__ . '/pdfs.txt';
$downloads = file($urls);

$targetFolder = __DIR__; // define your taget folder
$ariaDownloadList = '';
foreach($downloads as $idx => $url)
{
    $pdfName = $idx . '.pdf'; // 0.pdf ... N.pdf

    // add new download entry
    $ariaDownloadList .= $url;
    $ariaDownloadList .= '  dir=' . pathinfo($targetFolder, PATHINFO_DIRNAME) . PHP_EOL;
    $ariaDownloadList .= '  out=' . $pdfName . PHP_EOL;
}

file_put_contents('ariaDownloadsFile.txt', $ariaDownloadList);

This outputs a file ariaDownloadsFile.txt with the following content:

http://www.fooBar.com/myPDF.pdf
  dir=C:\pdfs
  out=0.pdf
http://www.fooBar.com/kindness.pdf
  dir=C:\pdfs
  out=1.pdf
http://www.fooBar.com/crazyPdf.pdf
  dir=C:\pdfs
  out=2.pdf
http://www.fooBar.com/myLastPdf.pdf  
  dir=C:\pdfs
  out=3.pdf

Then simply run the following command:

aria2c -i ariaDownloadsFile.txt