mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-14 16:04:42 +02:00
refactor(cache): extract and encapsulate cache expiration logic (#3547)
* refactor(cache): extract and encapsulate cache expiration logic * fix: logic bug in getSimpleHTMLDOMCached * fix: silly me, index should of course be on the key column * silly me again, PRIMARY keys get index by default lol * comment out the delete portion in loadData * remove a few log statements * tweak twitter cache timeout
This commit is contained in:
parent
087e790ec1
commit
6254b8593e
12 changed files with 124 additions and 137 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue