Skip to content

Commit

Permalink
[FurAffinityBridge] Option for instance host to add custom cookie (#3638
Browse files Browse the repository at this point in the history
)

* added custom cookie config

* appease phpunit
  • Loading branch information
mruac authored Aug 29, 2023
1 parent 9e33a15 commit f0ec797
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions bridges/FurAffinityBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ class FurAffinityBridge extends BridgeAbstract
const URI = 'https://www.furaffinity.net';
const CACHE_TIMEOUT = 300; // 5min
const DESCRIPTION = 'Returns posts from various sections of FurAffinity';
const MAINTAINER = 'Roliga';
const MAINTAINER = 'Roliga, mruac';
const CONFIGURATION = [
'aCookie' => [
'required' => false,
'defaultValue' => 'ca6e4566-9d81-4263-9444-653b142e35f8'

],
'bCookie' => [
'required' => false,
'defaultValue' => '4ce65691-b50f-4742-a990-bf28d6de16ee'
]
];
const PARAMETERS = [
'Search' => [
'q' => [
Expand Down Expand Up @@ -594,7 +605,7 @@ class FurAffinityBridge extends BridgeAbstract
* This was aquired by creating a new user on FA then
* extracting the cookie from the browsers dev console.
*/
const FA_AUTH_COOKIE = 'b=4ce65691-b50f-4742-a990-bf28d6de16ee; a=ca6e4566-9d81-4263-9444-653b142e35f8';
private $FA_AUTH_COOKIE;

public function detectParameters($url)
{
Expand Down Expand Up @@ -662,7 +673,14 @@ public function getName()
. '\'s Folder '
. $this->getInput('folder-id');
default:
return parent::getName();
$name = parent::getName();
if ($this->getOption('aCookie') !== null) {
$username = $this->loadCacheValue('username');
if ($username !== null) {
$name = $username . '\'s ' . parent::getName();
}
}
return $name;
}
}

Expand Down Expand Up @@ -741,6 +759,7 @@ public function getURI()

public function collectData()
{
$this->FA_AUTH_COOKIE = 'b=' . $this->getOption('bCookie') . '; a=' . $this->getOption('aCookie');
switch ($this->queriedContext) {
case 'Search':
$data = [
Expand Down Expand Up @@ -806,32 +825,44 @@ private function postFASimpleHTMLDOM($data)
$header = [
'Host: ' . parse_url(self::URI, PHP_URL_HOST),
'Content-Type: application/x-www-form-urlencoded',
'Cookie: ' . self::FA_AUTH_COOKIE
'Cookie: ' . $this->FA_AUTH_COOKIE
];

$html = getSimpleHTMLDOM($this->getURI(), $header, $opts);
$html = defaultLinkTo($html, $this->getURI());

$this->saveLoggedInUser($html);
return $html;
}

private function getFASimpleHTMLDOM($url, $cache = false)
{
$header = [
'Cookie: ' . self::FA_AUTH_COOKIE
'Cookie: ' . $this->FA_AUTH_COOKIE
];

if ($cache) {
$html = getSimpleHTMLDOMCached($url, 86400, $header); // 24 hours
} else {
$html = getSimpleHTMLDOM($url, $header);
}

$this->saveLoggedInUser($html);
$html = defaultLinkTo($html, $url);

return $html;
}

private function saveLoggedInUser($html)
{
$current_user = $html->find('#my-username', 0);
if ($current_user !== null) {
preg_match('/^(?:My FA \( |~)(.*?)(?: \)|)$/', trim($current_user->plaintext), $matches);
$current_user = $current_user ? $matches[1] : null;
if ($current_user !== null) {
$this->saveCacheValue('username', $current_user);
}
}
}

private function itemsFromJournalList($html, $limit)
{
foreach ($html->find('table[id^=jid:]') as $journal) {
Expand Down

0 comments on commit f0ec797

Please sign in to comment.