Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MangaDexBridge] Add new bridge #2583

Merged
merged 5 commits into from
Apr 1, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions bridges/MangaDexBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
class MangaDexBridge extends BridgeAbstract {
const MAINTAINER = 'Yaman Qalieh';
const NAME = 'MangaDex Bridge';
const URI = 'https://mangadex.org/';
const API_ROOT = 'https://api.mangadex.org/';
const DESCRIPTION = 'Returns MangaDex items using the API';
const CACHE_TIMEOUT = 0;
yamanq marked this conversation as resolved.
Show resolved Hide resolved

const PARAMETERS = array(
'global' => array(
'limit' => array(
'name' => 'Item Limit',
'type' => 'number',
'defaultValue' => 10,
'required' => true
),
'lang' => array(
'name' => 'Chapter Languages',
'title' => 'two-letter language codes, separated by just ","',
yamanq marked this conversation as resolved.
Show resolved Hide resolved
'defaultValue' => 'en',
'required' => false
),
),
'Title Chapters' => array(
'url' => array(
'name' => 'URL to title page',
'exampleValue' => 'https://mangadex.org/title/f9c33607-9180-4ba6-b85c-e4b5faee7192/official-test-manga',
'required' => true
)
)
);

const TITLE_REGEX = '#title/(?<uuid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})#';

protected $feedName = '';

protected function buildArrayQuery($name, $array) {
$query = '';
foreach($array as $item) {
$query .= '&' . $name . '=' . $item;
}
return $query;
}

protected function getAPI() {
$params = array(
'limit' => $this->getInput('limit')
);

$array_params = array();
if (!empty($this->getInput('lang'))) {
$array_params['translatedLanguage[]'] = explode(',', $this->getInput('lang'));
}

switch($this->queriedContext) {
case 'Title Chapters':
preg_match(self::TITLE_REGEX, $this->getInput('url'), $matches)
or returnClientError('Invalid URL Parameter');
$params['order[updatedAt]'] = 'desc';
$array_params['includes[]'] = array('manga', 'scanlation_group', 'user');
$uri = self::API_ROOT . 'manga/' . $matches['uuid'] . '/feed';
break;
default:
returnServerError('Unimplemented Context (getAPI)');
}

$uri .= '?' . http_build_query($params);

// Arrays are passed as repeated keys to MangaDex
// This cannot be handled by http_build_query
foreach($array_params as $name => $array_param) {
$uri .= $this->buildArrayQuery($name, $array_param);
}

return $uri;

}

public function getName() {
switch($this->queriedContext) {
case 'Title Chapters':
return $this->feedName . ' Chapters';
default:
return parent::getName();
}
}

public function collectData() {
$api_uri = $this->getApi();
$header = array(
'Content-Type: application/json'
);
$content = json_decode(getContents($api_uri, $header), true);
if ($content['result'] == 'ok') {
$content = $content['data'];
} else {
returnServerError('Could not retrieve API results');
}

switch($this->queriedContext) {
case 'Title Chapters':
$this->getChapters($content);
break;
default:
returnServerError('Unimplemented Context (collectData)');
}
}

protected function getChapters($content) {
foreach($content as $chapter) {
$item = array();
$item['uid'] = $chapter['id'];
$item['uri'] = self::URI . 'chapter/' . $chapter['id'];

// Preceding space accounts for Manga title added later
$item['title'] = ' Chapter ' . $chapter['attributes']['chapter'];
if (!empty($chapter['attributes']['title'])) {
$item['title'] .= ' - ' . $chapter['attributes']['title'];
}
$item['title'] .= ' [' . $chapter['attributes']['translatedLanguage'] . ']';

$item['timestamp'] = $chapter['attributes']['updatedAt'];

$groups = array();
$users = array();
foreach($chapter['relationships'] as $rel) {
switch($rel['type']) {
case 'scanlation_group':
$groups[] = $rel['attributes']['name'];
break;
case 'manga':
if (empty($this->feedName)) {
$this->feedName = reset($rel['attributes']['title']);
}
$item['title'] = reset($rel['attributes']['title']) . $item['title'];
break;
case 'user':
if (isset($item['author'])) {
$users[] = $rel['attributes']['username'];
} else {
$item['author'] = $rel['attributes']['username'];
}
break;
}
}
$item['content'] = 'Groups: ' .
(empty($groups) ? 'No Group' : implode(', ', $groups));
if (!empty($users)) {
$item['content'] .= '<br>Other Users: ' . implode(', ', $users);
}

$this->items[] = $item;
}
}
}