Skip to content

Commit

Permalink
[YandexZenBridge] Fix broken bridge for some channels (RSS-Bridge#4078)
Browse files Browse the repository at this point in the history
Fixes RSS-Bridge#4071.

Major changes:
- the bridge's URI changed from zen.yandex.com to dzen.ru, as the former
  redirects to the latter (perhaps the bridge's name should be changed
  as well);
- the channel's URL is now required instead of the channel's username;
- two kinds of URLs are supported, one for channels with usernames and
  one for channels with IDs in their URL;
- the channel's real name, as shown in the webpage, is now used as the
  feed title.
  • Loading branch information
llamasblade committed Apr 14, 2024
1 parent b4d397f commit 957a820
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions bridges/YandexZenBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
class YandexZenBridge extends BridgeAbstract
{
const NAME = 'YandexZen Bridge';
const URI = 'https://zen.yandex.com';
const DESCRIPTION = 'Latest posts from the specified profile.';
const URI = 'https://dzen.ru';
const DESCRIPTION = 'Latest posts from the specified channel.';
const MAINTAINER = 'llamasblade';
const PARAMETERS = [
[
'username' => [
'name' => 'Username',
'channelURL' => [
'name' => 'Channel URL',
'type' => 'text',
'required' => true,
'title' => 'The account\'s username, found in its URL',
'exampleValue' => 'dream_faity_diy',
'title' => 'The channel\'s URL',
'exampleValue' => 'https://dzen.ru/dream_faity_diy',
],
'limit' => [
'name' => 'Limit',
Expand All @@ -27,14 +27,41 @@ class YandexZenBridge extends BridgeAbstract
];

# credit: https://github.com/teromene see #1032
const _API_URL = 'https://zen.yandex.ru/api/v3/launcher/more?channel_name=';
const _BASE_API_URL_WITH_CHANNEL_NAME = 'https://dzen.ru/api/v3/launcher/more?channel_name=';
const _BASE_API_URL_WITH_CHANNEL_ID = 'https://dzen.ru/api/v3/launcher/more?channel_id=';

const _ACCOUNT_URL_WITH_CHANNEL_ID_REGEX = '#^https?://dzen\.ru/id/(?<channelID>[a-z0-9]{24})#';
const _ACCOUNT_URL_WITH_CHANNEL_NAME_REGEX = '#^https?://dzen\.ru/(?<channelName>[\w\.]+)#';

private $channelRealName = null; # as shown in the webpage, not in the URL


public function collectData()
{
$profile_json = json_decode(getContents($this->getAPIUrl()));
$channelURL = $this->getInput('channelURL');

if (preg_match(self::_ACCOUNT_URL_WITH_CHANNEL_ID_REGEX, $channelURL, $matches)) {
$channelID = $matches['channelID'];
$channelAPIURL = self::_BASE_API_URL_WITH_CHANNEL_ID . $channelID;
} elseif (preg_match(self::_ACCOUNT_URL_WITH_CHANNEL_NAME_REGEX, $channelURL, $matches)) {
$channelName = $matches['channelName'];
$channelAPIURL = self::_BASE_API_URL_WITH_CHANNEL_NAME . $channelName;
} else {
returnClientError(<<<EOT
Invalid channel URL provided.
The channel\'s URL must be in one of these two forms:
- https://dzen.ru/dream_faity_diy
- https://dzen.ru/id/5ad7777f1aa80ce576015250
EOT);
}

$APIResponse = json_decode(getContents($channelAPIURL));

$this->channelRealName = $APIResponse->header->title;

$limit = $this->getInput('limit');

foreach (array_slice($profile_json->items, 0, $limit) as $post) {
foreach (array_slice($APIResponse->items, 0, $limit) as $post) {
$item = [];

$item['uri'] = $post->share_link;
Expand All @@ -56,21 +83,19 @@ public function collectData()
}
}

private function getAPIUrl()
{
return self::_API_URL . $this->getInput('username');
}

public function getURI()
{
return self::URI . '/' . $this->getInput('username');
if (is_null($this->getInput('channelURL'))) {
return parent::getURI();
}
return $this->getInput('channelURL');
}

public function getName()
{
if (is_null($this->getInput('username'))) {
if (is_null($this->channelRealName)) {
return parent::getName();
}
return $this->getInput('username') . '\'s latest zen.yandex posts';
return $this->channelRealName . '\'s latest zen.yandex posts';
}
}

0 comments on commit 957a820

Please sign in to comment.