feat: custom http ua in AO3, fix #3188 (#3189)

* refactor

* feat: custom http ua in AO3, #3188
This commit is contained in:
Dag 2022-12-13 09:53:42 +01:00 committed by GitHub
parent 933be15a77
commit a13c4624fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 28 deletions

View File

@ -34,8 +34,30 @@ class AO3Bridge extends BridgeAbstract
]
];
// Feed for lists of works (e.g. recent works, search results, filtered tags,
// bookmarks, series, collections).
public function collectData()
{
switch ($this->queriedContext) {
case 'Bookmarks':
$user = $this->getInput('user');
$this->title = $user;
$url = self::URI
. '/users/' . $user
. '/bookmarks?bookmark_search[sort_column]=bookmarkable_date';
$this->collectList($url);
break;
case 'List':
$this->collectList($this->getInput('url'));
break;
case 'Work':
$this->collectWork($this->getInput('id'));
break;
}
}
/**
* Feed for lists of works (e.g. recent works, search results, filtered tags,
* bookmarks, series, collections).
*/
private function collectList($url)
{
$html = getSimpleHTMLDOM($url);
@ -64,11 +86,14 @@ class AO3Bridge extends BridgeAbstract
}
}
// Feed for recent chapters of a specific work.
/**
* Feed for recent chapters of a specific work.
*/
private function collectWork($id)
{
$url = self::URI . "/works/$id/navigate";
$html = getSimpleHTMLDOM($url);
$response = _http_request($url, ['useragent' => 'rss-bridge bot (https://github.com/RSS-Bridge/rss-bridge)']);
$html = \str_get_html($response['body']);
$html = defaultLinkTo($html, self::URI);
$this->title = $html->find('h2 a', 0)->plaintext;
@ -93,27 +118,6 @@ class AO3Bridge extends BridgeAbstract
$this->items = array_reverse($this->items);
}
public function collectData()
{
switch ($this->queriedContext) {
case 'Bookmarks':
$user = $this->getInput('user');
$this->title = $user;
$url = self::URI
. '/users/' . $user
. '/bookmarks?bookmark_search[sort_column]=bookmarkable_date';
return $this->collectList($url);
case 'List':
return $this->collectList(
$this->getInput('url')
);
case 'Work':
return $this->collectWork(
$this->getInput('id')
);
}
}
public function getName()
{
$name = parent::getName() . " $this->queriedContext";

View File

@ -125,6 +125,8 @@ function getContents(
$httpHeadersNormalized[$headerName] = $headerValue;
}
$config = [
'useragent' => Configuration::getConfig('http', 'useragent'),
'timeout' => Configuration::getConfig('http', 'timeout'),
'headers' => array_merge($defaultHttpHeaders, $httpHeadersNormalized),
'curl_options' => $curlOptions,
];
@ -215,8 +217,8 @@ function getContents(
function _http_request(string $url, array $config = []): array
{
$defaults = [
'useragent' => Configuration::getConfig('http', 'useragent'),
'timeout' => Configuration::getConfig('http', 'timeout'),
'useragent' => null,
'timeout' => 5,
'headers' => [],
'proxy' => null,
'curl_options' => [],
@ -236,7 +238,9 @@ function _http_request(string $url, array $config = []): array
$httpHeaders[] = sprintf('%s: %s', $name, $value);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
if ($config['useragent']) {
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
}
curl_setopt($ch, CURLOPT_TIMEOUT, $config['timeout']);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);