From 57d5aa45f7296fe99215c8bb11201a322204092d Mon Sep 17 00:00:00 2001 From: Dag Date: Sun, 4 Sep 2022 04:35:21 +0200 Subject: [PATCH] fix: php notice in eztvbridge (#2998) * fix: php notice in eztvbridge Fixes Undefined property: stdClass::$torrents * lint --- bridges/EZTVBridge.php | 87 +++++++++++++++++------------------------- lib/utils.php | 15 ++++++++ tests/UtilsTest.php | 9 +++++ 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/bridges/EZTVBridge.php b/bridges/EZTVBridge.php index cf969cb5..89aeba9f 100644 --- a/bridges/EZTVBridge.php +++ b/bridges/EZTVBridge.php @@ -1,20 +1,21 @@ [ - 'name' => 'Show IMDB IDs', + 'name' => 'IMDB ids', 'exampleValue' => '8740790,1733785', 'required' => true, - 'title' => 'One or more IMDB show IDs (can be found in the IMDB show URL)' + 'title' => 'One or more IMDB ids' ], 'no480' => [ 'name' => 'No 480p', @@ -44,53 +45,16 @@ on EZTV. Get IMDB IDs from IMDB.'; ] ]; - // Shamelessly lifted from https://stackoverflow.com/a/2510459 - protected function formatBytes($bytes, $precision = 2) - { - $units = ['B', 'KB', 'MB', 'GB', 'TB']; - - $bytes = max($bytes, 0); - $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); - $pow = min($pow, count($units) - 1); - $bytes /= pow(1024, $pow); - - return round($bytes, $precision) . ' ' . $units[$pow]; - } - - protected function getItemFromTorrent($torrent) - { - $item = []; - $item['uri'] = $torrent->episode_url; - $item['author'] = $torrent->imdb_id; - $item['timestamp'] = date('d F Y H:i:s', $torrent->date_released_unix); - $item['title'] = $torrent->title; - $item['enclosures'][] = $torrent->torrent_url; - - $thumbnailUri = 'https:' . $torrent->small_screenshot; - $torrentSize = $this->formatBytes($torrent->size_bytes); - - $item['content'] = $torrent->filename . '
File size: ' - . $torrentSize . '
magnet link
torrent link
'; - - return $item; - } - - private static function compareDate($torrent1, $torrent2) - { - return (strtotime($torrent1['timestamp']) < strtotime($torrent2['timestamp']) ? 1 : -1); - } - public function collectData() { - $showIds = explode(',', $this->getInput('ids')); - - foreach ($showIds as $showId) { - $eztvUri = $this->getURI() . 'api/get-torrents?imdb_id=' . $showId; - $content = getContents($eztvUri); - $torrents = json_decode($content)->torrents; - foreach ($torrents as $torrent) { + $ids = explode(',', trim($this->getInput('ids'))); + foreach ($ids as $id) { + $data = json_decode(getContents(sprintf('https://eztv.re/api/get-torrents?imdb_id=%s', $id))); + if (!isset($data->torrents)) { + // No results + continue; + } + foreach ($data->torrents as $torrent) { $title = $torrent->title; $regex480 = '/480p/'; $regex720 = '/720p/'; @@ -107,12 +71,31 @@ on EZTV. Get IMDB IDs from IMDB.'; ) { continue; } - $this->items[] = $this->getItemFromTorrent($torrent); } } + usort($this->items, function ($torrent1, $torrent2) { + return $torrent2['timestamp'] <=> $torrent1['timestamp']; + }); + } - // Sort all torrents in array by date - usort($this->items, ['EZTVBridge', 'compareDate']); + protected function getItemFromTorrent($torrent) + { + $item = []; + $item['uri'] = $torrent->episode_url; + $item['author'] = $torrent->imdb_id; + $item['timestamp'] = $torrent->date_released_unix; + $item['title'] = $torrent->title; + $item['enclosures'][] = $torrent->torrent_url; + + $thumbnailUri = 'https:' . $torrent->small_screenshot; + $torrentSize = format_bytes((int) $torrent->size_bytes); + + $item['content'] = $torrent->filename . '
File size: ' + . $torrentSize . '
magnet link
torrent link
'; + + return $item; } } diff --git a/lib/utils.php b/lib/utils.php index a411d2aa..de8ae8a8 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -136,3 +136,18 @@ function parse_mime_type($url) return 'application/octet-stream'; } + +/** + * https://stackoverflow.com/a/2510459 + */ +function format_bytes(int $bytes, $precision = 2) +{ + $units = ['B', 'KB', 'MB', 'GB', 'TB']; + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + $bytes /= pow(1024, $pow); + + return round($bytes, $precision) . ' ' . $units[$pow]; +} diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 3dd389c6..6e05ed57 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -17,6 +17,15 @@ final class UtilsTest extends TestCase $this->assertSame('fo[...]', truncate('foo', 2, '[...]')); } + public function testFormatBytes() + { + $this->assertSame('1 B', format_bytes(1)); + $this->assertSame('1 KB', format_bytes(1024)); + $this->assertSame('1 MB', format_bytes(1024 ** 2)); + $this->assertSame('1 GB', format_bytes(1024 ** 3)); + $this->assertSame('1 TB', format_bytes(1024 ** 4)); + } + public function testFileCache() { $sut = new \FileCache();