Skip to content

Instantly share code, notes, and snippets.

@alispx
Created June 30, 2015 02:02
Show Gist options
  • Save alispx/6b264462b95dfe4c98e6 to your computer and use it in GitHub Desktop.
Save alispx/6b264462b95dfe4c98e6 to your computer and use it in GitHub Desktop.
Instagram scrapper
// based on https://gist.github.com/cosmocatalano/4544576
function scrape_instagram( $username, $slice = 9 ) {
$username = strtolower( $username );
if ( false === ( $instagram = get_transient( 'instagram-media-new-'.sanitize_title_with_dashes( $username ) ) ) ) {
$remote = wp_remote_get( 'http://instagram.com/'.trim( $username ) );
if ( is_wp_error( $remote ) )
return new WP_Error( 'site_down', __( 'Unable to communicate with Instagram.', 'tokoo' ) );
if ( 200 != wp_remote_retrieve_response_code( $remote ) )
return new WP_Error( 'invalid_response', __( 'Instagram did not return a 200.', 'tokoo' ) );
$shards = explode( 'window._sharedData = ', $remote['body'] );
$insta_json = explode( ';</script>', $shards[1] );
$insta_array = json_decode( $insta_json[0], TRUE );
if ( ! $insta_array )
return new WP_Error( 'bad_json', __( 'Instagram has returned invalid data.', 'tokoo' ) );
// old style
if ( isset( $insta_array['entry_data']['UserProfile'][0]['userMedia'] ) ) {
$images = $insta_array['entry_data']['UserProfile'][0]['userMedia'];
$type = 'old';
// new style
} else if ( isset( $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'] ) ) {
$images = $insta_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'];
$type = 'new';
} else {
return new WP_Error( 'bad_josn_2', __( 'Instagram has returned invalid data.', 'tokoo' ) );
}
if ( !is_array( $images ) )
return new WP_Error( 'bad_array', __( 'Instagram has returned invalid data.', 'tokoo' ) );
$instagram = array();
switch ( $type ) {
case 'old':
foreach ( $images as $image ) {
if ( $image['user']['username'] == $username ) {
$image['link'] = preg_replace( "/^http:/i", "", $image['link'] );
$image['images']['thumbnail'] = preg_replace( "/^http:/i", "", $image['images']['thumbnail'] );
$image['images']['standard_resolution'] = preg_replace( "/^http:/i", "", $image['images']['standard_resolution'] );
$image['images']['low_resolution'] = preg_replace( "/^http:/i", "", $image['images']['low_resolution'] );
$instagram[] = array(
'description' => $image['caption']['text'],
'link' => $image['link'],
'time' => $image['created_time'],
'comments' => $image['comments']['count'],
'likes' => $image['likes']['count'],
'thumbnail' => $image['images']['thumbnail'],
'large' => $image['images']['standard_resolution'],
'small' => $image['images']['low_resolution'],
'type' => $image['type']
);
}
}
break;
default:
foreach ( $images as $image ) {
$image['display_src'] = preg_replace( "/^http:/i", "", $image['display_src'] );
if ( $image['is_video'] == true ) {
$type = 'video';
} else {
$type = 'image';
}
$instagram[] = array(
'description' => __( 'Instagram Image', 'tokoo' ),
'link' => '//instagram.com/p/' . $image['code'],
'time' => $image['date'],
'comments' => $image['comments']['count'],
'likes' => $image['likes']['count'],
'thumbnail' => $image['display_src'],
'type' => $type
);
}
break;
}
// do not set an empty transient - should help catch private or empty accounts
if ( ! empty( $instagram ) ) {
$instagram = base64_encode( serialize( $instagram ) );
set_transient( 'instagram-media-new-'.sanitize_title_with_dashes( $username ), $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS*2 ) );
}
}
if ( ! empty( $instagram ) ) {
$instagram = unserialize( base64_decode( $instagram ) );
return array_slice( $instagram, 0, $slice );
} else {
return new WP_Error( 'no_images', __( 'Instagram did not return any images.', 'tokoo' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment