feat: new bridge pokemonnews (#3042)

* feat: new bridge pokemonnews

fix #3040

* fix
This commit is contained in:
Dag 2022-09-17 01:29:40 +02:00 committed by GitHub
parent 11220ef373
commit 55cc74c816
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
final class PokemonNewsBridge extends BridgeAbstract
{
const NAME = 'Pokemon.com news';
const URI = 'https://www.pokemon.com/us/pokemon-news';
const DESCRIPTION = 'Fetches the latest news from pokemon.com';
const MAINTAINER = 'dvikan';
public function collectData()
{
// todo: parse json api instead: https://www.pokemon.com/api/1/us/news/get-news.json
$url = 'https://www.pokemon.com/us/pokemon-news';
$dom = getSimpleHTMLDOM($url);
foreach ($dom->find('.news-list ul li') as $item) {
$title = $item->find('h3', 0)->plaintext;
$description = $item->find('p.hidden-mobile', 0);
$dateString = $item->find('p.date', 0)->plaintext;
// e.g. September 15, 2022
$createdAt = date_create_from_format('F d, Y', $dateString);
// todo:
$tagsString = $item->find('p.tags', 0)->plaintext;
$path = $item->find('a', 0)->href;
$imagePath = $item->find('img', 0)->src;
$tags = explode('&', $tagsString);
$tags = array_map('trim', $tags);
$this->items[] = [
'title' => $title,
'uri' => sprintf('https://www.pokemon.com%s', $path),
'timestamp' => $createdAt ? $createdAt->getTimestamp() : time(),
'categories' => $tags,
'content' => sprintf(
'<img src="https://pokemon.com%s"><br><br>%s',
$imagePath,
$description ? $description->plaintext : ''
),
];
}
}
}