Files
nexusphp/nexus/PTGen/PTGen.php

208 lines
6.9 KiB
PHP
Raw Normal View History

2021-01-15 01:12:44 +08:00
<?php
/**
* get media info base on PT-Gen
*
* @since 1.6
* @see https://github.com/Rhilip/pt-gen-cfworker
*/
namespace Nexus\PTGen;
use GuzzleHttp\Client;
class PTGen
{
private $apiPoint;
const SITE_DOUBAN = 'douban';
const SITE_IMDB = 'imdb';
const SITE_BANGUMI = 'bangumi';
private static $validSites = [
self::SITE_IMDB => [
'url_pattern' => '/(?:https?:\/\/)?(?:www\.)?imdb\.com\/title\/(tt\d+)\/?/',
2021-01-15 22:13:46 +08:00
'home_page' => 'https://www.imdb.com/',
],
self::SITE_DOUBAN => [
'url_pattern' => '/(?:https?:\/\/)?(?:(?:movie|www)\.)?douban\.com\/(?:subject|movie)\/(\d+)\/?/',
'home_page' => 'https://www.douban.com/',
2021-01-15 01:12:44 +08:00
],
self::SITE_BANGUMI => [
'url_pattern' => '/(?:https?:\/\/)?(?:bgm\.tv|bangumi\.tv|chii\.in)\/subject\/(\d+)\/?/',
2021-01-15 22:13:46 +08:00
'home_page' => 'https://bangumi.tv/',
2021-01-15 01:12:44 +08:00
],
];
public function __construct()
{
$this->setApiPoint('https://ptgen.rhilip.info');
}
public function getApiPoint(): string
{
return $this->apiPoint;
}
public function setApiPoint(string $apiPoint)
{
$this->apiPoint = $apiPoint;
}
2021-01-18 00:41:35 +08:00
public function generate(string $url, bool $withoutCache = false): array
{
$parsed = $this->parse($url);
$targetUrl = sprintf('%s/?site=%s&sid=%s', trim($this->apiPoint, '/'), $parsed['site'] , $parsed['id']);
return $this->request($targetUrl, $withoutCache);
}
public function parse(string $url): array
2021-01-15 01:12:44 +08:00
{
2021-01-15 22:13:46 +08:00
foreach (self::$validSites as $site => $info) {
if (preg_match($info['url_pattern'], $url, $matches)) {
2021-01-18 00:41:35 +08:00
return [
'site' => $site,
'url' => $matches[0],
'id' => $matches[1]
];
2021-01-15 22:13:46 +08:00
}
}
throw new PTGenException("invalid url: $url");
2021-01-15 01:12:44 +08:00
}
2021-01-18 00:41:35 +08:00
private function buildDetailsPageTableRow($torrentId, $ptGenArr, $site)
2021-01-15 01:12:44 +08:00
{
2021-01-15 22:13:46 +08:00
global $lang_details;
$ptGenFormatted = $ptGenArr['format'];
$prefix = sprintf("[img]%s[/img]\n", $ptGenArr['poster']);
$ptGenFormatted = mb_substr($ptGenFormatted, mb_strlen($prefix, 'utf-8') + 1);
$ptGenFormatted = format_comment($ptGenFormatted);
2021-01-18 00:41:35 +08:00
$ptGenFormatted .= sprintf(
'%s%s%s<a href="retriver.php?id=%s&type=1&siteid=%s">%s</a>',
$lang_details['text_information_updated_at'], date('Y-m-d H:i:s', intval($ptGenArr['generate_at'] / 1000)), $lang_details['text_might_be_outdated'],
$torrentId, $site, $lang_details['text_here_to_update']
);
2021-01-15 22:13:46 +08:00
$titleShowOrHide = $lang_details['title_show_or_hide'] ?? '';
$id = 'pt-gen-' . $site;
$html = <<<HTML
<tr>
<td class="rowhead">
<a href="javascript: klappe_ext('{$id}')">
<span class="nowrap">
<img id="pic{$id}" class="minus" src="pic/trans.gif" alt="Show/Hide" title="{$titleShowOrHide}" />
PT-Gen-{$site}
</span>
</a>
<div id="poster{$id}">
<img src="{$ptGenArr['poster']}" width="105" onclick="Preview(this);" alt="poster" />
</div>
</td>
<td class="rowfollow" align="left">
<div id="k{$id}">
{$ptGenFormatted}
</div>
</td>
</tr>
HTML;
return $html;
2021-01-15 01:12:44 +08:00
}
2021-01-18 00:41:35 +08:00
private function request(string $url, bool $withoutCache = false): array
2021-01-15 01:12:44 +08:00
{
2021-01-15 22:13:46 +08:00
global $Cache;
2021-01-15 01:12:44 +08:00
$logPrefix = "url: $url";
2021-01-18 00:41:35 +08:00
$cacheKey = $this->getApiPointResultCacheKey($url);
if (!$withoutCache) {
$cache = $Cache->get_value($cacheKey);
if ($cache) {
do_log("$logPrefix, from cache");
return $cache;
}
2021-01-15 22:13:46 +08:00
}
2021-01-15 01:12:44 +08:00
$http = new Client();
2021-01-15 22:13:46 +08:00
$response = $http->get($url, ['timeout' => 10]);
2021-01-15 01:12:44 +08:00
$statusCode = $response->getStatusCode();
if ($statusCode != 200) {
$msg = "api point response http status code: $statusCode";
do_log("$logPrefix, $msg");
throw new PTGenException($msg);
}
$bodyString = (string)$response->getBody();
if (empty($bodyString)) {
$msg = "response body empty";
do_log("$logPrefix, $msg");
throw new PTGenException($msg);
}
$bodyArr = json_decode($bodyString, true);
if (empty($bodyArr) || !is_array($bodyArr)) {
$msg = "response body error: $bodyString";
do_log("$logPrefix, $msg");
throw new PTGenException($msg);
}
if (!isset($bodyArr['success']) || !$bodyArr['success']) {
$msg = "error: " . $bodyArr['error'] ?? '';
do_log("$logPrefix, response: $bodyString");
throw new PTGenException($msg);
}
2021-01-15 22:13:46 +08:00
$Cache->cache_value($cacheKey, $bodyArr, 24 * 3600);
2021-01-18 00:41:35 +08:00
do_log("$logPrefix, success get from api point");
2021-01-15 22:13:46 +08:00
return $bodyArr;
2021-01-15 01:12:44 +08:00
}
2021-01-18 00:41:35 +08:00
public function deleteApiPointResultCache($url)
{
global $Cache;
$Cache->delete_value($this->getApiPointResultCacheKey($url));
}
private function getApiPointResultCacheKey($url)
{
return __METHOD__ . "_$url";
}
2021-01-15 22:13:46 +08:00
public function renderUploadPageFormInput($ptGen = '')
2021-01-15 01:12:44 +08:00
{
2021-01-15 22:13:46 +08:00
global $lang_functions;
$html = '';
$ptGen = (array)json_decode($ptGen, true);
foreach (self::$validSites as $site => $info) {
$value = $ptGen[$site]['link'] ?? '';
$x = $lang_functions["row_pt_gen_{$site}_url"];
$y = "<input type=\"text\" style=\"width: 650px;\" name=\"pt_gen[{$site}][link]\" value=\"{$value}\" /><br /><font class=\"medium\">".$lang_functions["text_pt_gen_{$site}_url_note"]."</font>";
$html .= tr($x, $y, 1);
2021-01-15 01:12:44 +08:00
}
2021-01-15 22:13:46 +08:00
return $html;
}
2021-01-18 00:41:35 +08:00
public function renderDetailsPageDescription($torrentId, array $torrentPtGenArr): array
2021-01-15 22:13:46 +08:00
{
$html = '';
$jsonArr = [];
$update = false;
foreach (self::$validSites as $site => $info) {
if (empty($torrentPtGenArr[$site]['link'])) {
continue;
}
$link = $torrentPtGenArr[$site]['link'];
$data = $torrentPtGenArr[$site]['data'] ?? [];
if (!empty($data)) {
$jsonArr[$site] = [
'link' => $link,
'data' => $data,
];
2021-01-18 00:41:35 +08:00
$html .= $this->buildDetailsPageTableRow($torrentId, $data, $site);
2021-01-15 22:13:46 +08:00
} else {
$ptGenArr = $this->generate($torrentPtGenArr[$site]['link']);
$jsonArr[$site] = [
'link' => $link,
'data' => $ptGenArr,
];
2021-01-18 00:41:35 +08:00
$html .= $this->buildDetailsPageTableRow($torrentId, $ptGenArr, $site);
2021-01-15 22:13:46 +08:00
if (!$update) {
$update = true;
}
}
2021-01-15 01:12:44 +08:00
}
2021-01-15 22:13:46 +08:00
return ['json_arr' => $jsonArr, 'html' => $html, 'update' => $update];
2021-01-15 01:12:44 +08:00
}
}