1
0
mirror of https://github.com/Rogiel/predb synced 2025-12-05 23:22:48 +00:00

Implements namespaces and spl autoloading

This commit is contained in:
2014-11-23 15:18:58 -02:00
parent f7b2691c4b
commit 68cf6fc064
16 changed files with 851 additions and 566 deletions

53
src/Adapter/Adapter.php Normal file
View File

@@ -0,0 +1,53 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB\Adapter;
/**
* The pre adapter interface
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
interface Adapter {
/**
* Implements the underlying search method
*
* @param string $release
*/
function search($release);
/**
* Implements the underlying latest method
*/
function latest();
}

101
src/Adapter/OrlyDB.php Normal file
View File

@@ -0,0 +1,101 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB\Adapter;
use PreDB\Release;
use DOMDocument;
use DOMXPath;
use DateTime;
use DateTimeZone;
/**
* Adapter implementation for http://orlydb.com/
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*
*/
class OrlyDB implements 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();
@$dom->loadHTML($html); // ignore parse errors
$xpath = new DOMXPath($dom);
$i = 0;
$pres = array();
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 Release();
$entry->release = $node->item(2)->nodeValue;
$entry->type = $node->item(1)->nodeValue;
$entry->date = new DateTime($node->item(0)->nodeValue, new DateTimeZone('UTC'));
$entry->size = (intval($size)) * 1024 * 1024; // as bytes
$entry->files = intval($files);
$entry->nuke = $node->item(4)->nodeValue;
return $entry;
}
}
?>

89
src/PreDB.php Normal file
View File

@@ -0,0 +1,89 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB;
use PreDB\Adapter\Adapter;
use PreDB\Search\Helper as SearchHelper;
class PreDB {
/**
* The Pre_Adapter implementation
*
* @var Adapter
*/
private $adapter;
/**
* Creates a new instance
*
* @param Adapter $adapter
* the adapter to use. If none is provided an default is used.
*/
public function __construct($adapter = null) {
if ($adapter == null) {
$adapter = new \PreDB\Adapter\OrlyDB();
}
$this->adapter = $adapter;
}
/**
* Searches the pre database for certain releases
*
* @param
* string, SearchHelper $query the query
*/
public function search($query) {
if ($query instanceof 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();
}
}

204
src/Release.php Normal file
View File

@@ -0,0 +1,204 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB;
use DateTime;
/**
* Pre entry
*
* @author @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
class Release {
/**
*
* @var string
*/
var $release;
/**
*
* @var string
*/
var $type;
/**
*
* @var DateTime
*/
var $date;
/**
*
* @var int
*/
var $size;
/**
*
* @var int
*/
var $files;
/**
*
* @var string
*/
var $nuke;
/**
* Creates a new release instance.
*
* If $release is a string, it is set as the release name
* If $release is an arraym the following keys are set to the release object model: release, type, date, size, files and nuke.
*
* @param string $release
* the release argument
*/
public function __construct($release = NULL) {
if (is_string($release)) {
$this->release = $release;
} else if (is_array($release)) {
$this->release = (isset($release['release']) ? $release['release'] : NULL);
$this->type = (isset($release['type']) ? $release['type'] : NULL);
$this->date = (isset($release['date']) ? $release['date'] : NULL);
$this->size = (isset($release['size']) ? $release['size'] : NULL);
$this->files = (isset($release['files']) ? $release['files'] : NULL);
$this->nuke = (isset($release['nuke']) ? $release['nuke'] : NULL);
}
}
/**
*
* @return the string
*/
public function getRelease() {
return $this->release;
}
/**
*
* @param string $release
*/
public function setRelease($release) {
$this->release = $release;
}
/**
*
* @return the string
*/
public function getType() {
return $this->type;
}
/**
*
* @param string $type
*/
public function setType($type) {
$this->type = $type;
}
/**
*
* @return the DateTime
*/
public function getDate() {
return $this->date;
}
/**
*
* @param DateTime $date
*/
public function setDate(DateTime $date) {
$this->date = $date;
}
/**
*
* @return the int
*/
public function getSize() {
return $this->size;
}
/**
*
* @param int $size
*/
public function setSize($size) {
$this->size = $size;
}
/**
*
* @return the int
*/
public function getFiles() {
return $this->files;
}
/**
*
* @param int $files
*/
public function setFiles($files) {
$this->files = $files;
}
/**
*
* @return the string
*/
public function getNuke() {
return $this->nuke;
}
/**
*
* @return bool true if the released is nuked
*/
public function isNuked() {
return $this->nuke != NULL;
}
/**
*
* @param string $nuke
*/
public function setNuke($nuke) {
$this->nuke = $nuke;
}
}

49
src/Search/Helper.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB\Search;
/**
* This is an search helper.
* It assists the creation of queries that will be used to find an given release.
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
interface Helper {
/**
* Returns a search query that will filter and or specialize the results
*
* @return string
*/
function getSearchQuery();
}

125
src/Search/TVShow.php Normal file
View File

@@ -0,0 +1,125 @@
<?php
/*
* Copyright (c) 2014, Rogiel Sulzbach <rogiel@rogiel.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Rogiel Sulzbach.
* 4. Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ROGIEL SULZBACH ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ROGIEL SULZBACH BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace PreDB\Search;
/**
* This Helper asists searching for TVShow releases
*
* @author <a href="http://www.rogiel.com/">Rogiel</a>
* @since 1.0
*/
class TVShow implements Helper {
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" . self::padZero($this->season);
if ($this->episode != null)
$query .= "E" . self::padZero($this->episode);
}
return str_replace(" ", ".", $query);
}
/**
*
* @return the $name
*/
public function getName() {
return $this->name;
}
/**
*
* @param string $name
* @return TVShow this instance
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
*
* @return the $season
*/
public function getSeason() {
return $this->season;
}
/**
*
* @param int $season
* @return TVShow this instance
*/
public function setSeason($season) {
$this->season = $season;
return $this;
}
/**
*
* @return the $episode
*/
public function getEpisode() {
return $this->episode;
}
/**
*
* @param int $episode
* @return TVShow this instance
*/
public function setEpisode($episode) {
$this->episode = $episode;
return $this;
}
/**
*
* @param int $number
* @return the number with padded zeros
*/
private static function padZero($number) {
if ($number < 10)
return "0" . $number;
return $number;
}
}