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
1 changed files with 36 additions and 11 deletions

View File

@ -101,28 +101,53 @@ modify the `repository` in `scalingo.json`. See https://github.com/RSS-Bridge/rs
Learn more in Learn more in
[Installation](https://rss-bridge.github.io/rss-bridge/For_Hosts/Installation.html). [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
<?php <?php
class ExecuteBridge extends BridgeAbstract class BearBlogBridge extends BridgeAbstract
{ {
const NAME = 'Execute Program Blog'; const NAME = 'BearBlog (bearblog.dev)';
public function collectData() public function collectData()
{ {
$url = 'https://www.executeprogram.com/api/pages/blog'; // We can perform css selectors on $dom
$data = json_decode(getContents($url)); $dom = getSimpleHTMLDOM('https://herman.bearblog.dev/blog/');
foreach ($data->posts as $post) { // An array of dom nodes
$this->items[] = [ $blogPosts = $dom->find('.blog-posts li');
'uri' => sprintf('https://www.executeprogram.com/blog/%s', $post->slug),
'title' => $post->title, foreach ($blogPosts as $blogPost) {
'content' => $post->body, // 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;
} }
} }
} }