-
Notifications
You must be signed in to change notification settings - Fork 2
/
social-counters.php
106 lines (85 loc) · 3.01 KB
/
social-counters.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
<?php
namespace Grav\Plugin;
use Grav\Common\HTTP\Client;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use Symfony\Component\DomCrawler\Crawler;
/**
* Class SocialCountersPlugin
* @package Grav\Plugin
*/
class SocialCountersPlugin extends Plugin
{
protected $active = false;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
/**
* Make form accessible from twig.
*/
public function onTwigSiteVariables()
{
require_once $this->grav['locator']->findResource('plugins://github/vendor/autoload.php');
$config = $this->config->get('plugins.social-counters');
$cache = $this->grav['cache'];
$cache_id = md5('social-counters'.$cache->getKey());
$github = $cache->fetch($cache_id . '-github');
$twitter = $cache->fetch($cache_id . '-twitter');
// Github not found in cache, try again
if ($github === false) {
$client = new \Github\Client();
$repo = $client->api('repo');
try {
$github['stars'] = $repo->show($config['github']['user'], $config['github']['repo'])['stargazers_count'];
$cache->save($cache_id . '-github', $github, $config['cache_timeout']);
} catch(\Exception $e) {
$github['error'] = $e->getMessage();
}
}
// Twitter not found in cache, try again
if ($twitter === false) {
$followers = null;
$client = Client::getClient();
try {
// $response = $client->request('GET', 'https://livecounts.io/twitter-live-follower-counter/' . $config['twitter']['user']);
// $status = $response->getStatusCode();
// if ($status === 200) {
// $body = $response->getContent();
//
// $crawler = new Crawler($body);
// $follower_text = $crawler->filterXpath('//div[@class="odometer"]')->text();
// $followers = intval($follower_text);
// }
$followers = 5772;
if (is_int($followers)) {
$twitter['followers'] = $followers;
$cache->save($cache_id . '-twitter', $twitter, $config['cache_timeout']);
} else {
$twitter['error'] = 'Could not retrieve Twitter followers';
}
} catch (\Exception $e) {
$twitter['error'] = $e->getMessage();
}
}
$this->grav['twig']->twig_vars['social_counters'] = ['github' => $github, 'twitter' => $twitter];
}
}