docs: add a better bridge example in readme (#3057)

This commit is contained in:
Dag 2022-09-21 22:20:51 +02:00 committed by GitHub
parent 8d8fe66aab
commit aabbeef743
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -101,28 +101,53 @@ modify the `repository` in `scalingo.json`. See https://github.com/RSS-Bridge/rs
Learn more in
[Installation](https://rss-bridge.github.io/rss-bridge/For_Hosts/Installation.html).
### Create a bridge
### Create a new bridge from scratch
Create the new bridge in e.g. `bridges/ExecuteBridge.php`:
Create the new bridge in e.g. `bridges/BearBlogBridge.php`:
```php
<?php
class ExecuteBridge extends BridgeAbstract
class BearBlogBridge extends BridgeAbstract
{
const NAME = 'Execute Program Blog';
const NAME = 'BearBlog (bearblog.dev)';
public function collectData()
{
$url = 'https://www.executeprogram.com/api/pages/blog';
$data = json_decode(getContents($url));
// We can perform css selectors on $dom
$dom = getSimpleHTMLDOM('https://herman.bearblog.dev/blog/');
foreach ($data->posts as $post) {
$this->items[] = [
'uri' => sprintf('https://www.executeprogram.com/blog/%s', $post->slug),
'title' => $post->title,
'content' => $post->body,
// An array of dom nodes
$blogPosts = $dom->find('.blog-posts li');
foreach ($blogPosts as $blogPost) {
// Select the anchor at index 0 (the first anchor found)
$a = $blogPost->find('a', 0);
// Select the inner text of the anchor
$title = $a->innertext;
// Select the href attribute of the anchor
$url = $a->href;
// Select the <time> tag
$time = $blogPost->find('time', 0);
// Create a \DateTime object from the datetime attribute
$createdAt = date_create_from_format('Y-m-d', $time->datetime);
$item = [
'title' => $title,
'author' => 'Herman',
// Prepend the url because $url is a relative path
'uri' => 'https://herman.bearblog.dev' . $url,
// Grab the unix timestamp
'timestamp' => $createdAt->getTimestamp(),
];
// Add the item to the list of items
$this->items[] = $item;
}
}
}