2018-09-22 19:57:07 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
class DevToBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const CONTEXT_BY_TAG = 'By tag';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
const NAME = 'dev.to Bridge';
|
|
|
|
const URI = 'https://dev.to';
|
|
|
|
const DESCRIPTION = 'Returns feeds for tags';
|
|
|
|
const MAINTAINER = 'logmanoriginal';
|
|
|
|
const CACHE_TIMEOUT = 10800; // 15 min.
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
const PARAMETERS = [
|
|
|
|
self::CONTEXT_BY_TAG => [
|
|
|
|
'tag' => [
|
|
|
|
'name' => 'Tag',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'title' => 'Insert your tag',
|
|
|
|
'exampleValue' => 'python'
|
|
|
|
],
|
|
|
|
'full' => [
|
|
|
|
'name' => 'Full article',
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'required' => false,
|
2019-01-05 14:29:26 +03:00
|
|
|
'title' => 'Enable to receive the full article for each item'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2018-09-22 19:57:07 +03:00
|
|
|
];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
public function getURI()
|
|
|
|
{
|
|
|
|
switch ($this->queriedContext) {
|
|
|
|
case self::CONTEXT_BY_TAG:
|
|
|
|
if ($tag = $this->getInput('tag')) {
|
|
|
|
return static::URI . '/t/' . urlencode($tag);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
return parent::getURI();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
public function getIcon()
|
|
|
|
{
|
|
|
|
return 'https://practicaldev-herokuapp-com.freetls.fastly.net/assets/
|
|
|
|
apple-icon-5c6fa9f2bce280428589c6195b7f1924206a53b782b371cfe2d02da932c8c173.png';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function collectData()
|
|
|
|
{
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = getSimpleHTMLDOMCached($this->getURI());
|
2018-09-22 19:57:07 +03:00
|
|
|
|
|
|
|
$html = defaultLinkTo($html, static::URI);
|
|
|
|
|
2020-08-12 21:07:53 +03:00
|
|
|
$articles = $html->find('div.crayons-story')
|
2018-09-22 19:57:07 +03:00
|
|
|
or returnServerError('Could not find articles!');
|
|
|
|
|
|
|
|
foreach ($articles as $article) {
|
|
|
|
$item = [];
|
|
|
|
|
|
|
|
$item['uri'] = $article->find('a[id*=article-link]', 0)->href;
|
2020-08-12 21:07:53 +03:00
|
|
|
$item['title'] = $article->find('h2 > a', 0)->plaintext;
|
2018-09-22 19:57:07 +03:00
|
|
|
|
2020-08-12 21:07:53 +03:00
|
|
|
$item['timestamp'] = $article->find('time', 0)->datetime;
|
|
|
|
$item['author'] = $article->find('a.crayons-story__secondary.fw-medium', 0)->plaintext;
|
2018-09-22 19:57:07 +03:00
|
|
|
|
|
|
|
// Profile image
|
|
|
|
$item['enclosures'] = [$article->find('img', 0)->src];
|
|
|
|
|
|
|
|
if ($this->getInput('full')) {
|
|
|
|
$fullArticle = $this->getFullArticle($item['uri']);
|
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<p>{$fullArticle}</p>
|
|
|
|
EOD;
|
|
|
|
} else {
|
|
|
|
$item['content'] = <<<EOD
|
|
|
|
<img src="{$item['enclosures'][0]}" alt="{$item['author']}">
|
|
|
|
<p>{$item['title']}</p>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:07:53 +03:00
|
|
|
// categories
|
|
|
|
foreach ($article->find('a.crayons-tag') as $tag) {
|
|
|
|
$item['categories'][] = str_replace('#', '', $tag->plaintext);
|
|
|
|
}
|
2018-09-22 19:57:07 +03:00
|
|
|
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:56:03 +03:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
if (!is_null($this->getInput('tag'))) {
|
|
|
|
return ucfirst($this->getInput('tag')) . ' - dev.to';
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getName();
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
private function getFullArticle($url)
|
|
|
|
{
|
2022-01-02 12:36:09 +03:00
|
|
|
$html = getSimpleHTMLDOMCached($url);
|
2018-09-22 19:57:07 +03:00
|
|
|
|
|
|
|
$html = defaultLinkTo($html, static::URI);
|
|
|
|
|
2020-08-12 21:07:53 +03:00
|
|
|
if ($html->find('div.crayons-article__cover', 0)) {
|
|
|
|
return $html->find('div.crayons-article__cover', 0) . $html->find('[id="article-body"]', 0);
|
|
|
|
}
|
|
|
|
|
2018-09-22 19:57:07 +03:00
|
|
|
return $html->find('[id="article-body"]', 0);
|
|
|
|
}
|
|
|
|
}
|