1
0
Fork 0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-02-20 09:09:55 +03:00

refactor: return proper response object ()

This commit is contained in:
Dag 2024-07-31 17:30:06 +02:00 committed by GitHub
parent aa3989873c
commit 891c8979a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 69 additions and 84 deletions

View file

@ -54,8 +54,8 @@ class ConnectivityAction implements ActionInterface
]; ];
try { try {
$response = getContents($bridge::URI, [], $curl_opts, true); $response = getContents($bridge::URI, [], $curl_opts, true);
$result['http_code'] = $response['code']; $result['http_code'] = $response->getCode();
if (in_array($response['code'], [200])) { if (in_array($result['http_code'], [200])) {
$result['successful'] = true; $result['successful'] = true;
} }
} catch (\Exception $e) { } catch (\Exception $e) {

View file

@ -284,8 +284,7 @@ class BadDragonBridge extends BridgeAbstract
case 'Clearance': case 'Clearance':
$toyData = json_decode(getContents($this->inputToURL(true))); $toyData = json_decode(getContents($this->inputToURL(true)));
$productList = json_decode(getContents(self::URI $productList = json_decode(getContents(self::URI . 'api/inventory-toy/product-list'));
. 'api/inventory-toy/product-list'));
foreach ($toyData->toys as $toy) { foreach ($toyData->toys as $toy) {
$item = []; $item = [];

View file

@ -111,12 +111,12 @@ class BandcampBridge extends BridgeAbstract
$url = self::URI . 'api/hub/1/dig_deeper'; $url = self::URI . 'api/hub/1/dig_deeper';
$data = $this->buildRequestJson(); $data = $this->buildRequestJson();
$header = [ $header = [
'Content-Type: application/json', 'Content-Type: application/json',
'Content-Length: ' . strlen($data) 'Content-Length: ' . strlen($data),
]; ];
$opts = [ $opts = [
CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $data CURLOPT_POSTFIELDS => $data,
]; ];
$content = getContents($url, $header, $opts); $content = getContents($url, $header, $opts);
@ -314,7 +314,8 @@ class BandcampBridge extends BridgeAbstract
{ {
$url = self::URI . 'api/' . $endpoint . '?' . http_build_query($query_data); $url = self::URI . 'api/' . $endpoint . '?' . http_build_query($query_data);
// todo: 429 Too Many Requests happens a lot // todo: 429 Too Many Requests happens a lot
$data = json_decode(getContents($url)); $response = getContents($url);
$data = json_decode($response);
return $data; return $data;
} }

View file

@ -47,8 +47,10 @@ class CubariBridge extends BridgeAbstract
*/ */
public function collectData() public function collectData()
{ {
// TODO: fix trivial SSRF
$json = getContents($this->getInput('gist')); $json = getContents($this->getInput('gist'));
$jsonFile = json_decode($json, true);
$jsonFile = Json::decode($json);
$this->mangaTitle = $jsonFile['title']; $this->mangaTitle = $jsonFile['title'];

View file

@ -24,7 +24,8 @@ class DemosBerlinBridge extends BridgeAbstract
public function collectData() public function collectData()
{ {
$json = getContents('https://www.berlin.de/polizei/service/versammlungsbehoerde/versammlungen-aufzuege/index.php/index/all.json'); $url = 'https://www.berlin.de/polizei/service/versammlungsbehoerde/versammlungen-aufzuege/index.php/index/all.json';
$json = getContents($url);
$jsonFile = json_decode($json, true); $jsonFile = json_decode($json, true);
$daysInterval = DateInterval::createFromDateString($this->getInput('days') . ' day'); $daysInterval = DateInterval::createFromDateString($this->getInput('days') . ' day');

View file

@ -78,13 +78,9 @@ class DerpibooruBridge extends BridgeAbstract
public function collectData() public function collectData()
{ {
$queryJson = json_decode(getContents( $url = self::URI . 'api/v1/json/search/images?filter_id=' . urlencode($this->getInput('f')) . '&q=' . urlencode($this->getInput('q'));
self::URI
. 'api/v1/json/search/images?filter_id=' $queryJson = json_decode(getContents($url));
. urlencode($this->getInput('f'))
. '&q='
. urlencode($this->getInput('q'))
));
foreach ($queryJson->images as $post) { foreach ($queryJson->images as $post) {
$item = []; $item = [];

View file

@ -50,7 +50,9 @@ class EZTVBridge extends BridgeAbstract
$eztv_uri = $this->getEztvUri(); $eztv_uri = $this->getEztvUri();
$ids = explode(',', trim($this->getInput('ids'))); $ids = explode(',', trim($this->getInput('ids')));
foreach ($ids as $id) { foreach ($ids as $id) {
$data = json_decode(getContents(sprintf('%s/api/get-torrents?imdb_id=%s', $eztv_uri, $id))); $url = sprintf('%s/api/get-torrents?imdb_id=%s', $eztv_uri, $id);
$json = getContents($url);
$data = json_decode($json);
if (!isset($data->torrents)) { if (!isset($data->torrents)) {
// No results // No results
continue; continue;

View file

@ -34,11 +34,9 @@ class ElloBridge extends BridgeAbstract
]; ];
if (!empty($this->getInput('u'))) { if (!empty($this->getInput('u'))) {
$postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header) or $postData = getContents(self::URI . 'api/v2/users/~' . urlencode($this->getInput('u')) . '/posts', $header);
returnServerError('Unable to query Ello API.');
} else { } else {
$postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header) or $postData = getContents(self::URI . 'api/v2/posts?terms=' . urlencode($this->getInput('s')), $header);
returnServerError('Unable to query Ello API.');
} }
$postData = json_decode($postData); $postData = json_decode($postData);
@ -117,7 +115,7 @@ class ElloBridge extends BridgeAbstract
$apiKey = $this->cache->get($cacheKey); $apiKey = $this->cache->get($cacheKey);
if (!$apiKey) { if (!$apiKey) {
$keyInfo = getContents(self::URI . 'api/webapp-token') or returnServerError('Unable to get token.'); $keyInfo = getContents(self::URI . 'api/webapp-token');
$apiKey = json_decode($keyInfo)->token->access_token; $apiKey = json_decode($keyInfo)->token->access_token;
$ttl = 60 * 60 * 20; $ttl = 60 * 60 * 20;
$this->cache->set($cacheKey, $apiKey, $ttl); $this->cache->set($cacheKey, $apiKey, $ttl);

View file

@ -31,7 +31,7 @@ class FDroidBridge extends BridgeAbstract
CURLOPT_NOBODY => true, CURLOPT_NOBODY => true,
]; ];
$reponse = getContents($url, [], $curlOptions, true); $reponse = getContents($url, [], $curlOptions, true);
$lastModified = $reponse['headers']['last-modified'][0] ?? null; $lastModified = $reponse->getHeader('last-modified');
$timestamp = strtotime($lastModified ?? 'today'); $timestamp = strtotime($lastModified ?? 'today');
return $timestamp; return $timestamp;
} }

View file

@ -32,7 +32,7 @@ class FunkBridge extends BridgeAbstract
$url .= '?size=' . $this->getInput('max'); $url .= '?size=' . $this->getInput('max');
} }
$jsonString = getContents($url) or returnServerError('No contents received!'); $jsonString = getContents($url);
$json = json_decode($jsonString, true); $json = json_decode($jsonString, true);
foreach ($json['list'] as $element) { foreach ($json['list'] as $element) {

View file

@ -41,8 +41,7 @@ class GlowficBridge extends BridgeAbstract
$first_page = 1; $first_page = 1;
} }
for ($page_offset = $first_page; $page_offset <= $metadata['Last-Page']; $page_offset++) { for ($page_offset = $first_page; $page_offset <= $metadata['Last-Page']; $page_offset++) {
$jsonContents = getContents($url . '/replies?page=' . $page_offset) or $jsonContents = getContents($url . '/replies?page=' . $page_offset);
returnClientError('Could not retrieve replies for page ' . $page_offset . '.');
$replies = json_decode($jsonContents); $replies = json_decode($jsonContents);
foreach ($replies as $reply) { foreach ($replies as $reply) {
$item = []; $item = [];
@ -75,8 +74,9 @@ class GlowficBridge extends BridgeAbstract
private function getPost() private function getPost()
{ {
$url = $this->getAPIURI(); $url = $this->getAPIURI();
$jsonPost = getContents($url) or returnClientError('Could not retrieve post metadata.'); $jsonPost = getContents($url);
$post = json_decode($jsonPost); $post = json_decode($jsonPost);
return $post; return $post;
} }

View file

@ -30,7 +30,7 @@ class InternationalInstituteForStrategicStudiesBridge extends BridgeAbstract
]; ];
$headers = [ $headers = [
'Accept: application/json, text/plain, */*', 'Accept: application/json, text/plain, */*',
'Content-Type: application/json;charset=UTF-8' 'Content-Type: application/json;charset=UTF-8',
]; ];
$json = getContents($url, $headers, $opts); $json = getContents($url, $headers, $opts);
$data = json_decode($json); $data = json_decode($json);

View file

@ -669,11 +669,11 @@ class ItakuBridge extends BridgeAbstract
if ($cache) { if ($cache) {
$data = $this->loadCacheValue($url); $data = $this->loadCacheValue($url);
if (is_null($data)) { if (is_null($data)) {
$data = getContents($url, $httpHeaders, $curlOptions) or returnServerError("Could not load $url"); $data = getContents($url, $httpHeaders, $curlOptions);
$this->saveCacheValue($url, $data); $this->saveCacheValue($url, $data);
} }
} else { } else {
$data = getContents($url, $httpHeaders, $curlOptions) or returnServerError("Could not load $url"); $data = getContents($url, $httpHeaders, $curlOptions);
} }
return json_decode($data, true); return json_decode($data, true);
} else { //get simpleHTMLDOM object } else { //get simpleHTMLDOM object

View file

@ -12,8 +12,7 @@ class KilledbyGoogleBridge extends BridgeAbstract
public function collectData() public function collectData()
{ {
$json = getContents(self::URI . '/graveyard.json') $json = getContents(self::URI . '/graveyard.json');
or returnServerError('Could not request: ' . self::URI . '/graveyard.json');
$this->handleJson($json); $this->handleJson($json);
$this->orderItems(); $this->orderItems();

View file

@ -52,8 +52,7 @@ Once a project reaches 10,000 supporters, it gets reviewed by the lego experts.'
CURLOPT_POST => 1, CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $this->getHttpPostData() CURLOPT_POSTFIELDS => $this->getHttpPostData()
]; ];
$responseData = getContents($this->getHttpPostURI(), $header, $opts) or $responseData = getContents($this->getHttpPostURI(), $header, $opts);
returnServerError('Unable to query Lego Ideas API.');
foreach (json_decode($responseData)->results as $project) { foreach (json_decode($responseData)->results as $project) {
preg_match('/datetime=\"(\S+)\"/', $project->entity->published_at, $date_matches); preg_match('/datetime=\"(\S+)\"/', $project->entity->published_at, $date_matches);

View file

@ -147,10 +147,9 @@ class OpenCVEBridge extends BridgeAbstract
for ($i = 1; $i <= $this->getInput('pages'); $i++) { for ($i = 1; $i <= $this->getInput('pages'); $i++) {
$queryPaginated = array_merge($query, ['page' => $i]); $queryPaginated = array_merge($query, ['page' => $i]);
$url = $instance . '/api/cve?' . http_build_query($queryPaginated); $url = $instance . '/api/cve?' . http_build_query($queryPaginated);
$response = getContents(
$url, $response = getContents($url, [$authHeader]);
[$authHeader]
);
$titlePrefix = ''; $titlePrefix = '';
if (count($queries) > 1) { if (count($queries) > 1) {
$titlePrefix = '[' . $queryName . '] '; $titlePrefix = '[' . $queryName . '] ';
@ -205,10 +204,8 @@ class OpenCVEBridge extends BridgeAbstract
private function fetchContents($cveItem, $titlePrefix, $instance, $authHeader) private function fetchContents($cveItem, $titlePrefix, $instance, $authHeader)
{ {
$url = $instance . '/api/cve/' . $cveItem->id; $url = $instance . '/api/cve/' . $cveItem->id;
$response = getContents(
$url, $response = getContents($url, [$authHeader]);
[$authHeader]
);
$datum = json_decode($response); $datum = json_decode($response);
$title = $this->getTitleFromDatum($datum, $titlePrefix); $title = $this->getTitleFromDatum($datum, $titlePrefix);

View file

@ -191,15 +191,12 @@ HEREDOC;
} }
} }
/**
* Extract the cookies obtained from the URL
* @return array the array containing the cookies set by the URL
*/
private function getCookiesHeaderValue($url) private function getCookiesHeaderValue($url)
{ {
$response = getContents($url, [], [], true); $response = getContents($url, [], [], true);
$setCookieHeaders = $response['headers']['set-cookie'] ?? []; $setCookieHeaders = $response->getHeader('set-cookie', true);
$cookies = array_map(fn($c): string => explode(';', $c)[0], $setCookieHeaders); $cookies = array_map(fn($c): string => explode(';', $c)[0], $setCookieHeaders);
return implode('; ', $cookies); return implode('; ', $cookies);
} }

View file

@ -332,21 +332,20 @@ class PixivBridge extends BridgeAbstract
} }
if ($cache) { if ($cache) {
$data = $this->loadCacheValue($url); $response = $this->loadCacheValue($url);
if (!$data) { if (!$response || is_array($response)) {
$data = getContents($url, $httpHeaders, $curlOptions, true); $response = getContents($url, $httpHeaders, $curlOptions, true);
$this->saveCacheValue($url, $data); $this->saveCacheValue($url, $response);
} }
} else { } else {
$data = getContents($url, $httpHeaders, $curlOptions, true); $response = getContents($url, $httpHeaders, $curlOptions, true);
} }
$this->checkCookie($data['headers']); $this->checkCookie($response->getHeaders());
if ($getJSON) { if ($getJSON) {
return json_decode($data['content'], true); return json_decode($response->getBody(), true);
} else {
return $data['content'];
} }
return $response->getBody();
} }
} }

View file

@ -22,7 +22,7 @@ class RainbowSixSiegeBridge extends BridgeAbstract
$dlUrl = $dlUrl . '&limit=6&mediaFilter=all&skip=0&startIndex=0&tags=BR-rainbow-six%20GA-siege'; $dlUrl = $dlUrl . '&limit=6&mediaFilter=all&skip=0&startIndex=0&tags=BR-rainbow-six%20GA-siege';
$dlUrl = $dlUrl . '&locale=en-us&fallbackLocale=en-us&environment=master'; $dlUrl = $dlUrl . '&locale=en-us&fallbackLocale=en-us&environment=master';
$jsonString = getContents($dlUrl, [ $jsonString = getContents($dlUrl, [
'Authorization: ' . self::NIMBUS_API_KEY 'Authorization: ' . self::NIMBUS_API_KEY,
]); ]);
$json = json_decode($jsonString, true); $json = json_decode($jsonString, true);

View file

@ -149,7 +149,7 @@ class RedditBridge extends BridgeAbstract
$response = getContents($url, ['User-Agent: ' . $useragent], [], true); $response = getContents($url, ['User-Agent: ' . $useragent], [], true);
$json = $response['content']; $json = $response->getBody();
$parsedJson = Json::decode($json, false); $parsedJson = Json::decode($json, false);

View file

@ -417,9 +417,11 @@ class ReutersBridge extends BridgeAbstract
$get_embed_url = 'https://publish.twitter.com/oembed?url=' $get_embed_url = 'https://publish.twitter.com/oembed?url='
. urlencode($tweet_url) . . urlencode($tweet_url) .
'&partner=&hide_thread=false'; '&partner=&hide_thread=false';
$oembed_json = json_decode(getContents($get_embed_url), true); $oembed_json = json_decode(getContents($get_embed_url), true);
$embed .= $oembed_json['html']; $embed .= $oembed_json['html'];
} catch (Exception $e) { // In case not found any tweet. } catch (\Exception $e) {
// In case not found any tweet.
$embed .= ''; $embed .= '';
} }
break; break;

View file

@ -68,9 +68,4 @@ class RoadAndTrackBridge extends BridgeAbstract
$item['content'] = $content; $item['content'] = $content;
return $item; return $item;
} }
private function getArticleContent($article)
{
return getContents($article->contentUrl);
}
} }

View file

@ -286,9 +286,9 @@ class SpotifyBridge extends BridgeAbstract
} else { } else {
$basicAuth = base64_encode(sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret'))); $basicAuth = base64_encode(sprintf('%s:%s', $this->getInput('clientid'), $this->getInput('clientsecret')));
$json = getContents('https://accounts.spotify.com/api/token', [ $json = getContents('https://accounts.spotify.com/api/token', [
"Authorization: Basic $basicAuth" "Authorization: Basic $basicAuth",
], [ ], [
CURLOPT_POSTFIELDS => 'grant_type=client_credentials' CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
]); ]);
$data = Json::decode($json); $data = Json::decode($json);
$this->token = $data['access_token']; $this->token = $data['access_token'];

View file

@ -20,8 +20,12 @@ class SummitsOnTheAirBridge extends BridgeAbstract
public function collectData() public function collectData()
{ {
$header = ['Content-type:application/json']; $header = [
$opts = [CURLOPT_HTTPGET => 1]; 'Content-type:application/json',
];
$opts = [
CURLOPT_HTTPGET => 1,
];
$json = getContents($this->getURI() . $this->getInput('c'), $header, $opts); $json = getContents($this->getURI() . $this->getInput('c'), $header, $opts);
$spots = json_decode($json, true); $spots = json_decode($json, true);

View file

@ -598,7 +598,7 @@ EXTERNAL;
private function makeApiCall($api, $authHeaders, $params) private function makeApiCall($api, $authHeaders, $params)
{ {
$uri = self::API_URI . $api . '?' . http_build_query($params); $uri = self::API_URI . $api . '?' . http_build_query($params);
$result = getContents($uri, $authHeaders, [], false); $result = getContents($uri, $authHeaders);
$data = json_decode($result); $data = json_decode($result);
return $data; return $data;
} }

View file

@ -92,7 +92,7 @@ class UnogsBridge extends BridgeAbstract
{ {
$header = [ $header = [
'Referer: https://unogs.com/', 'Referer: https://unogs.com/',
'referrer: http://unogs.com' 'referrer: http://unogs.com',
]; ];
$raw = getContents($url, $header); $raw = getContents($url, $header);

View file

@ -511,11 +511,11 @@ class VkBridge extends BridgeAbstract
while ($redirects < 2) { while ($redirects < 2) {
$response = getContents($uri, $httpHeaders, [CURLOPT_FOLLOWLOCATION => false], true); $response = getContents($uri, $httpHeaders, [CURLOPT_FOLLOWLOCATION => false], true);
if (in_array($response['code'], [200, 304])) { if (in_array($response->getCode(), [200, 304])) {
return $response['content']; return $response->getBody();
} }
$headers = $response['headers']; $headers = $response->getHeaders();
$uri = urljoin(self::URI, $headers['location'][0]); $uri = urljoin(self::URI, $headers['location'][0]);
if (str_contains($uri, '/429.html')) { if (str_contains($uri, '/429.html')) {

View file

@ -5,8 +5,8 @@
* *
* @param array $httpHeaders E.g. ['Content-type: text/plain'] * @param array $httpHeaders E.g. ['Content-type: text/plain']
* @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3] * @param array $curlOptions Associative array e.g. [CURLOPT_MAXREDIRS => 3]
* @param bool $returnFull Whether to return an array: ['code' => int, 'headers' => array, 'content' => string] * @param bool $returnFull Whether to return Response object
* @return string|array * @return string|Response
*/ */
function getContents( function getContents(
string $url, string $url,
@ -113,13 +113,7 @@ function getContents(
throw $e; throw $e;
} }
if ($returnFull === true) { if ($returnFull === true) {
// todo: return the actual response object return $response;
return [
'code' => $response->getCode(),
'headers' => $response->getHeaders(),
// For legacy reasons, use 'content' instead of 'body'
'content' => $response->getBody(),
];
} }
return $response->getBody(); return $response->getBody();
} }