From 29c466ddf34ed0b1e9e85bc2247fd2727e4c41cb Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 25 Jan 2023 18:56:26 +0000 Subject: [PATCH] Added support for short nicovideo url and 'from' param. --- src/Apis/v1_0.php | 4 ++++ src/Lookup/NicoNicoLookup.php | 35 +++++++++++++++++++++++++---- src/Lookup/NicoNicoLookupResult.php | 11 +++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Apis/v1_0.php b/src/Apis/v1_0.php index 6bc15c7..c2764cd 100644 --- a/src/Apis/v1_0.php +++ b/src/Apis/v1_0.php @@ -176,8 +176,12 @@ final class v1_0 implements \Uiharu\IApi { if($result instanceof NicoNicoLookupResult) { $resp->nicovideo_video_id = $result->getNicoNicoVideoId(); + if($result->hasNicoNicoVideoStartTime()) + $resp->nicovideo_start_time = $result->getNicoNicoVideoStartTime(); + if(UIH_DEBUG) { $resp->dbg_nicovideo_thumb_info = $result->getNicoNicoThumbInfo()->ownerDocument->saveXML(); + $resp->dbg_nicovideo_query = $result->getNicoNicoUrlQuery(); } } diff --git a/src/Lookup/NicoNicoLookup.php b/src/Lookup/NicoNicoLookup.php index 24d8a7b..d0ddd77 100644 --- a/src/Lookup/NicoNicoLookup.php +++ b/src/Lookup/NicoNicoLookup.php @@ -6,18 +6,43 @@ use RuntimeException; use Uiharu\Url; final class NicoNicoLookup implements \Uiharu\ILookup { + private const SHORT_DOMAINS = [ + 'nico.ms', + 'www.nico.ms', + ]; + + private const LONG_DOMAINS = [ + 'www.nicovideo.jp', + 'nicovideo.jp', + ]; + + public static function isShortDomain(string $host): bool { + return in_array($host, self::SHORT_DOMAINS); + } + + public static function isLongDomain(string $host): bool { + return in_array($host, self::LONG_DOMAINS); + } + public function match(Url $url): bool { - if(!$url->isWeb() || ($url->getHost() !== 'www.nicovideo.jp' && $url->getHost() !== 'nicovideo.jp')) + if(!$url->isWeb()) return false; - if(str_starts_with($url->getPath(), '/watch/sm')) + if(self::isShortDomain($url->getHost())) + return true; + + if(self::isLongDomain($url->getHost()) && str_starts_with($url->getPath(), '/watch/sm')) return true; return false; } public function lookup(Url $url): NicoNicoLookupResult { - $videoId = explode('/', trim($url->getPath(), '/'))[1] ?? ''; + if(self::isShortDomain($url->getHost())) + $videoId = explode('/', trim($url->getPath(), '/'))[0] ?? ''; + else + $videoId = explode('/', trim($url->getPath(), '/'))[1] ?? ''; + if(empty($videoId)) throw new RuntimeException('Nico Nico Douga video id missing.'); @@ -31,7 +56,9 @@ final class NicoNicoLookup implements \Uiharu\ILookup { if(empty($thumbInfo)) throw new RuntimeException('Nico Nico Douga thumb info missing from API result????'); - return new NicoNicoLookupResult($url, $videoId, $thumbInfo); + parse_str($url->getQuery(), $urlQuery); + + return new NicoNicoLookupResult($url, $videoId, $thumbInfo, $urlQuery); } private static function lookupThumbInfo(string $videoId): DOMDocument { diff --git a/src/Lookup/NicoNicoLookupResult.php b/src/Lookup/NicoNicoLookupResult.php index 1c82311..90a010c 100644 --- a/src/Lookup/NicoNicoLookupResult.php +++ b/src/Lookup/NicoNicoLookupResult.php @@ -14,6 +14,7 @@ final class NicoNicoLookupResult implements \Uiharu\ILookupResult { private Url $url, private string $videoId, private DOMElement $thumbInfo, + private array $urlQuery, ) {} public function getUrl(): Url { @@ -29,6 +30,16 @@ final class NicoNicoLookupResult implements \Uiharu\ILookupResult { public function getNicoNicoThumbInfo(): DOMElement { return $this->thumbInfo; } + public function getNicoNicoUrlQuery(): array { + return $this->urlQuery; + } + + public function hasNicoNicoVideoStartTime(): bool { + return isset($this->urlQuery['from']); + } + public function getNicoNicoVideoStartTime(): string { + return $this->urlQuery['from'] ?? ''; + } public function hasMediaType(): bool { return false;