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

[OsmAndBlogBridge] Add new bridge #973

Merged
merged 1 commit into from
Dec 26, 2018
Merged
Changes from all commits
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
67 changes: 67 additions & 0 deletions bridges/OsmAndBlogBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
class OsmAndBlogBridge extends BridgeAbstract {
const NAME = 'OsmAnd Blog';
const URI = 'https://osmand.net/';
const DESCRIPTION = 'Get the latest news from OsmAnd.net';
const MAINTAINER = 'fulmeek';


public function collectData() {
$html = getSimpleHTMLDOM(self::URI . 'blog')
or returnServerError('Could not load content');

foreach($html->find('div.article') as $element) {
$item = array();

$objTitle = $element->find('h1', 0);
if (!$objTitle)
$objTitle = $element->find('h2', 0);
if (!$objTitle)
$objTitle = $element->find('h3', 0);
if ($objTitle)
$item['title'] = $objTitle->plaintext;

$objDate = $element->find('meta[pubdate]', 0);
if ($objDate) {
$item['timestamp'] = strtotime($objDate->pubdate);
} else {
$objDate = $element->find('.date', 0);
if ($objDate)
$item['timestamp'] = strtotime($objDate->plaintext);
}

$this->cleanupContent($element, $objTitle, $objDate, $element->find('.date', 0));
$item['content'] = $element->innertext;

$objLink = $html->find('.articlelinklist a', 0);
if ($objLink) {
$item['uri'] = $this->filterURL($objLink->href);
} else {
$item['uri'] = 'urn:sha1:' . hash('sha1', $item['content']);
}

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


private function filterURL($url) {
if (strpos($url, '://') === false)
return self::URI . ltrim($url, '/');
return $url;
}


private function cleanupContent($content, ...$removeItems) {
foreach ($removeItems as $obj) {
if ($obj) $obj->outertext = '';
}
foreach ($content->find('img') as $obj) {
$obj->src = $this->filterURL($obj->src);
}
foreach ($content->find('a') as $obj) {
$obj->href = $this->filterURL($obj->href);
$obj->target = '_blank';
}
}
}