This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi.instagrizzle.php
111 lines (84 loc) · 2.62 KB
/
pi.instagrizzle.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$plugin_info = array(
'pi_name' => 'Instagrizzle',
'pi_version' => '1.0',
'pi_author' => 'Jason Varga',
'pi_author_url' => 'http://pixelfear.com',
'pi_description'=> 'An EE port of Jack McDade\'s Statamic Instagram Plugin',
'pi_usage' => Instagrizzle::usage()
);
class Instagrizzle
{
public function __construct()
{
$this->EE =& get_instance();
$username = $this->EE->TMPL->fetch_param('username');
$limit = (int) $this->EE->TMPL->fetch_param('limit');
$offset = (int) $this->EE->TMPL->fetch_param('offset');
$debug = (bool) preg_match('/1|on|yes|y|true/i', $this->EE->TMPL->fetch_param('debug'));
$media = $this->getMedia($username, $limit, $offset);
$vars = array();
foreach ($media as $val) {
$vars[] = $this->flattenArray($val);
}
if ($debug) {
die(var_dump($vars));
}
$this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
}
private function getMedia($username, $limit = false, $offset = 0)
{
$media = $this->scrape($username);
// offset results
if ($offset > 0) {
$media = array_splice($media, $offset);
}
// limit results
if ($limit) {
$media = array_slice($media, 0, $limit);
}
return $media;
}
// Quick and dirty Instagram scraper
// Based on https://gist.github.com/cosmocatalano/4544576
private function scrape($username)
{
$source = file_get_contents('http://instagram.com/' . $username);
$shards = explode('window._sharedData = ', $source);
$json_response = explode('"}};', $shards[1]);
$response_array = json_decode($json_response[0].'"}}', TRUE);
$media = $response_array['entry_data']['UserProfile'][0]['userMedia'];
return $media;
}
// Change multidimensional array to dot notation
// Based on http://stackoverflow.com/a/10424516/1569621
private function flattenArray($array)
{
$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$result = array();
foreach ($ritit as $leafValue) {
$keys = array();
foreach (range(0, $ritit->getDepth()) as $depth) {
$keys[] = $ritit->getSubIterator($depth)->key();
}
$result[ join(':', $keys) ] = $leafValue;
}
return $result;
}
// ----------------------------------------------------------------
/**
* Plugin Usage
*/
public static function usage()
{
ob_start();
?>
View docs on Github: https://github.com/pixelfear/EE-Instagrizzle
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
/* End of file pi.instagrizzle.php */
/* Location: /system/expressionengine/third_party/instagrizzle/pi.instagrizzle.php */