Attempt to circumvent Cloudflare on Oatmealdome's API...

This commit is contained in:
flash 2024-04-12 00:44:43 +00:00
parent 901f69a424
commit b5b545ae7e
2 changed files with 26 additions and 7 deletions

View file

@ -2,7 +2,7 @@
namespace Satori; namespace Satori;
final class SHttp { final class SHttp {
public static function getJson(string $url): mixed { public static function getJson(string $url, bool $pretendToBeFirefox = false): mixed {
$curl = curl_init($url); $curl = curl_init($url);
curl_setopt_array($curl, [ curl_setopt_array($curl, [
CURLOPT_AUTOREFERER => true, CURLOPT_AUTOREFERER => true,
@ -17,16 +17,35 @@ final class SHttp {
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS, CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_TIMEOUT => 5, CURLOPT_TIMEOUT => 5,
CURLOPT_USERAGENT => sprintf('Satori/%s (+https://fii.moe/satori)', SAT_VERSION),
CURLOPT_HTTPHEADER => ['Accept: application/json'],
]); ]);
if($pretendToBeFirefox) {
curl_setopt_array($curl, [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0',
CURLOPT_HTTPHEADER => [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language: en-GB,en;q=0.5',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Sec-Fetch-Site: none',
'Sec-Fetch-User: ?1',
'Upgrade-Insecure-Requests: 1',
],
]);
} else {
curl_setopt_array($curl, [
CURLOPT_USERAGENT => sprintf('Satori/%s (+https://fii.moe/satori)', SAT_VERSION),
CURLOPT_HTTPHEADER => ['Accept: application/json'],
]);
}
$response = json_decode(curl_exec($curl), true); $response = json_decode(curl_exec($curl), true);
curl_close($curl); curl_close($curl);
return $response; return $response;
} }
public static function getJsonCached(string $url): mixed { public static function getJsonCached(string $url, bool $pretendToBeFirefox = false): mixed {
return SFileCache::for15mins($url, fn() => self::getJson($url)); return SFileCache::for15mins($url, fn() => self::getJson($url, $pretendToBeFirefox));
} }
} }

View file

@ -57,7 +57,7 @@ class Splatoon1Game implements ISplatoonGame, ISplatoonHasSchedules {
public function createLocale(string $locale): ISplatoonLocale { public function createLocale(string $locale): ISplatoonLocale {
return new Splatoon1Locale( return new Splatoon1Locale(
SHttp::getJsonCached(sprintf(self::LOCALE_URL, $locale)) SHttp::getJsonCached(sprintf(self::LOCALE_URL, $locale), true)
); );
} }
@ -94,7 +94,7 @@ class Splatoon1Game implements ISplatoonGame, ISplatoonHasSchedules {
private ?array $rawSchedule = null; private ?array $rawSchedule = null;
private function getRawScheduleData(): array { private function getRawScheduleData(): array {
if($this->rawSchedule === null) if($this->rawSchedule === null)
$this->rawSchedule = SHttp::getJsonCached(self::SCHED_URL); $this->rawSchedule = SHttp::getJsonCached(self::SCHED_URL, true);
return $this->rawSchedule; return $this->rawSchedule;
} }