Skip to content

Instantly share code, notes, and snippets.

@timfall
Last active December 16, 2015 08:39
Show Gist options
  • Save timfall/5407500 to your computer and use it in GitHub Desktop.
Save timfall/5407500 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'json'
require 'oauth'
#require '../lib/OauthFigsharesync'
#require '../lib/FigshareClasses'
workingdir = "~/.figsharesync/"
consumerkey
consumersecret
accesstoken
accesstokensecret
localarticlelist
#First run setup
absworkingdir = File.expand_path(workingdir)
if File.exist?("#{absworkingdir}/FirstRun.lock")
break
else
puts "This appears to be the first run of FigShare Sync. We'll setup a few things."
print "Where would you like to store settings and files? [~./figsharesync]: "
@input = gets.chomp
if @input != nil
workingdir = @input
absworkingdir = File.expand_path(workingdir)
else
break
end
print "Please enter OAuth consumer key: "
consumerkey = gets.chomp
print "Please enter OAuth consumer key secret: "
consumersecret = gets.chomp
print "Please enter OAuth access token: "
accesstoken = gets.chomp
print "Please enter OAuth access token secret: "
accesstokensecret = gets.chomp
puts "Great! we'll get running now..."
File.new("#{absworkingdir}/FirstRun.lock", "r")
end
#oauth authenticate
auth = OauthFighshare.new(consumerkey, consumersecret, accesstoken, accesstokensecret)
if auth.head (v1/my_data/articles) =! #http auth error
puts "Figshare returned #{auth.head (v1/my_data/articles)}"
end
puts "Successfully authenticated"
#Create article database and populate from local sources
if File.exists?("#{absworkingdir}/localdb.json")
puts "Local database detected, populating from there"
ldb = JSON.parse("#{absworkingdir}/localdb.json")
@i = ldb['count']
for @i.times do
localarticlelist[i] = Article.new(ldb[i].name, "#{absworkingdir}/localdb/#{ldb[i].name}", ldb[i].id)
done
puts "Done populating the local list from local database"
else
puts "No local database detected, grabbing one from the server"
ldb = auth.get('v1/my_data/articles')
ldb = JSON.parse(ldb)
@local = File.new("#{absworkingdir}/localdb.json", "w")
@local << ldb
i = ldb['count']
for i.times do
localarticlelist[i] = Article.new(ldb[i].name, "#{absworkingdir}/localdb/#{ldb[i].name}", ldb[i].id)
done
puts "Local database file created and populated from server"
done
#Compare local and remote databses for differences
rdb = auth.get('v1/my_data/articles')
rdb = JSON.parse(rdb)
i = rdb[count]
for i.times do
remotearticlelist[i] = Article.new(rdb[i].name, "v1/my_data/articles/#{ldb[i].name}", rdb[i].id)
done
for i.times do
@remotehash = remotearticlelist[i].hash
@localhash = localarticlelist[i].hash
if @remotehash != @localhash
puts "Hash mismatch! #{remotearticlelist[i].name} and #{localarticlelist[i].name} do not match"
end
puts "All hashes match"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment