Created
July 9, 2012 04:49
-
-
Save afeld/3074251 to your computer and use it in GitHub Desktop.
Jux: import Instagram photos for a user
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
user = User.find_by_username('catturner') | |
user_id = user.id | |
next_max_id = nil | |
quark_ids = [] | |
picture_ids = [] | |
begin | |
# instagram limits to 60 | |
query = {access_token: user.instagram_token.token, count: 60} | |
query[:max_id] = next_max_id if next_max_id | |
results = HTTParty.get 'https://api.instagram.com/v1/users/self/media/recent', format: :json, query: query | |
results = Hashie::Mash.new results | |
results.data.reverse_each do |result| | |
user_data = result.user | |
loc_data = result.location || Hashie::Mash.new | |
created_at = Time.at(result.created_time.to_i) | |
# create the Picture | |
pic = Picture.get user_id, url: result.images.standard_resolution.url | |
pic.update_attributes!( | |
source: 'instagram_auto', | |
filter: result.filter, | |
location: { | |
lat: loc_data.latitude, | |
lng: loc_data.longitude | |
}, | |
service: 'instagram', | |
service_creator_displayname: user_data.full_name, | |
service_creator_id: user_data.id, | |
service_creator_username: user_data.username, | |
service_media_id: result.id, | |
service_media_url: result.link, | |
service_uploaded_at: created_at, | |
title: result.caption.try(:text) || '' | |
) | |
# only create a quark if the image hasn't been used by this user | |
if pic.quarks.empty? | |
# create the Quark | |
quark = Photo.create! caption: pic.title, user_id: user_id | |
# assign default picture after create, because it was getting overwritten by copied styling | |
quark.background_picture = pic | |
# quark.picture_size = 'frame' | |
quark.sort_time = created_at | |
quark.save! | |
if pic.reload.quark_ids.empty? | |
# not sure why it isn't getting added automatically... -AF | |
pic.quarks << quark | |
pic.save! | |
end | |
print quark_ids.size | |
quark_ids << quark.id.to_s | |
picture_ids << pic.id.to_s | |
puts "\tcreated picture #{pic.id} and quark #{quark.id} for Instagram #{result.id}" | |
else | |
puts "skipped #{result.id}" | |
end | |
end | |
next_max_id = results.pagination.next_max_id | |
end while next_max_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment