Skip to content

Instantly share code, notes, and snippets.

@timfall
Last active December 16, 2015 08:39

Revisions

  1. timfall revised this gist Apr 20, 2013. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -56,7 +56,7 @@
    else
    puts "No local database detected, grabbing one from the server"
    ldb = auth.get('v1/my_data/articles')
    ldb = JSON.parse(ldb)
    ldb = JSON.parse(ldb.body)
    @local = File.new("#{absworkingdir}/localdb.json", "w")
    @local << ldb
    i = ldb['count']
    @@ -68,7 +68,7 @@

    #Compare local and remote databses for differences
    rdb = auth.get('v1/my_data/articles')
    rdb = JSON.parse(rdb)
    rdb = JSON.parse(rdb.body)
    i = rdb[count]
    r = ldb[count]
    for i.times do
    @@ -85,7 +85,9 @@
    File.new('#{absworkingdir}/localdb/#{remotearticlelist[i].name}', "w") = auth.get(remotearticlelist[i].path)#download remote version
    localarticlelist[r] = Article.new(rdb[i].name, "#{absworkingdir}/localdb/#{rbd[i].name}", rbd[i].id)#create new local article entry
    download = auth.get(remotearticlelist[i].path)
    #replace entry in localdb.json
    download = JSON.parse(download.body)
    @local = File.read("#{absworkingdir}/localdb.json")
    @local.gsub(/pattern/) { matchmatchmatch } #replace entry in localdb.json
    end
    elsif File.mtime(remotearticlelist[i].path) <= File.mtime(localarticlelist[r].path)#If local version is newer than remote
    localfile = JSON.pretty_generate('Title' => localarticlelist[r].title, 'description' => localarticlelist[r].description, 'defined_type' => localarticlelist[r].defined_type)#generate a json list from the object
  2. timfall revised this gist Apr 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@
    #oauth authenticate
    auth = OauthFighshare.new(consumerkey, consumersecret, accesstoken, accesstokensecret)
    if auth.header (v1/my_data/articles) == Net::HTTPUnauthorized
    puts "Figshare returned #{auth.head (v1/my_data/articles)}"
    puts "Figshare returned #{auth.header (v1/my_data/articles)}"
    end
    puts "Successfully authenticated"

  3. timfall revised this gist Apr 18, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@
    end
    #oauth authenticate
    auth = OauthFighshare.new(consumerkey, consumersecret, accesstoken, accesstokensecret)
    if auth.head (v1/my_data/articles) =! #http auth error
    if auth.header (v1/my_data/articles) == Net::HTTPUnauthorized
    puts "Figshare returned #{auth.head (v1/my_data/articles)}"
    end
    puts "Successfully authenticated"
  4. timfall revised this gist Apr 18, 2013. 1 changed file with 19 additions and 7 deletions.
    26 changes: 19 additions & 7 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@
    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)
    localarticlelist[i] = Article.new(ldb[i].name, "#{absworkingdir}/localdb/#{ldb[i].name}", ldb[i].id, ldb[i].title, ldb[i].description, ldb[i].defined_type)
    done
    puts "Done populating the local list from local database"
    else
    @@ -61,7 +61,7 @@
    @local << ldb
    i = ldb['count']
    for i.times do
    localarticlelist[i] = Article.new(ldb[i].name, "#{absworkingdir}/localdb/#{ldb[i].name}", ldb[i].id)
    localarticlelist[i] = Article.new(ldb[i].name, "#{absworkingdir}/localdb/#{ldb[i].name}", ldb[i].id, ldb[i].title, ldb[i].description, ldb[i].defined_type)
    done
    puts "Local database file created and populated from server"
    done
    @@ -72,16 +72,28 @@
    i = rdb[count]
    r = ldb[count]
    for i.times do
    remotearticlelist[i] = Article.new(rdb[i].name, "v1/my_data/articles/#{ldb[i].name}", rdb[i].id)
    remotearticlelist[i] = Article.new(rdb[i].name, "v1/my_data/articles/#{ldb[i].name}", rdb[i].id, rdb[i].title, rdb[i].description, rdb[i].defined_type)
    done
    for i.times do
    for r.times do
    @remotehash = remotearticlelist[i].hash
    @localhash = localarticlelist[r].hash
    if @remotehash == @localhash
    if rdb[i].id == ldb[r].id
    if @remotehash != @localhash
    puts "Hash mismatch! #{remotearticlelist[i].name} and #{localarticlelist[i].name} do not match"
    puts "Hash mismatch! #{remotearticlelist[i].name} and #{localarticlelist[r].name} do not match"
    if File.mtime(remotearticlelist[i].path) >= File.mtime(localarticlelist[r].path)#If remote version is newer than local
    File.new('#{absworkingdir}/localdb/#{remotearticlelist[i].name}', "w") = auth.get(remotearticlelist[i].path)#download remote version
    localarticlelist[r] = Article.new(rdb[i].name, "#{absworkingdir}/localdb/#{rbd[i].name}", rbd[i].id)#create new local article entry
    download = auth.get(remotearticlelist[i].path)
    #replace entry in localdb.json
    end
    elsif File.mtime(remotearticlelist[i].path) <= File.mtime(localarticlelist[r].path)#If local version is newer than remote
    localfile = JSON.pretty_generate('Title' => localarticlelist[r].title, 'description' => localarticlelist[r].description, 'defined_type' => localarticlelist[r].defined_type)#generate a json list from the object
    auth.post('v1/my_data/articles/', localfile, {"Content-Type" => "application/json"})#upload json list
    end
    end
    end
    puts "All hashes match"
    done
    next
    done
    done
    puts "All hashes match"
  5. timfall revised this gist Apr 17, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -76,9 +76,9 @@
    done
    for i.times do
    for r.times do
    if remotearticlelist[i].hash == localarticlelist[r].hash
    @remotehash = remotearticlelist[i].hash
    @localhash = localarticlelist[i].hash
    @remotehash = remotearticlelist[i].hash
    @localhash = localarticlelist[r].hash
    if @remotehash == @localhash
    if @remotehash != @localhash
    puts "Hash mismatch! #{remotearticlelist[i].name} and #{localarticlelist[i].name} do not match"
    end
  6. timfall revised this gist Apr 17, 2013. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -70,14 +70,18 @@
    rdb = auth.get('v1/my_data/articles')
    rdb = JSON.parse(rdb)
    i = rdb[count]
    r = ldb[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"
    for r.times do
    if remotearticlelist[i].hash == localarticlelist[r].hash
    @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
    end
    puts "All hashes match"
    done
  7. timfall revised this gist Apr 17, 2013. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -68,4 +68,16 @@

    #Compare local and remote databses for differences
    rdb = auth.get('v1/my_data/articles')
    rdb = JSON.parse(rdb)
    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
  8. timfall revised this gist Apr 17, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -64,4 +64,8 @@
    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
    done

    #Compare local and remote databses for differences
    rdb = auth.get('v1/my_data/articles')
    rdb = JSON.parse(rdb)
  9. timfall revised this gist Apr 17, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -48,17 +48,17 @@
    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
    @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
    @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)
  10. timfall revised this gist Apr 17, 2013. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -59,5 +59,9 @@
    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
    done
  11. timfall revised this gist Apr 17, 2013. 1 changed file with 13 additions and 2 deletions.
    15 changes: 13 additions & 2 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,7 @@
    consumersecret
    accesstoken
    accesstokensecret
    localarticlelist

    #First run setup
    absworkingdir = File.expand_path(workingdir)
    @@ -47,6 +48,16 @@
    if File.exists?("#{absworkingdir}/localdb.json")
    puts "Local database detected, populating from there"
    ldb = JSON.parse("#{absworkingdir}/localdb.json")
    i = lab['count']
    i = ldb['count']
    for i.times do
    Article.new(JSON.parse("#{absworkingdir}/localdb.json"))
    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
    puts "Local database file created and populated from server"
    done
  12. timfall revised this gist Apr 17, 2013. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@
    @input = gets.chomp
    if @input != nil
    workingdir = @input
    absworkingdir = File.expand_path(workingdir)
    else
    break
    end
    @@ -42,4 +43,10 @@
    end
    puts "Successfully authenticated"

    #Create article database and populate from local sources
    #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 = lab['count']
    for i.times do
    Article.new(JSON.parse("#{absworkingdir}/localdb.json"))
  13. timfall revised this gist Apr 17, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@

    #First run setup
    absworkingdir = File.expand_path(workingdir)
    if File.exist?(absworkingdir)
    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."
    @@ -33,6 +33,7 @@
    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)
  14. timfall created this gist Apr 17, 2013.
    44 changes: 44 additions & 0 deletions figsharesync-test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    #!/usr/bin/ruby

    require 'json'
    require 'oauth'
    #require '../lib/OauthFigsharesync'
    #require '../lib/FigshareClasses'

    workingdir = "~/.figsharesync/"
    consumerkey
    consumersecret
    accesstoken
    accesstokensecret

    #First run setup
    absworkingdir = File.expand_path(workingdir)
    if File.exist?(absworkingdir)
    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
    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..."
    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