Updated the NZB import and export scripts.

This commit is contained in:
DariusIII
2015-06-04 22:50:38 +02:00
parent e3943ca906
commit 441332fc07
7 changed files with 136 additions and 80 deletions
+37
View File
@@ -0,0 +1,37 @@
<?php
require_once dirname(__FILE__) . '/../../www/config.php';
$n = PHP_EOL;
// Print usage.
if (count($argv) !== 6) {
exit(
'This will export NZB files(to .nzb or .nzb.gz) into sub folders (using group name) of the specified folder.' . $n . $n .
'Usage: ' . $n .
$_SERVER['_'] . ' ' . __FILE__ . ' arg1 arg2 arg3 arg4 arg5' . $n . $n .
'arg1 : Path to folder where NZB files are to be stored. | a folder path' . $n .
'arg2 : The start date in this format: 01/01/2008 or false | date/false' . $n .
'arg3 : The end date in this format: 01/01/2008 or false | date/false' . $n .
'arg4 : Group ID for the group or false | number/false' . $n .
'arg5 : Gzip the NZB files (recommended, faster/takes less space) | true/false' . $n . $n .
'Examples: ' . $n .
$_SERVER['_'] . ' ' . $argv[0] . ' ' . NN_ROOT . 'exportFolder' . DS . ' 01/01/2012 01/01/2014 false true' . $n .
$_SERVER['_'] . ' ' . $argv[0] . ' ' . NN_ROOT . 'exportFolder' . DS . ' false 01/01/2014 12 false' . $n
);
}
$NE = new NZBExport();
$NE->beginExport(
array(
// Path.
$argv[1],
// Start time.
(strtolower($argv[2]) === 'false' ? '' : $argv[2]),
// End time.
(strtolower($argv[3]) === 'false' ? '' : $argv[3]),
// Group ID.
(strtolower($argv[4]) === 'false' ? 0 : (int)$argv[4]),
// Gzip.
(strtolower($argv[5]) === 'true' ? true : false)
)
);
@@ -1,12 +1,12 @@
<?php
require_once(dirname(__FILE__) . "/../../bin/config.php");
require_once dirname(__FILE__) . '/../../www/config.php';
$n = PHP_EOL;
// Print usage.
if (count($argv) !== 6) {
exit(
'This will import NZB files(.nzb or .nzb.gz), into your newznab site from a folder recursively(it will go down into sub-folders).'. $n .
'This will import NZB files(.nzb or .nzb.gz), into your newznab site from a folder recursively(it will go down into sub-folders).' . $n .
'Please use arg5, something sensible like 100k, if you have millions of NZB files the initial scan will be VERY slow otherwise.' . $n . $n .
'Usage: ' . $n .
$_SERVER['_'] . ' ' . __FILE__ . ' arg1 arg2 arg3 arg4 arg5' . $n . $n .
@@ -23,13 +23,13 @@ if (count($argv) !== 6) {
if (!is_dir($argv[1])) {
exit('Error: arg1 must be a path (you might not have read access to this path)' . $n);
}
if (!in_array($argv[2], ['true', 'false'])) {
if (!in_array($argv[2], array('true', 'false'))) {
exit('Error: arg2 must be true or false' . $n);
}
if (!in_array($argv[3], ['true', 'false'])) {
if (!in_array($argv[3], array('true', 'false'))) {
exit('Error: arg3 must be true or false' . $n);
}
if (!in_array($argv[4], ['true', 'false'])) {
if (!in_array($argv[4], array('true', 'false'))) {
exit('Error: arg4 must be true or false' . $n);
}
if (!is_numeric($argv[5])) {
@@ -54,7 +54,7 @@ $files = new \RegexIterator(
);
$i = 1;
$nzbFiles = [];
$nzbFiles = array();
foreach ($files as $file) {
$nzbFiles[] = $file[0];
if ($i++ >= $argv[5]) {
@@ -72,9 +72,9 @@ if ($i > 1) {
$useNzbName = ($argv[4] == 'true') ? true : false;
// Create a new instance of NZBImport and send it the file locations.
$NZBImport = new \NZBImport();
$NZBImport = new NZBImport();
$NZBImport->beginImport($nzbFiles, $useNzbName, $deleteNZB, $deleteFailedNZB);
} else {
echo 'Nothing found to import!' . $n;
}
}
+3 -3
View File
@@ -97,10 +97,10 @@ foreach ($filestoprocess as $nzbFile) {
$relguid = md5(uniqid());
$name = $releases->cleanReleaseName(str_replace(".nzb", "", basename($nzbFile)));
$catId = $cat->determineCategory($groupName, $name);
$relid = $releases->insertRelease($name, $nzbInfo->filecount, $groupID, $relguid, $catId, "", date("Y-m-d H:i:s", $nzbInfo->postedlast), $nzbInfo->poster, "", $page->site);
$relid = $releases->insertRelease($name, $nzbInfo->filecount, $groupID, $relguid, $catId, "", date("Y-m-d H:i:s", $nzbInfo->postedlast), $nzbInfo->poster, "", $page->settings);
$db->queryExec(sprintf("update releases set totalpart = %d, size = %s, completion = %d, GID=%s where id = %d", $nzbInfo->filecount, $nzbInfo->filesize, $nzbInfo->completion, $db->escapeString($nzbInfo->gid), $relid));
$nzbfilename = $nzb->getNZBPath($relguid, $page->site->nzbpath, true);
$nzbfilename = $nzb->getNZBPath($relguid, $page->settings->getSetting('nzbpath'), true);
$fp = gzopen($nzbfilename, "w");
if ($fp) {
gzwrite($fp, $nzbInfo->toNzb());
@@ -154,7 +154,7 @@ foreach ($filestoprocess as $nzbFile) {
$numbins++;
if (count($postFile['segments']) > 0) {
$sql = "INSERT INTO parts (binaryID, messageID, number, partnumber, size) values ";
$sql = "INSERT INTO parts (binaryid, messageid, number, partnumber, size) values ";
foreach ($postFile['segments'] as $fileSegmentNum => $fileSegment) {
$sql .= sprintf("(%d, %s, 0, %d, %d),", $binaryId, $db->escapeString($fileSegment), $fileSegmentNum, $postFile['segmentbytes'][$fileSegmentNum]);
$numparts++;
+5 -6
View File
@@ -51,7 +51,7 @@ class NZBExport
*
* @access public
*/
public function __construct(array $options = array())
public function __construct(array $options = [])
{
$defaults = [
'Browser' => false, // Started from browser?
@@ -65,8 +65,8 @@ class NZBExport
$this->browser = $options['Browser'];
$this->echoCLI = (!$this->browser && NN_ECHOCLI && $options['Echo']);
$this->pdo = ($options['Settings'] instanceof Settings ? $options['Setting'] : new Settings());
$this->releases = ($options['Releases'] instanceof \Releases ? $options['Releases'] : new \Releases(['Settings' => $this->pdo]));
$this->nzb = ($options['NZB'] instanceof \NZB ? $options['NZB'] : new \NZB($this->pdo));
$this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['Settings' => $this->pdo]));
$this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB($this->pdo));
}
/**
@@ -140,9 +140,8 @@ class NZBExport
// Loop over groups to take less RAM.
foreach ($groups as $group) {
$currentExport = 0;
$cat = '';
// Get all the releases based on the parameters.
$releases = $this->releases->getForExport($fromDate, $toDate, $group['id'], $cat);
$releases = $this->releases->getForExport($fromDate, $toDate, $group['id']);
$totalFound = count($releases);
if ($totalFound === 0) {
if ($this->echoCLI) {
@@ -182,7 +181,7 @@ class NZBExport
}
continue;
}
// If not, decompress it and create a file to store it in.
// If not, decompress it and create a file to store it in.
} else {
$nzbContents = Utility::unzipGzipFile($nzbFile);
if (!$nzbContents) {
+33 -34
View File
@@ -28,7 +28,7 @@ class NZBImport
protected $releaseCleaner;
/**
* @var bool|stdClass
* @var bool|\stdClass
* @access protected
*/
protected $site;
@@ -93,24 +93,24 @@ class NZBImport
public function __construct(array $options = [])
{
$defaults = [
'Browser' => false, // Was this started from the browser?
'Echo' => true, // Echo to CLI?
'Binaries' => null,
'Categorize' => null,
'NZB' => null,
'ReleaseCleaning' => null,
'Releases' => null,
'Settings' => null,
'Browser' => false, // Was this started from the browser?
'Echo' => true, // Echo to CLI?
'Binaries' => null,
'Categorize' => null,
'NZB' => null,
'ReleaseCleaning' => null,
'Releases' => null,
'Settings' => null,
];
$options += $defaults;
$this->echoCLI = (!$this->browser && NN_ECHOCLI && $options['Echo']);
$this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings());
$this->binaries = ($options['Binaries'] instanceof \Binaries ? $options['Binaries'] : new \Binaries(['Settings' => $this->pdo, 'Echo' => $this->echoCLI]));
$this->category = ($options['Categorize'] instanceof \Categorize ? $options['Categorize'] : new \Categorize(['Settings' => $this->pdo]));
$this->nzb = ($options['NZB'] instanceof \NZB ? $options['NZB'] : new \NZB($this->pdo));
$this->releaseCleaner = ($options['ReleaseCleaning'] instanceof \ReleaseCleaning ? $options['ReleaseCleaning'] : new \ReleaseCleaning($this->pdo));
$this->releases = ($options['Releases'] instanceof \Releases ? $options['Releases'] : new \Releases(['settings' => $this->pdo]));
$this->binaries = ($options['Binaries'] instanceof Binaries ? $options['Binaries'] : new Binaries(['Settings' => $this->pdo, 'Echo' => $this->echoCLI]));
$this->category = ($options['Categorize'] instanceof Categorize ? $options['Categorize'] : new Categorize(['Settings' => $this->pdo]));
$this->nzb = ($options['NZB'] instanceof NZB ? $options['NZB'] : new NZB($this->pdo));
$this->releaseCleaner = ($options['ReleaseCleaning'] instanceof ReleaseCleaning ? $options['ReleaseCleaning'] : new ReleaseCleaning($this->pdo));
$this->releases = ($options['Releases'] instanceof Releases ? $options['Releases'] : new Releases(['settings' => $this->pdo]));
$this->crossPostt = ($this->pdo->getSetting('crossposttime') != '') ? $this->pdo->getSetting('crossposttime') : 2;
$this->browser = $options['Browser'];
@@ -182,11 +182,11 @@ class NZBImport
if ($inserted) {
// Try to copy the NZB to the NZB folder.
$path = $this->nzb->getNZBPath($this->relGuid, '', true);
$path = $this->nzb->getNZBPath($this->relGuid, 0, true);
// Try to compress the NZB file in the NZB folder.
$fp = gzopen ($path, 'w5');
gzwrite ($fp, $nzbString);
$fp = gzopen($path, 'w5');
gzwrite($fp, $nzbString);
gzclose($fp);
if (!is_file($path)) {
@@ -264,25 +264,25 @@ class NZBImport
$groupID = -1;
// Get the nzb info.
if ($firstName === false ) {
$firstName =(string) $file->attributes()->subject;
if ($firstName === false) {
$firstName = (string)$file->attributes()->subject;
}
if ($posterName === false) {
$posterName = (string) $file->attributes()->poster;
$posterName = (string)$file->attributes()->poster;
}
if ($postDate === false) {
$postDate = date("Y-m-d H:i:s", (string) $file->attributes()->date);
$postDate = date("Y-m-d H:i:s", (string)$file->attributes()->date);
}
// Make a fake message array to use to check the blacklist.
$msg = ["Subject" => (string) $file->attributes()->subject, "From" => (string) $file->attributes()->poster, "Message-ID" => ""];
$msg = ["Subject" => (string)$file->attributes()->subject, "From" => (string)$file->attributes()->poster, "Message-ID" => ""];
// Get the group names, groupid, check if it's blacklisted.
// Get the group names, group_id, check if it's blacklisted.
$groupArr = [];
foreach ($file->groups->group as $group) {
$group = (string) $group;
$group = (string)$group;
// If groupid is -1 try to get a groupid.
// If group_id is -1 try to get a group_id.
if ($groupID === -1) {
if (array_key_exists($group, $this->allGroups)) {
$groupID = $this->allGroups[$group];
@@ -330,7 +330,7 @@ class NZBImport
'useFName' => $useNzbName,
'postDate' => (empty($postDate) ? date("Y-m-d H:i:s") : $postDate),
'from' => (empty($posterName) ? '' : $posterName),
'groupid' => $groupID,
'group_id' => $groupID,
'groupName' => $groupName,
'totalFiles' => $totalFiles,
'totalSize' => $totalSize
@@ -393,19 +393,16 @@ class NZBImport
'name' => $escapedSubject,
'searchname' => $escapedSearchName,
'totalpart' => $nzbDetails['totalFiles'],
'groupid' => $nzbDetails['groupid'],
'groupid' => $nzbDetails['group_id'],
'guid' => $this->pdo->escapeString($this->relGuid),
'regexid' => NULL,
'postdate' => $this->pdo->escapeString($nzbDetails['postDate']),
'fromname' => $escapedFromName,
'reqid' => NULL,
'passwordstatus' => ($this->pdo->getSetting('checkpasswordedrar') > 0 ? -1 : 0),
'size' => $this->pdo->escapeString($nzbDetails['totalSize']),
'categoryid' => $this->category->determineCategory($nzbDetails['groupid'], $cleanName),
'categoryid' => $this->category->determineCategory($nzbDetails['group_id'], $cleanName),
'isrenamed' => $renamed,
'reqidstatus' => 0,
'prehashid' => 0,
'nzbstatus' => \Enzebe::NZB_ADDED
'nzbstatus' => Enzebe::NZB_ADDED
]
);
} else {
@@ -419,10 +416,11 @@ class NZBImport
}
return true;
}
/**
* Get all groups in the DB.
* @return bool
*
* @return bool
* @access protected
*/
protected function getAllGroups()
@@ -442,7 +440,8 @@ class NZBImport
/**
* Echo message to browser or CLI.
* @param $message
*
* @param string $message
*
* @access protected
*/
+49 -28
View File
@@ -441,38 +441,59 @@ class Releases
}
/**
* Get a range of releases. Used in nzb export
* Get list of releases available for export.
*
* @param string $postFrom (optional) Date in this format : 01/01/2014
* @param string $postTo (optional) Date in this format : 01/01/2014
* @param string $groupID (optional) Group ID.
*
* @return array
*/
public function getForExport($postfrom, $postto, $group, $cat)
public function getForExport($postFrom = '', $postTo = '', $groupID = '')
{
return $this->pdo->query(
sprintf(
"SELECT searchname, guid, groups.name AS gname, CONCAT(cp.title,'_',category.title) AS catName
FROM releases r
INNER JOIN category ON r.categoryid = category.id
INNER JOIN groups ON r.groupid = groups.id
INNER JOIN category cp ON cp.id = category.parentid
WHERE r.nzbstatus = %d
%s %s %s",
Enzebe::NZB_ADDED,
$this->exportDateString($postFrom),
$this->exportDateString($postTo, false),
(($groupID != '' && $groupID != '-1') ? sprintf(' AND group_id = %d ', $groupID) : '')
)
);
}
if ($postfrom != "") {
$dateparts = explode("/", $postfrom);
if (count($dateparts) == 3)
$postfrom = sprintf(" and postdate > %s ", $this->pdo->escapeString($dateparts[2] . "-" . $dateparts[1] . "-" . $dateparts[0] . " 00:00:00"));
else
$postfrom = "";
/**
* Create a date query string for exporting.
*
* @param string $date
* @param bool $from
*
* @return string
*/
private function exportDateString($date, $from = true)
{
if ($date != '') {
$dateParts = explode('/', $date);
if (count($dateParts) === 3) {
$date = sprintf(
' AND postdate %s %s ',
($from ? '>' : '<'),
$this->pdo->escapeString(
$dateParts[2] . '-' . $dateParts[1] . '-' . $dateParts[0] .
($from ? ' 00:00:00' : ' 23:59:59')
)
);
} else {
$date = '';
}
}
if ($postto != "") {
$dateparts = explode("/", $postto);
if (count($dateparts) == 3)
$postto = sprintf(" and postdate < %s ", $this->pdo->escapeString($dateparts[2] . "-" . $dateparts[1] . "-" . $dateparts[0] . " 23:59:59"));
else
$postto = "";
}
if ($group != "" && $group != "-1")
$group = sprintf(" and groupid = %d ", $group);
else
$group = "";
if ($cat != "" && $cat != "-1")
$cat = sprintf(" and categoryid = %d ", $cat);
else
$cat = "";
return $this->pdo->queryDirect(sprintf("SELECT searchname, guid, CONCAT(cp.title,'_',category.title) AS catName FROM releases INNER JOIN category ON releases.categoryid = category.id LEFT OUTER JOIN category cp ON cp.id = category.parentid WHERE 1 = 1 %s %s %s %s", $postfrom, $postto, $group, $cat));
return $date;
}
/**
+1 -1
View File
@@ -21,7 +21,7 @@ class ForkingImportNZB extends Forking
$options += $defaults;
parent::__construct($options);
$this->importPath = (PHP_BINARY . ' ' . NN_TMUX . 'lib' . DS . 'testing' . DS . 'nzb-import.php ');
$this->importPath = (PHP_BINARY . ' ' . NN_MISC . 'testing' . DS . 'nzb-import.php ');
$this->pdo = $options['settings'];
}