-
Notifications
You must be signed in to change notification settings - Fork 12
/
instagram.php
144 lines (124 loc) · 4.7 KB
/
instagram.php
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Page\Page;
use Grav\Common\GPM\Response;
class InstagramPlugin extends Plugin
{
private $template_html = 'partials/instagram.html.twig';
private $template_vars = [];
private $cache;
const HOUR_IN_SECONDS = 3600;
/**
* Return a list of subscribed events.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize configuration.
*/
public function onPluginsInitialized()
{
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
}
/**
* Add Twig Extensions.
*/
public function onTwigInitialized()
{
$this->grav['twig']->twig->addFunction(new \Twig_SimpleFunction('instagram_feed', [$this, 'getFeed']));
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* @return array
*/
public function getFeed($params = [])
{
/** @var Page $page */
$page = $this->grav['page'];
/** @var Twig $twig */
$twig = $this->grav['twig'];
/** @var Data $config */
$config = $this->mergeConfig($page, TRUE);
// Autoload composer components
require __DIR__ . '/vendor/autoload.php';
// Set up cache settings
$cache_config = array(
"storage" => "files",
"default_chmod" => 0777,
"fallback" => "files",
"securityKey" => "auto",
"htaccess" => true,
"path" => __DIR__ . "/cache"
);
// Init the cache engine
$this->cache = phpFastCache("files", $cache_config);
// Generate API url
$url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $config->get('feed_parameters.access_token').'&count=' . $config->get('feed_parameters.count');
// Get the cached results if available
$results = $this->cache->get($url);
// Get the results from the live API, cached version not found
if ($results === null) {
$results = Response::get($url);
// Cache the results
$this->cache->set($url, $results, InstagramPlugin::HOUR_IN_SECONDS * $config->get('feed_parameters.cache_time')); // Convert hours to seconds
}
$this->parseResponse($results);
$this->template_vars = [
'user_id' => $config->get('feed_parameters.user_id'),
'client_id' => $config->get('feed_parameters.client_id'),
'feed' => $this->feeds,
'count' => $config->get('feed_parameters.count'),
'params' => $params
];
$output = $this->grav['twig']->twig()->render($this->template_html, $this->template_vars);
return $output;
}
private function addFeed($result) {
foreach ($result as $key => $val) {
if (!isset($this->feeds[$key])) {
$this->feeds[$key] = $val;
}
}
krsort($this->feeds);
}
private function parseResponse($json) {
$r = array();
$content = json_decode($json, true);
if (count($content['data'])) {
foreach ($content['data'] as $key => $val) {
$created_at = $val['created_time'];
$r[$created_at]['time'] = $created_at;
$r[$created_at]['text'] = $val['caption']['text'];
$r[$created_at]['image'] = $val['images']['standard_resolution']['url'];
$r[$created_at]['image_width'] = $val['images']['standard_resolution']['width'];
$r[$created_at]['thumb'] = $val['images']['low_resolution']['url'];
$r[$created_at]['thumb_width'] = $val['images']['low_resolution']['width'];
$r[$created_at]['micro'] = $val['images']['thumbnail']['url'];
$r[$created_at]['micro_width'] = $val['images']['thumbnail']['width'];
$r[$created_at]['user'] = $val['user']['full_name'];
$r[$created_at]['link'] = $val['link'];
$r[$created_at]['comments'] = $val['comments']['count'];
$r[$created_at]['likes'] = $val['likes']['count'];
$r[$created_at]['type'] = $val['type'];
}
$this->addFeed($r);
}
}
}