# encoding: utf-8 module TokiCLI class Import def initialize toki, view @toki_api = toki @view = view end def restore token, channel_id please_quit backup_db adn_data = get_adn_data token, channel_id db = create_db create_table db lines = decode adn_data populate db, lines replace end def replace if File.exist? @toki_api.helpers.db_path puts "\nAre you sure you want to replace the current Toki.app database?\n\nHit SPACE to continue (any other key to cancel).\n\n" abort("Canceled.\n\n") unless STDIN.getch == ' ' FileUtils.mv @toki_api.helpers.db_path, "#{Dir.home}/.Trash/" end FileUtils.mv @new_db_path, @toki_api.helpers.db_path puts "\n\nDone. You may relaunch Toki.app now.\n\n" end def populate db, lines puts "Populating new database with App.net data...\n\n" before = Time.now idx = 0 db.transaction do |db_in_transaction| lines.each do |obj| insert_data = {} insert_data[':id'] = obj[:id] insert_data[':bundleIdentifier'] = obj[:bundleIdentifier] insert_data[':activeTo'] = obj[:activeTo] insert_data[':activeFrom'] = obj[:activeFrom] insert_data[':totalSeconds'] = obj[:totalSeconds] insert_data[':UUID'] = obj[:uuid] insert_data[':synced'] = 1 insert_data[':availableToSync'] = 1 db_in_transaction.prepare("INSERT INTO plain(id, bundleIdentifier, activeTo, activeFrom, totalSeconds, UUID, synced, availableToSync) VALUES(:id, :bundleIdentifier, :activeTo, :activeFrom, :totalSeconds, :UUID, :synced, :availableToSync);") do |insert| insert.execute insert_data end idx += 1 print "Processed #{idx} of #{lines.size}\r" $stdout.flush end end puts "\n\n\nFinished inserting #{idx} lines in #{Time.now - before}.\n\n" end def decode adn_data puts "Decoding App.net data...\n\n" lines = [] adn_data.each do |obj| adn = obj['annotations'][0] lines << { type: adn['type'], table: adn['value']['c'], uuid: adn['value']['d']['UUID'], bundleIdentifier: adn['value']['d']['bundleIdentifier'], activeTo: adn['value']['d']['activeTo'], activeFrom: adn['value']['d']['activeFrom'], totalSeconds: adn['value']['d']['totalSeconds'], id: adn['value']['d']['id'] } end lines end def create_table db puts "Creating table...\n\n" db.execute_batch <<-SQL CREATE TABLE KKAppActivity ( id INTEGER, bundleIdentifier VARCHAR(256), activeTo INTEGER, activeFrom INTEGER, totalSeconds INTEGER, UUID VARCHAR(256), synced INTEGER, availableToSync INTEGER ); SQL db.reload_schema! end def create_db puts "\n\nCreating a new SQLite3 database...\n\n" @new_db_path = "#{@toki_api.helpers.toki_path}/db_from_adn.sqlite3" Amalgalite::Database.new @new_db_path end def get_adn_data token, channel_id puts "Connecting to App.net...\n\n" channels = ADNChannels::GetMessages.new(token) channels.get_messages(channel_id) end def backup_db if File.exist? @toki_api.helpers.db_path FileUtils.copy(@toki_api.helpers.db_path, "#{@toki_api.helpers.toki_path}/toki_data.sqlite3.bak") puts "Current database has been backed up in #{@toki_api.helpers.toki_path}\n\n" end end def please_quit puts "\n\nYou need to quit Toki.app before restoring data from App.net.\n\nPlease quit Toki.app then hit SPACE to continue (any other key to cancel).\n\n" abort("Canceled.\n\n") unless STDIN.getch == ' ' end end end