aboutsummaryrefslogtreecommitdiff
path: root/bridges/HashnodeBridge.php
blob: 159510fb0563340be0673c93457680ab783386b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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';
	}
}