-
-
Save zarchary/b667bd09dc3f6702ceb8 to your computer and use it in GitHub Desktop.
Download all the photos on Instagram for a tag. Probably hit API limits for popular tags
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
<?php | |
/** | |
* Get All Photos for a Tag on Instagram | |
*/ | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
$everything = array(); | |
$access_token = 'need access token for Instagram API'; | |
$json = file_get_contents("https://api.instagram.com/v1/tags/lad01/media/recent?access_token={$access_token}"); | |
$result = json_decode($json, true); | |
$everything[] = $result['data']; | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// get all pages | |
while ( isset( $result['pagination']['next_url'] ) ) { | |
$json = file_get_contents($result['pagination']['next_url']); | |
$result = json_decode($json, true); | |
$everything[] = $result['data']; | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// build an array of image src, permalink, and date | |
$images = array(); | |
foreach ( $everything as $page ) { | |
foreach ( $page as $image ) { | |
if ( $image['type'] !== 'image' ) { | |
continue; | |
} | |
$images[] = array( | |
'date' => $image['created_time'], | |
'link' => $image['link'], | |
'src' => $image['images']['standard_resolution']['url'] | |
); | |
} | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// save all those files locally | |
$count = 0; | |
$total = count($images); | |
foreach ( $images as $image ) { | |
$count++; | |
// pause every once in a while to not get blocked | |
if ( $count % 10 === 0 ) { | |
$current = $total - $count; | |
echo "<p>Sleeping 10 seconds, $current images left</p>"; | |
sleep(10); | |
} | |
$current_images = scandir( '.' ); | |
$filename = basename( $image ); | |
if ( ! in_array( $filename, $current_images ) ) { | |
$data = file_get_contents( $image ); | |
if ( file_put_contents( $filename, $data ) ) { | |
echo "<p>$filename added</p>"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment