Forked from saranyan/bigcommerce_to_dropbox.rake
Last active
December 14, 2015 22:08
-
-
Save chaitanyakuber/5155901 to your computer and use it in GitHub Desktop.
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
require 'bigcommerce' | |
require 'httparty' | |
require 'dropbox-api' | |
namespace :backups do | |
desc "Run a backup of all images" | |
task :run => :environment do | |
# let us setup a user with bcstore and dropbox creds | |
# this assumes that the user has allowed access to the dropbox application registered and in return, | |
# we are storing their accesskey and secret. | |
# to follow the oauth flow and grab the tokens, please refer to the doc at | |
# https://github.com/futuresimple/dropbox-api | |
user = { | |
"bcstore" => { | |
"store_url" => "store-xxx.mybigcommerce.com", | |
"user_name" => "admin", | |
"api_key" => "jfkdj34..." | |
}, | |
"dbstorage" => { | |
"access_token" => "db access token here", | |
"secret" => "db secret key" | |
} | |
} | |
# configure your app - the app_key and secret is provided when you register your app with dropbox | |
Dropbox::API::Config.app_key = "appkey" | |
Dropbox::API::Config.app_secret = "appsecret" | |
Dropbox::API::Config.mode = "sandbox" | |
# just a pointless check, but illustrated as a case, if the user's data is being fetched from database | |
# to proceed, we need valid bigcommerce store creds and dropbox oauth access tokens | |
if !user.bcstore.nil? && !user.dbstorage.nil? | |
client = Dropbox::API::Client.new(:token => user["dbstorage"]["access_token"], :secret => user["dbstorage"]["secret"]) | |
api = Bigcommerce::Api.new({ | |
:store_url => user["bcstore"]["store_url"], | |
:username => user["bcstore"]["user_name"], | |
:api_key => user["bcstore"]["api_key"] | |
}) | |
# let us get 200 images, the max allowed and update the counter | |
images = api.get_products_images({:limit => 200, :page => 1}) | |
count = 0 | |
page = 1 | |
loop do | |
image = images[count] | |
img_url = user.bcstore.store_url + "/product_images/" + image["image_file"] | |
# download the image to dropbox | |
response = HTTParty.get(img_url) | |
client.upload image['image_file'], response.body | |
puts img_url | |
count++ | |
# verbose check, provided for clarity | |
# images < 200, means you have hit the last batch. | |
break if (images.size < 200) and (count == images.size - 1) | |
# else | |
page++ | |
images = api.get_products_images({:limit => 200, :page => page}) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment