Created
February 27, 2012 18:40
-
-
Save straup/1926097 to your computer and use it in GitHub Desktop.
This is a dumb sample script / reference implementation to fetch all the instagram photos belonging to a user and then upload them to their flickr account.
This file contains hidden or 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 | |
# flinstagram | |
# THIS IS PROOF-OF-CONCEPT CODE | |
# ALL THE USUAL CAVEATS APPLY. | |
# NO. REALLY. | |
# This is a dumb sample script to fetch all the instagram photos | |
# belonging to a user and then upload them to their flickr account. | |
# It assumes that you are using Flamework (or a similar derivative). | |
# Also that you've got valid auth tokens for both Flickr and Instagram | |
# which are left out of this example and as an exercise to the user. | |
# Really, it's just meant to be a reference implementation and not | |
# anything you'd actually use. Note: There is no error checking, handling | |
# or reporting. It might still have bugs. (20120227/straup) | |
# See also: | |
# https://github.com/straup/flamework | |
# https://github.com/straup/flamework-flickrapp | |
# https://github.com/straup/flamework-instagramapp | |
include("include/init.php"); | |
loadlib("instagram_users"); | |
loadlib("instagram_api"); | |
loadlib("flickr_users"); | |
loadlib("flickr_api"); | |
loadlib("http"); | |
# first get users (flamework, instagram, flickr) | |
$user = users_get_by_id(...); | |
$insta_user = instagram_users_get_by_user_id($user['id']); | |
$flickr_user = flickr_users_get_by_user_id($user['id']); | |
# collect all the photo URLs (from instagram) | |
$insta_urls = array(); | |
$method = 'users/self/media/recent'; | |
$args = array( | |
'access_token' => $insta_user['oauth_token'], | |
); | |
$moar_photos = 1; | |
while ($moar_photos){ | |
$rsp = instagram_api_call($method, $args); | |
$pg = $rsp['rsp']['pagination']; | |
foreach ($rsp['rsp']['data'] as $d){ | |
$insta_urls[] = $d['images']['standard_resolution']['url']; | |
} | |
$args['max_id'] = $pg['next_max_id']; | |
$moar_photos = ($pg['next_max_id']) ? 1 : 0; | |
} | |
# fetch the actual files (from instagram) | |
$insta_files = array(); | |
$temp_dir = sys_get_temp_dir(); | |
foreach ($insta_urls as $url){ | |
$rsp = http_get($url); | |
$fname = basename($url); | |
$path = $temp_dir . "/". $fname; | |
$fh = fopen($path, "wb"); | |
fwrite($fh, $rsp['body']); | |
fclose($fh); | |
$insta_files[] = $path; | |
} | |
# upload them (to flickr) | |
$photo_ids = array(); | |
foreach ($insta_files as $path){ | |
$args = array( | |
'auth_token' => $flickr_user['auth_token'], | |
); | |
$rsp = flickr_api_upload($path, $args); | |
$photo_ids[] = $rsp['photo_id']; | |
} | |
dumper($photo_ids); | |
# clean up | |
foreach ($insta_files as $path){ | |
unlink($path); | |
} | |
# powerful cross-network synergies! | |
exit(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment