echoOutput = $echoOutput; $this->category = new Categorize(); $this->db = new DB(); $this->consoleTools = new ConsoleTools(); $this->colorCLI = new ColorCLI(); $this->site = (new Sites)->get(); $this->functions = new Functions(); } /** * Process RequestID's via Web or Local lookup. * * @param int $groupID The ID of the group. * @param int $limit How many requests to do. * @param bool $local Do a local or web lookup? * * @return int How many request ID's were found. */ public function lookupReqIDs($groupID, $limit, $local) { $this->groupID = $groupID; $this->limit = $limit; $this->local = $local; $this->reqIDsFound = 0; $this->getResults(); if ($this->results !== false && $this->results->rowCount() > 0) { $this->findReqIdMatches(); } return $this->reqIDsFound; } /** * Get all results from the releases table that have request ID's to be processed. */ protected function getResults() { // Look for records that potentially have requestID titles and have not been matched to a PreDB title $this->results = $this->db->queryDirect( sprintf( 'SELECT r.ID, r.name, r.searchname, g.name AS groupname, r.groupID FROM releases r LEFT JOIN groups g ON r.groupID = g.ID WHERE nzbstatus = 1 AND prehashID = 0 AND (isrequestid = 1 AND reqidstatus = %d OR (reqidstatus = %d AND adddate > NOW() - INTERVAL %d HOUR) ) %s %s LIMIT %d', ($this->local === true ? self::REQID_UPROC : self::REQID_NOLL), self::REQID_NONE, (isset($this->site->request_hours) ? (int)$this->site->request_hours : 1), (empty($this->groupID) ? '' : ('AND groupID = ' . $this->groupID)), ($this->local === true ? '' : 'ORDER BY postdate DESC'), $this->limit ) ); } /** * See if the release has a valid request ID, try to a PRE name locally or from the internet. */ protected function findReqIdMatches() { $this->newTitle = false; foreach ($this->results as $result) { $this->result = $result; $this->newTitle = false; // Try to get request id. if (preg_match('/\[\s*(\d+)\s*\]/', $this->result['name'], $requestID) || preg_match('/^REQ\s*(\d{4,6})/i', $this->result['name'], $requestID) || preg_match('/^(\d{4,6})-\d{1}\[/', $this->result['name'], $requestID) || preg_match('/(\d{4,6}) -/', $this->result['name'], $requestID) ) { $this->requestID = (int)$requestID[1]; } else { $this->requestID = self::REQID_ZERO; } if ($this->requestID === self::REQID_ZERO) { $this->db->exec( sprintf(' UPDATE releases SET reqidstatus = %d WHERE ID = %d', self::REQID_ZERO, $this->result['ID'] ) ); if ($this->local === false && $this->echoOutput) { echo '-'; } } else { if ($this->local === true) { $this->localCheck(); } else { $this->remoteCheck(); } if ($this->newTitle !== false) { if ($this->preDbID === false) { $this->insertIntoPreDB(); } $this->updateRelease(); } else { $this->db->exec( sprintf( 'UPDATE releases SET reqidstatus = %d WHERE ID = %d', self::REQID_NONE, $this->result['ID'] ) ); if ($this->local === false && $this->echoOutput) { echo '-'; } } } } } /** * Try to find a PRE name using the request ID in our local PRE database. */ protected function localCheck() { $localCheck = $this->db->queryOneRow( sprintf(" SELECT ID, title FROM prehash WHERE requestID = %d AND groupID = %d", $this->requestID, $this->result['groupID'] ) ); if ($localCheck !== false) { $this->newTitle = $localCheck['title']; $this->preDbID = $localCheck['ID']; $this->reqIDsFound++; } } /** * Try to find a PRE name on the internet using the found request ID. */ protected function remoteCheck() { // Do a web lookup. $xml = $this->functions->getUrl( str_ireplace( '[REQUEST_ID]', $this->requestID, str_ireplace( '[GROUP_NM]', urlencode($this->result['groupname']), $this->site->request_url ) ) ); if ($xml === false && preg_match('/alt\.binaries\.(etc|mom|\.hdtv.x264)/', $this->result['groupname'])) { $reqGname = 'alt.binaries.moovee'; if ($this->result['groupname'] === 'alt.binaries.etc') { $reqGname = 'alt.binaries.teevee'; } $xml = $this->functions->getUrl( str_ireplace( '[REQUEST_ID]', $this->requestID, str_ireplace( '[GROUP_NM]', urlencode($reqGname), $this->site->request_url ) ) ); } if ($xml !== false) { $xml = simplexml_load_string($xml); if ($xml !== false && isset($xml->request[0]['name']) && !empty($xml->request[0]['name']) && strtolower($xml->request[0]['name']) !== strtolower($this->result['searchname']) ) { $this->newTitle = $xml->request[0]['name']; $this->reqIDsFound++; } } } /** * If we found a PRE name, update the releases name and reset post processing. */ protected function updateRelease() { $determinedCategory = $this->category->determineCategory($this->result['groupID'], $this->newTitle); $this->db->exec( sprintf(' UPDATE releases SET rageid = -1, seriesfull = NULL, season = NULL, episode = NULL, tvtitle = NULL, tvairdate = NULL, imdbID = NULL, musicinfoID = NULL, consoleinfoID = NULL, bookinfoID = NULL, anidbID = NULL, reqidstatus = %d, isrenamed = 1, proc_files = 1, searchname = %s, categoryiID = %d, prehashID = %d WHERE ID = %d', self::REQID_FOUND, $this->db->escapeString($this->newTitle), $determinedCategory, $this->preDbID, $this->result['ID'] ) ); if ($this->echoOutput) { NameFixer::echoChangedReleaseName(array( 'new_name' => $this->newTitle, 'old_name' => $this->result['searchname'], 'new_category' => $this->functions->getNameByID($determinedCategory), 'old_category' => '', 'group' => $this->result['groupname'], 'release_id' => $this->result['ID'], 'method' => 'RequestID->updateRelease<' . ($this->local ? 'local' : 'web') . '>' ) ); } } /** * If we found a request ID on the internet, check if our PRE database has it, insert it if not. */ protected function insertIntoPreDB() { $dupeCheck = $this->db->queryOneRow( sprintf(' SELECT ID AS prehashID, requestID, groupID FROM prehash WHERE title = %s', $this->db->escapeString($this->newTitle) ) ); if ($dupeCheck === false) { $this->preDbID = $this->db->queryInsert( sprintf(" INSERT INTO prehash (title, source, requestID, groupID, predate) VALUES (%s, %s, %d, %d, NOW())", $this->db->escapeString($this->newTitle), $this->db->escapeString('requestWEB'), $this->requestID, $this->result['groupID'] ) ); } else { $this->preDbID = $dupeCheck['prehashID']; $this->db->exec( sprintf(' UPDATE prehash SET requestID = %d, groupID = %d WHERE ID = %d', $this->requestID, $this->result['groupID'], $this->preDbID ) ); } } }