diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BridgeAbstract.php | 22 | ||||
-rw-r--r-- | lib/CacheInterface.php | 4 | ||||
-rw-r--r-- | lib/TwitterClient.php | 15 | ||||
-rw-r--r-- | lib/contents.php | 42 |
4 files changed, 30 insertions, 53 deletions
diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index f024393d..eb9d5a3c 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -409,26 +409,15 @@ abstract class BridgeAbstract implements BridgeInterface /** * Loads a cached value for the specified key * - * @param int $duration Cache duration (optional) + * @param int $timeout Cache duration (optional) * @return mixed Cached value or null if the key doesn't exist or has expired */ - protected function loadCacheValue(string $key, $duration = null) + protected function loadCacheValue(string $key, int $timeout = 86400) { $cache = RssBridge::getCache(); - // Create class name without the namespace part - $scope = $this->getShortName(); - $cache->setScope($scope); + $cache->setScope($this->getShortName()); $cache->setKey([$key]); - $timestamp = $cache->getTime(); - - if ( - $duration - && $timestamp - && $timestamp < time() - $duration - ) { - return null; - } - return $cache->loadData(); + return $cache->loadData($timeout); } /** @@ -439,8 +428,7 @@ abstract class BridgeAbstract implements BridgeInterface protected function saveCacheValue(string $key, $value) { $cache = RssBridge::getCache(); - $scope = $this->getShortName(); - $cache->setScope($scope); + $cache->setScope($this->getShortName()); $cache->setKey([$key]); $cache->saveData($value); } diff --git a/lib/CacheInterface.php b/lib/CacheInterface.php index 414a9c84..85aa830f 100644 --- a/lib/CacheInterface.php +++ b/lib/CacheInterface.php @@ -6,11 +6,11 @@ interface CacheInterface public function setKey(array $key): void; - public function loadData(); + public function loadData(int $timeout = 86400); public function saveData($data): void; public function getTime(): ?int; - public function purgeCache(int $seconds): void; + public function purgeCache(int $timeout = 86400): void; } diff --git a/lib/TwitterClient.php b/lib/TwitterClient.php index cdedcbc1..341610a4 100644 --- a/lib/TwitterClient.php +++ b/lib/TwitterClient.php @@ -11,8 +11,13 @@ class TwitterClient public function __construct(CacheInterface $cache) { $this->cache = $cache; - $this->authorization = 'AAAAAAAAAAAAAAAAAAAAAGHtAgAAAAAA%2Bx7ILXNILCqkSGIzy6faIHZ9s3Q%3DQy97w6SIrzE7lQwPJEYQBsArEE2fC25caFwRBvAGi456G09vGR'; + + $cache->setScope('twitter'); + $cache->setKey(['cache']); + $cache->purgeCache(60 * 60 * 3); + $this->data = $this->cache->loadData() ?? []; + $this->authorization = 'AAAAAAAAAAAAAAAAAAAAAGHtAgAAAAAA%2Bx7ILXNILCqkSGIzy6faIHZ9s3Q%3DQy97w6SIrzE7lQwPJEYQBsArEE2fC25caFwRBvAGi456G09vGR'; } public function fetchUserTweets(string $screenName): \stdClass @@ -22,7 +27,6 @@ class TwitterClient $userInfo = $this->fetchUserInfoByScreenName($screenName); } catch (HttpException $e) { if ($e->getCode() === 403) { - Logger::info('The guest token has expired'); $this->data['guest_token'] = null; $this->fetchGuestToken(); $userInfo = $this->fetchUserInfoByScreenName($screenName); @@ -35,7 +39,6 @@ class TwitterClient $timeline = $this->fetchTimeline($userInfo->rest_id); } catch (HttpException $e) { if ($e->getCode() === 403) { - Logger::info('The guest token has expired'); $this->data['guest_token'] = null; $this->fetchGuestToken(); $timeline = $this->fetchTimeline($userInfo->rest_id); @@ -88,7 +91,6 @@ class TwitterClient private function fetchGuestToken(): void { if (isset($this->data['guest_token'])) { - Logger::info('Reusing cached guest token: ' . $this->data['guest_token']); return; } $url = 'https://api.twitter.com/1.1/guest/activate.json'; @@ -99,7 +101,6 @@ class TwitterClient $this->cache->setScope('twitter'); $this->cache->setKey(['cache']); $this->cache->saveData($this->data); - Logger::info("Fetch new guest token: $guest_token"); } private function fetchUserInfoByScreenName(string $screenName) @@ -115,7 +116,7 @@ class TwitterClient 'https://twitter.com/i/api/graphql/hc-pka9A7gyS3xODIafnrQ/UserByScreenName?variables=%s', urlencode(json_encode($variables)) ); - $response = json_decode(getContents($url, $this->createHttpHeaders())); + $response = Json::decode(getContents($url, $this->createHttpHeaders()), false); if (isset($response->errors)) { // Grab the first error message throw new \Exception(sprintf('From twitter api: "%s"', $response->errors[0]->message)); @@ -168,7 +169,7 @@ class TwitterClient urlencode(json_encode($variables)), urlencode(json_encode($features)) ); - $response = json_decode(getContents($url, $this->createHttpHeaders())); + $response = Json::decode(getContents($url, $this->createHttpHeaders()), false); return $response; } diff --git a/lib/contents.php b/lib/contents.php index b6b74539..454f2066 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -100,9 +100,6 @@ function getContents( bool $returnFull = false ) { $httpClient = RssBridge::getHttpClient(); - $cache = RssBridge::getCache(); - $cache->setScope('server'); - $cache->setKey([$url]); // Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102 $defaultHttpHeaders = [ @@ -138,6 +135,11 @@ function getContents( if (Configuration::getConfig('proxy', 'url') && !defined('NOPROXY')) { $config['proxy'] = Configuration::getConfig('proxy', 'url'); } + + $cache = RssBridge::getCache(); + $cache->setScope('server'); + $cache->setKey([$url]); + if (!Debug::isEnabled() && $cache->getTime()) { $config['if_not_modified_since'] = $cache->getTime(); } @@ -384,7 +386,7 @@ function getSimpleHTMLDOM( * _Notice_: Cached contents are forcefully removed after 24 hours (86400 seconds). * * @param string $url The URL. - * @param int $duration Cache duration in seconds. + * @param int $timeout Cache duration in seconds. * @param array $header (optional) A list of cURL header. * For more information follow the links below. * * https://php.net/manual/en/function.curl-setopt.php @@ -409,7 +411,7 @@ function getSimpleHTMLDOM( */ function getSimpleHTMLDOMCached( $url, - $duration = 86400, + $timeout = 86400, $header = [], $opts = [], $lowercase = true, @@ -422,29 +424,15 @@ function getSimpleHTMLDOMCached( $cache = RssBridge::getCache(); $cache->setScope('pages'); $cache->setKey([$url]); - - // Determine if cached file is within duration - $time = $cache->getTime(); - if ( - $time - && time() - $duration < $time - && !Debug::isEnabled() - ) { - // Cache hit - $content = $cache->loadData(); - } else { - $content = getContents( - $url, - $header ?? [], - $opts ?? [] - ); - if ($content) { - $cache->setScope('pages'); - $cache->setKey([$url]); - $cache->saveData($content); - } + $content = $cache->loadData($timeout); + if (!$content || Debug::isEnabled()) { + $content = getContents($url, $header ?? [], $opts ?? []); + } + if ($content) { + $cache->setScope('pages'); + $cache->setKey([$url]); + $cache->saveData($content); } - return str_get_html( $content, $lowercase, |