From 737e743709009cbd412e0fe921c9d3360c9c45a7 Mon Sep 17 00:00:00 2001 From: Rogiel Date: Sat, 1 Oct 2011 19:58:13 -0300 Subject: [PATCH] Adds the initial files This commit adds the initial pre-database files to the repository --- .buildpath | 5 + .project | 22 ++ .settings/org.eclipse.php.core.prefs | 3 + Adapter/Pre_Orlydb_Adapter.php | 75 +++++++ Adapter/Pre_ScnSrc_Adapter.php | 125 +++++++++++ Examples/Example_AlternativeAdapter.php | 27 +++ Examples/Example_SimpleSearch.php | 26 +++ Examples/Example_TVShowHelper.php | 35 +++ PreDatabase_Library.php | 284 ++++++++++++++++++++++++ 9 files changed, 602 insertions(+) create mode 100644 .buildpath create mode 100644 .project create mode 100644 .settings/org.eclipse.php.core.prefs create mode 100644 Adapter/Pre_Orlydb_Adapter.php create mode 100644 Adapter/Pre_ScnSrc_Adapter.php create mode 100644 Examples/Example_AlternativeAdapter.php create mode 100644 Examples/Example_SimpleSearch.php create mode 100644 Examples/Example_TVShowHelper.php create mode 100755 PreDatabase_Library.php diff --git a/.buildpath b/.buildpath new file mode 100644 index 0000000..8bcb4b5 --- /dev/null +++ b/.buildpath @@ -0,0 +1,5 @@ + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..b5c66bc --- /dev/null +++ b/.project @@ -0,0 +1,22 @@ + + + lib-predb + + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.dltk.core.scriptbuilder + + + + + + org.eclipse.php.core.PHPNature + + diff --git a/.settings/org.eclipse.php.core.prefs b/.settings/org.eclipse.php.core.prefs new file mode 100644 index 0000000..6f7cc23 --- /dev/null +++ b/.settings/org.eclipse.php.core.prefs @@ -0,0 +1,3 @@ +#Sat Oct 01 19:11:06 BRT 2011 +eclipse.preferences.version=1 +include_path=0;/lib-predb diff --git a/Adapter/Pre_Orlydb_Adapter.php b/Adapter/Pre_Orlydb_Adapter.php new file mode 100644 index 0000000..9d7ed86 --- /dev/null +++ b/Adapter/Pre_Orlydb_Adapter.php @@ -0,0 +1,75 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +// Require core library +require_once '../PreDatabase_Library.php'; + +/** +* Adapter implementation for http://orlydb.com/ +* @author Rogiel +* @since 1.0 +*/ +class Pre_Orlydb_Adapter implements Pre_Adapter { + public function latest() { + $html = file_get_contents("http://orlydb.com/1"); + return $this->parseList($html); + } + + public function search($release) { + $html = file_get_contents("http://orlydb.com/?q=" . urlencode($release)); + return $this->parseList($html); + } + + private function parseList($html) { + $dom = new DomDocument(); + $result = @$dom->loadHTML($html); //ignore parse errors + $xpath = new DomXPath($dom); + + $i = 0; + while (true) { + $i ++; + $node = $xpath->query("/html/body/div/div[2]/div[" . $i . "]/span"); + if ($node->length == 0) + break; + $pres[] = &$this->parseRelease($node); + } + return $pres; + } + + /** + * @param DOMNodeList $node + */ + private function parseRelease($node) { + $extra = $node->item(3)->nodeValue; + if (strlen($extra)) { + list ($size, $files) = explode("|", $extra); + $size = trim($size); + $files = trim($files); + } + $entry = new Pre_Release(); + $entry->release = $node->item(2)->nodeValue; + $entry->type = $node->item(1)->nodeValue; + $entry->date = strtotime($node->item(0)->nodeValue); + $entry->size = (intval($size)) * 1024 * 1024; //as bytes + $entry->files = intval($files); + $entry->nuke = $node->item(4)->nodeValue; + + return $entry; + } +} +?> \ No newline at end of file diff --git a/Adapter/Pre_ScnSrc_Adapter.php b/Adapter/Pre_ScnSrc_Adapter.php new file mode 100644 index 0000000..56baf06 --- /dev/null +++ b/Adapter/Pre_ScnSrc_Adapter.php @@ -0,0 +1,125 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +// Require core library +require_once '../PreDatabase_Library.php'; + +/** + * Adapter implementation for http://pre.scnsrc.net/ + * @author Rogiel + * @since 1.0 + */ +class Pre_ScnSrc_Adapter implements Pre_Adapter { + public function latest() { + $html = file_get_contents("http://pre.scnsrc.net/index.php"); + return $this->parseList($html); + } + + public function search($release) { + $html = $this->download( + "http://pre.corrupt-net.org/search.php?search=" . $release); + return $this->parseList($html); + } + + private function parseList($html) { + + $dom = new DomDocument(); + $result = @$dom->loadHTML($html); //ignore parse errors + $xpath = new DomXPath($dom); + + $i = 0; + while (true) { + $i ++; + ///html/body/table/tbody/tr[3] + $node = $xpath->query( + "/html/body/table/tr[" . $i . "]/td"); + if ($node->length == 0) { + //die("empty!"); + break; + } + $pres[] = &$this->parseRelease($node); + } + return $pres; + } + + /** + * @param DOMNodeList $node + */ + private function parseRelease($node) { + // $extra = $node->item(3)->nodeValue; + // if (strlen($extra)) { + // list ($size, $files) = explode("|", $extra); + // $size = trim($size); + // $files = trim($files); + // } + $entry = &new Pre_Release(); + $entry->release = $node->item(1)->nodeValue; + $entry->type = $node->item(0)->nodeValue; + if ($node->length == 3) { + $entry->date = strtotime(substr($node->item(2)->nodeValue, 4)) + + $vbulletin->options['release_time_offset']; + } else { + $entry->size = (intval($node->item(2)->nodeValue)) * 1024 * + 1024; //as bytes + $entry->files = intval($files); + } + + return $entry; + } + + private function download($url) { + $options = array( + CURLOPT_RETURNTRANSFER => true, // return web page + CURLOPT_HEADER => false, // don't return headers + CURLOPT_FOLLOWLOCATION => true, // follow redirects + CURLOPT_HEADER => array( + 'Host: pre.corrupt-net.org', + //'Accept: text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8', + 'User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.10 (maverick) Firefox/3.6.15', + 'Accept-Language: en-us,en;q=0.5', + 'Accept-Encoding: gzip,deflate', + 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7', + 'Keep-Alive: 115', + 'Connection: keep-alive', + 'Cache-Control: max-age=0'), + //CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.10 (maverick) Firefox/3.6.15", // who am i + //CURLOPT_AUTOREFERER => true, // set referer on redirect + CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect + CURLOPT_TIMEOUT => 120, // timeout on response + CURLOPT_MAXREDIRS => 10); // stop after 10 redirects + + + $ch = curl_init($url); + curl_setopt_array($ch, $options); + $content = curl_exec($ch); + $err = curl_errno($ch); + $errmsg = curl_error($ch); + $header = curl_getinfo($ch); + curl_close($ch); + + $header['errno'] = $err; + $header['errmsg'] = $errmsg; + $header['content'] = $content; + + print_r($header); + die(); + return $content; + } +} + +?> \ No newline at end of file diff --git a/Examples/Example_AlternativeAdapter.php b/Examples/Example_AlternativeAdapter.php new file mode 100644 index 0000000..826c6d0 --- /dev/null +++ b/Examples/Example_AlternativeAdapter.php @@ -0,0 +1,27 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +// This example does a simple search for an pre release + +require_once '../PreDatabase_Library.php'; +require_once '../Adapter/Pre_ScnSrc_Adapter.php'; + +$db = new Pre_DB(new Pre_ScnSrc_Adapter()); +print_r($db->search("Microsoft Windows")); + +?> \ No newline at end of file diff --git a/Examples/Example_SimpleSearch.php b/Examples/Example_SimpleSearch.php new file mode 100644 index 0000000..ed05934 --- /dev/null +++ b/Examples/Example_SimpleSearch.php @@ -0,0 +1,26 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +// This example does a simple search for an pre release + +require_once '../PreDatabase_Library.php'; + +$db = new Pre_DB(); +print_r($db->search("Microsoft Windows")); + +?> \ No newline at end of file diff --git a/Examples/Example_TVShowHelper.php b/Examples/Example_TVShowHelper.php new file mode 100644 index 0000000..51205f6 --- /dev/null +++ b/Examples/Example_TVShowHelper.php @@ -0,0 +1,35 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +// This example used the Pre_TVShow_SearchHelper to search an given +// episode of an TVShow. It can be used to filter data returned. + +require_once '../PreDatabase_Library.php'; + +$db = new Pre_DB(); + +// Show: True Blood +// Season: 4 +// Episode 1 +print_r($db->search(new Pre_TVShow_SearchHelper( + "true blood", // the tv show name + 4, // the season + 1 // the episode +))); + +?> \ No newline at end of file diff --git a/PreDatabase_Library.php b/PreDatabase_Library.php new file mode 100755 index 0000000..1859e93 --- /dev/null +++ b/PreDatabase_Library.php @@ -0,0 +1,284 @@ +. +* +* predb is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* predb is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with predb. If not, see . +*/ + +/** + * The pre database communication class. All operations are performed trough this class. + * @author Rogiel + * @since 1.0 + */ +class Pre_DB { + /** + * The Pre_Adapter implementation + * @var Pre_Adapter + */ + private $adapter; + + /** + * Creates a new instance + * @param Pre_Adapter $adapter the adapter to use. If none is provided an default is used. + */ + public function __construct($adapter = null) { + if ($adapter == null) { + require_once 'Adapter/Pre_Orlydb_Adapter.php'; + $adapter = new Pre_Orlydb_Adapter(); + } + $this->adapter = $adapter; + } + + /** + * Searches the pre database for certain releases + * @param string, Pre_SearchHelper $query the query + */ + public function search($query) { + if ($query instanceof Pre_SearchHelper) { + $query = $query->getSearchQuery(); + } + return $this->adapter->search($query); + } + + /** + * Searches the pre database for an certain release + * @param string $release the release name + * @return Pre_Release + */ + public function get($release) { + $releases = $this->adapter->search($release); + return predb_find_suitable_release($release, $releases); + } + + /** + * Retrieve the latest pre entries from the database + */ + public function latest() { + return $this->adapter->latest(); + } +} + +/** + * Pre entry + * @author @author Rogiel + * @since 1.0 + */ +class Pre_Release { + /** + * @var string + */ + var $release; + /** + * @var string + */ + var $type; + /** + * @var int + */ + var $date; + /** + * @var int + */ + var $size; + /** + * @var int + */ + var $files; + /** + * @var string + */ + var $nuke; + + /** + * @return the $release + */ + public function getRelease() { + return $this->release; + } + + /** + * @return the $date + */ + public function getDate() { + return $this->date; + } + + /** + * @return the $size + */ + public function getSize() { + return $this->size; + } + + /** + * @return the $files + */ + public function getFiles() { + return $this->files; + } + + /** + * @return the $nuke + */ + public function getNuke() { + return $this->nuke; + } + + /** + * @param string $release + */ + public function setRelease($release) { + $this->release = $release; + } + + /** + * @param int $date + */ + public function setDate($date) { + $this->date = $date; + } + + /** + * @param int $size + */ + public function setSize($size) { + $this->size = $size; + } + + /** + * @param int $files + */ + public function setFiles($files) { + $this->files = $files; + } + + /** + * @param string $nuke + */ + public function setNuke($nuke) { + $this->nuke = $nuke; + } +} + +/** + * This is an search helper. It assists the creation of queries that will be used to find an given release. + * @author Rogiel + * @since 1.0 + */ +interface Pre_SearchHelper { + function getSearchQuery(); +} + +/** + * This Pre_SearchHelper asists searching for TVShow releases + * @author Rogiel + * @since 1.0 + */ +class Pre_TVShow_SearchHelper implements Pre_SearchHelper { + private $name; + private $season = null; + private $episode = null; + + public function __construct($name, $season = null, $episode = null) { + $this->name = $name; + $this->season = $season; + $this->episode = $episode; + } + + public function getSearchQuery() { + $query = $this->name; + if ($this->season != null) { + $query .= " S" . predb_padding_zero($this->season); + if ($this->episode != null) + $query .= "E" . predb_padding_zero($this->episode); + } + return str_replace(" ", ".", $query); + } + + /** + * @return the $name + */ + public function getName() { + return $this->name; + } + + /** + * @param string $name + * @return Pre_TVShow_SearchHelper this instance + */ + public function setName($name) { + $this->name = $name; + return $this; + } + + /** + * @return the $season + */ + public function getSeason() { + return $this->season; + } + + /** + * @param int $season + * @return Pre_TVShow_SearchHelper this instance + */ + public function setSeason($season) { + $this->season = $season; + return $this; + } + + /** + * @return the $episode + */ + public function getEpisode() { + return $this->episode; + } + + /** + * @param int $episode + * @return Pre_TVShow_SearchHelper this instance + */ + public function setEpisode($episode) { + $this->episode = $episode; + return $this; + } +} + +/** + * The pre adapter interface + * @author Rogiel + * @since 1.0 + */ +interface Pre_Adapter { + /** + * Implements the underlying search method + * @param string $release + */ + function search($release); + + /** + * Implements the underlying latest method + */ + function latest(); +} + +/** + * @param int $number + * @return the number with padded zeros + */ +function predb_padding_zero($number) { + if ($number < 10) + return "0" . $number; + return $number; +}