mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2024-11-25 19:06:23 +03:00
docs: add a better bridge example in readme (#3057)
This commit is contained in:
parent
8d8fe66aab
commit
aabbeef743
1 changed files with 36 additions and 11 deletions
47
README.md
47
README.md
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue