From 4ef5ca50c6f9080481e34fa305af952187b39d88 Mon Sep 17 00:00:00 2001 From: July Date: Sat, 10 Aug 2024 11:36:58 -0400 Subject: [PATCH] [KemonoBridge] Add KemonoBridge (#4192) * [KemonoBridge] Add KemonoBridge * refactor * [KemonoBridge] fix categories in cases where it's a proper json array --------- Co-authored-by: Dag --- bridges/KemonoBridge.php | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 bridges/KemonoBridge.php diff --git a/bridges/KemonoBridge.php b/bridges/KemonoBridge.php new file mode 100644 index 00000000..53a820dc --- /dev/null +++ b/bridges/KemonoBridge.php @@ -0,0 +1,93 @@ + [ + 'name' => 'Content service', + 'type' => 'list', + 'defaultValue' => 'patreon', + 'values' => [ + 'Patreon' => 'patreon', + 'Pixiv Fanbox' => 'fanbox', + 'Fantia' => 'fantia', + 'Boosty' => 'boosty', + 'Gumroad' => 'gumroad', + 'SubscribeStar' => 'subscribestar', + ] + ], + 'user' => [ + 'name' => 'User ID/Name', + 'exampleValue' => '9069743', # Thomas Joy + 'required' => true, + ] + ]]; + + private $title; + + public function collectData() + { + $api = parent::getURI() . 'api/v1/'; + $url = $api . $this->getInput('service') . '/user/' . $this->getInput('user'); + $api_response = getContents($url); + $json = Json::decode($api_response); + + $url .= '/profile'; + $api_response = getContents($url); + $profile = Json::decode($api_response); + $this->title = ucfirst($profile['name']); + + foreach ($json as $element) { + $item = []; + $item['author'] = $this->title; + $item['content'] = $element['content']; + $item['timestamp'] = strtotime($element['published']); + $item['title'] = $element['title']; + $item['uid'] = $element['id']; + $item['uri'] = $this->getURI() . '/post/' . $item['uid']; + + if ($element['tags']) { + $tags = $element['tags']; + if (is_array($tags)) { + $item['categories'] = $tags; + } else { + $tags = preg_replace('/^{"?/', '["', $tags); + $tags = preg_replace('/"?}$/', '"]', $tags); + $item['categories'] = Json::decode($tags); + } + } + + $item['enclosures'] = []; + if (array_key_exists('url', $element['embed'])) { + $item['enclosures'][] = $element['embed']['url']; + } + if (array_key_exists('path', $element['file'])) { + $element['attachments'][] = $element['file']; + } + foreach ($element['attachments'] as $file) { + $item['enclosures'][] = parent::getURI() . $file['path']; + } + + $this->items[] = $item; + } + } + + public function getName() + { + $name = parent::getName(); + if (isset($this->title)) { + $name .= ' - ' . $this->title; + } + return $name; + } + + public function getURI() + { + $uri = parent::getURI() . $this->getInput('service') . '/user/' . $this->getInput('user'); + return $uri; + } +}