-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 09ad2be
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }} | ||
<img src="{{ images:high_resolution:url }}"> | ||
{{ /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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Instagrizzle Default Config | ||
|
||
# Instagram username to fetch | ||
username: colerise | ||
|
||
# Number of seconds to cache the instagram media | ||
cache_length: 3600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** | ||
* Instagrizzle | ||
* Get media from public Instagram accounts | ||
* | ||
* @author Jack McDade | ||
* @version 1.0 | ||
* @link http://github.com/jackmcdade/instagrizzle | ||
*/ | ||
|
||
class Plugin_instagrizzle extends Plugin | ||
{ | ||
|
||
/** | ||
* Get Instagram media | ||
* @return array parsed response from a public Instagram feed | ||
*/ | ||
public function index() | ||
{ | ||
$username = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
class Tasks_instagrizzle extends Tasks | ||
{ | ||
/** | ||
* Get and filter Instagram media from a public account | ||
* @param string $username Instagram username | ||
* @param int $limit limit the number of items returned | ||
* @param integer $offset offset the items returned | ||
* @return array the items | ||
*/ | ||
public function getMedia($username, $limit = false, $offset = 0) | ||
{ | ||
// clear old cache | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Instagrizzle Config | ||
|
||
# Instagram username to fetch | ||
username: colerise | ||
|
||
# Number of seconds to cache the instagram media | ||
cache_length: 3600 |