Added support for short nicovideo url and 'from' param.

This commit is contained in:
flash 2023-01-25 18:56:26 +00:00
parent 2df60bd7ae
commit 29c466ddf3
3 changed files with 46 additions and 4 deletions

View file

@ -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();
}
}

View file

@ -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 {

View file

@ -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;