diff --git a/README.md b/README.md new file mode 100644 index 0000000..f1529af --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +Instagrizzle - An Instagram Plugin for Statamic +=============================================== + +Fetch media from a public Instagram feed, without the need for the API. Yeah, it's a down and dirty scraping plugin. Enjoy! + +## The Tag + +``` +{{ instagrizzle }} + +{{ /instagrizzle }} +``` + +## The Parameters + +### Username `username` + +Instagram username of the feed you want to pull. + +``` +username="jackmcdade" +``` + +### Limit `limit` + +Limit the items returned. +``` +limit="5" +``` + +### Offset `offset` + +Offset the items returned. +``` +offset="1" +``` + +## Debugger + +There are a lot of variables to access from the Instagram response object. You can use `{{ instagrizzle:debug }}` to explore the data available to you. + +## Config + +There are currently two config options. + +### Username `username` + +Set the default username for the plugin across your whole site. + +```yaml +username: jackmcdade +``` + +### Cache Length `cache_length` + +Set how many seconds you would like to cache the Instagram response object. Default is 3600 (1 hour). \ No newline at end of file diff --git a/_add-ons/instagrizzle/default.yaml b/_add-ons/instagrizzle/default.yaml new file mode 100644 index 0000000..06ae7f5 --- /dev/null +++ b/_add-ons/instagrizzle/default.yaml @@ -0,0 +1,7 @@ +# Instagrizzle Default Config + +# Instagram username to fetch +username: colerise + +# Number of seconds to cache the instagram media +cache_length: 3600 diff --git a/_add-ons/instagrizzle/pi.instagrizzle.php b/_add-ons/instagrizzle/pi.instagrizzle.php new file mode 100644 index 0000000..8c8bb49 --- /dev/null +++ b/_add-ons/instagrizzle/pi.instagrizzle.php @@ -0,0 +1,40 @@ +fetch('username'); + $limit = $this->fetch('limit', false, 'is_numeric'); + $offset = $this->fetch('offset', 0, 'is_numeric'); + + $media = $this->tasks->getMedia($username, $limit, $offset); + + return Parse::tagLoop($this->content, $media); + } + + /** + * Dump and die the available data + * @return array + */ + public function debug() + { + $media = $this->tasks->getMedia($this->fetch('username')); + + rd($media); + } +} \ No newline at end of file diff --git a/_add-ons/instagrizzle/tasks.instagrizzle.php b/_add-ons/instagrizzle/tasks.instagrizzle.php new file mode 100644 index 0000000..145424b --- /dev/null +++ b/_add-ons/instagrizzle/tasks.instagrizzle.php @@ -0,0 +1,57 @@ +cache->purgeOlderThan($this->fetchConfig('cache_length')); + + // fetch from cache if available + $media = $this->cache->getYAML($username); + + // no cache. sad trombone. let's scrape new data. + if ( ! $media) { + + $media = $this->scrape($username); + + // store in cache + $this->cache->putYAML($username, $media); + + } + + // 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 + public 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; + } +} \ No newline at end of file diff --git a/_config/add-ons/instagrizzle/instagrizzle.yaml b/_config/add-ons/instagrizzle/instagrizzle.yaml new file mode 100755 index 0000000..8172cc3 --- /dev/null +++ b/_config/add-ons/instagrizzle/instagrizzle.yaml @@ -0,0 +1,7 @@ +# Instagrizzle Config + +# Instagram username to fetch +username: colerise + +# Number of seconds to cache the instagram media +cache_length: 3600