Skip to content

Commit

Permalink
feat: add new bridge HashnodeBridge (#2231)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Mar 24, 2022
1 parent bb06826 commit 50bab07
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions bridges/HashnodeBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

class HashnodeBridge extends BridgeAbstract {

const MAINTAINER = 'liamka';
const NAME = 'Hashnode';
const URI = 'https://hashnode.com';
const CACHE_TIMEOUT = 3600; // 1hr
const DESCRIPTION = 'See trending or latest posts in Hashnode community.';
const LATEST_POSTS = 'https://hashnode.com/api/stories/recent?page=';

public function collectData(){
$this->items = [];
for ($i = 0; $i < 5; $i++) {
$url = self::LATEST_POSTS . $i;
$content = getContents($url);
$array = json_decode($content, true);

if($array['posts'] != null) {
foreach($array['posts'] as $post) {
$item = [];
$item['title'] = $post['title'];
$item['content'] = nl2br(htmlspecialchars($post['brief']));
$item['timestamp'] = $post['dateAdded'];
if($post['partOfPublication'] === true) {
$item['uri'] = sprintf(
'https://%s.hashnode.dev/%s',
$post['publication']['username'],
$post['slug']
);
} else {
$item['uri'] = sprintf('https://hashnode.com/post/%s', $post['slug']);
}
if(!isset($item['uri'])) {
continue;
}
$this->items[] = $item;
}
}
}
}

public function getName(){
return self::NAME . ': Recent posts';
}
}

0 comments on commit 50bab07

Please sign in to comment.