Skip to content

Instantly share code, notes, and snippets.

@aeris
Created November 2, 2017 21:04
Show Gist options
  • Save aeris/25ea4d73e6dd94b6b6b86c328941458e to your computer and use it in GitHub Desktop.
Save aeris/25ea4d73e6dd94b6b6b86c328941458e to your computer and use it in GitHub Desktop.
Export photo Cozy v2
#!/usr/bin/env ruby
require 'typhoeus'
require 'oj'
require 'base64'
require 'fileutils'
def get(url, params = {})
res = Typhoeus.get url, params: params, headers: {'Accept' => 'application/json'}
Oj.load(res.body)
end
def content(database, id, rev)
url = [database, id].join '/'
get url, rev: rev, attachments: true
end
database = 'http://localhost:5984/cozy'
get(database + '/_design/photo/_view/all')['rows'].collect { |r| r['value'] }.each do |doc|
title = File.join 'export', doc['title']
raw = doc['binary']['raw']
raw = content database, raw['id'], raw['rev']
raw = raw['_attachments']['raw']['data']
raw = Base64.decode64 raw
FileUtils.mkdir_p File.dirname title
File.open(title, 'wb') { |f| f.write raw }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment