Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmcdade committed Sep 30, 2014
0 parents commit 09ad2be
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
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).
7 changes: 7 additions & 0 deletions _add-ons/instagrizzle/default.yaml
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
40 changes: 40 additions & 0 deletions _add-ons/instagrizzle/pi.instagrizzle.php
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);
}
}
57 changes: 57 additions & 0 deletions _add-ons/instagrizzle/tasks.instagrizzle.php
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;
}
}
7 changes: 7 additions & 0 deletions _config/add-ons/instagrizzle/instagrizzle.yaml
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

0 comments on commit 09ad2be

Please sign in to comment.