diff --git a/lib/skype_archive.rb b/lib/skype_archive.rb index 7f37aea..ed24244 100644 --- a/lib/skype_archive.rb +++ b/lib/skype_archive.rb @@ -4,6 +4,8 @@ require "json" module SkypeArchive + URL = "http://localhost" + autoload :Model, "skype_archive/model" autoload :Message, "skype_archive/message" diff --git a/lib/skype_archive/message.rb b/lib/skype_archive/message.rb index 3cd8bbb..8ad8e9a 100644 --- a/lib/skype_archive/message.rb +++ b/lib/skype_archive/message.rb @@ -8,6 +8,10 @@ def initialize(attributes={}) @attributes[:created_at] = Time.at(@timestamp) end + def to_json(*opts) + @attributes.to_json(*opts) + end + def method_missing(method, *args, &block) @attributes[method.to_sym] or super end diff --git a/lib/skype_archive/model.rb b/lib/skype_archive/model.rb index ecba021..a89541a 100644 --- a/lib/skype_archive/model.rb +++ b/lib/skype_archive/model.rb @@ -1,5 +1,9 @@ require "sqlite3" require "sequel" +require "active_support/core_ext/time/calculations" +require "rest_client" +require "json" +require "base64" module SkypeArchive class Model @@ -21,6 +25,51 @@ def search(text, options={}) query.all.map { |attrs| Message.new(attrs) } end + def sync + start_time = Time.now.yesterday.at_beginning_of_day.to_i + end_time = Time.now.yesterday.at_midnight.to_i + sync_contacts + sync_conversations + sync_participants + sync_messages(start_time, end_time) + end + + def sync_contacts + contacts = connection[:Contacts].select(:skypename, :displayname).all + RestClient.post "#{URL}/users", contacts.to_json, :content_type => :json, :accept => :json + contacts + end + + def sync_conversations + @conversations = connection[:Conversations].select(:id, :identity, :displayname).all + RestClient.post "#{URL}/conversations", @conversations.to_json, :content_type => :json, :accept => :json + @conversations + end + + def sync_participants + @conversations or sync_conversations + @conversations.each do |convo| + participants = connection[:Participants].filter(:convo_id => convo[:id]).select(:identity).all + unless participants.empty? + url = "#{URL}/conversations/#{Base64.encode64(convo[:identity])[0...-1]}/participants" + RestClient.post url, participants.to_json, :content_type => :json, :accept => :json + end + end + end + + def sync_messages(start_time=0, end_time=Time.now.to_i) + @conversations or sync_conversations + @conversations.each do |convo| + messages = connection[:Messages].filter("convo_id = ? and timestamp >= ? and timestamp <= ?", convo[:id], start_time, end_time). + select(:author, :from_dispname, :remote_id, :body_xml, :timestamp). + all.map { |attrs| Message.new(attrs) } + unless messages.empty? + url = "#{URL}/conversations/#{Base64.encode64(convo[:identity])[0...-1]}/messages" + RestClient.post url, messages.to_json, :content_type => :json, :accept => :json + end + end + end + private def connection @connection ||= Sequel.sqlite(File.expand_path("~/Library/Application\ Support/Skype/#{account_name}/main.db")) diff --git a/skype_archive.gemspec b/skype_archive.gemspec index 809dee4..2f397bc 100644 --- a/skype_archive.gemspec +++ b/skype_archive.gemspec @@ -17,7 +17,11 @@ Gem::Specification.new do |gem| gem.add_dependency "sqlite3" gem.add_dependency "sequel" + gem.add_dependency "activesupport" + gem.add_dependency "rest-client" + gem.add_dependency "json" gem.add_development_dependency "rspec" gem.add_development_dependency "mocha" + gem.add_development_dependency "webmock" end diff --git a/spec/model/skype_archive_spec.rb b/spec/model/skype_archive_spec.rb index 047f242..1421102 100644 --- a/spec/model/skype_archive_spec.rb +++ b/spec/model/skype_archive_spec.rb @@ -7,24 +7,58 @@ Sequel.stubs(:sqlite).returns(connection) end - it "should search messages" do - model.search("gree message").should have(3).items - end + context "#search" do + it "should search messages" do + model.search("gree message").should have(3).items + end - it "should search only text messages" do - model.search("self message").should have(1).item - end + it "should search only text messages" do + model.search("self message").should have(1).item + end - it "should search by user" do - model.search("message", :skypename => "gii.jason.lai").should have(2).items - end + it "should search by user" do + model.search("message", :skypename => "gii.jason.lai").should have(2).items + end + + it "should search by timestamp" do + timestamp = Time.now.to_i - 2400 + model.search("message", :timestamp => timestamp).should have(3).items + end - it "should search by timestamp" do - timestamp = Time.now.to_i - 2400 - model.search("message", :timestamp => timestamp).should have(3).items + it "should search by conversation" do + model.search("message", :conversation => "flyerhzm").should have(1).item + end end - it "should search by conversation" do - model.search("message", :conversation => "flyerhzm").should have(1).item + context "#sync" do + it "should sync_contacts" do + stub_request(:post, "#{SkypeArchive::URL}/users") + model.sync_contacts.should have(3).items + end + + it "should sync_conversations" do + stub_request(:post, "#{SkypeArchive::URL}/conversations") + model.sync_conversations.should have(2).items + end + + it "should sync_participants" do + stub_request(:post, "#{SkypeArchive::URL}/conversations") + stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/participants") + stub_request(:post, "#{SkypeArchive::URL}/conversations/Zmx5ZXJoem0=/participants") + model.sync_participants + end + + it "should sync_message" do + stub_request(:post, "#{SkypeArchive::URL}/conversations") + stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/messages") + stub_request(:post, "#{SkypeArchive::URL}/conversations/Zmx5ZXJoem0=/messages") + model.sync_messages + end + + it "should sync_message with start_time and end_time" do + stub_request(:post, "#{SkypeArchive::URL}/conversations") + stub_request(:post, "#{SkypeArchive::URL}/conversations/JGdpaS5qYXNvbi5sYWkvMTIzNDU2Nzg5/messages") + model.sync_messages(Time.now.to_i - 4000, Time.now.to_i - 1000) + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 79f26e9..6a916ee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require 'rspec' require 'skype_archive' require 'support/sqlite_seed' +require 'webmock/rspec' DB = Sequel.sqlite diff --git a/spec/support/sqlite_seed.rb b/spec/support/sqlite_seed.rb index 6260eb3..ede73f1 100644 --- a/spec/support/sqlite_seed.rb +++ b/spec/support/sqlite_seed.rb @@ -17,13 +17,13 @@ def seed_db DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.jason.lai", :from_dispname => "Jason Lai", :remote_id => 1, :body_xml => "gree message 1", :type => 61, :timestamp => Time.now.to_i - 3600) DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.richard.huang", :from_dispname => "Richard Huang", - :remote_id => 1, :body_xml => "gree message 2", :type => 61, :timestamp => Time.now.to_i - 1800) + :remote_id => 2, :body_xml => "gree message 2", :type => 61, :timestamp => Time.now.to_i - 1800) DB[:messages].insert(:convo_id => gree_conversation_id, :author => "gii.jason.lai", :from_dispname => "Jason Lai / (赖)", - :remote_id => 1, :body_xml => "gree message 3", :type => 61, :timestamp => Time.now.to_i) + :remote_id => 3, :body_xml => "gree message 3", :type => 61, :timestamp => Time.now.to_i) DB[:messages].insert(:convo_id => myself_conversation_id, :author => "gii.richard.huang", :from_dispname => "Richard Huang", - :remote_id => 1, :body_xml => "self message 11", :type => 61, :timestamp => Time.now.to_i) + :remote_id => 4, :body_xml => "self message 11", :type => 61, :timestamp => Time.now.to_i) DB[:messages].insert(:convo_id => myself_conversation_id, :author => "flyerhzm", :from_dispname => "Richard Huang", - :remote_id => 1, :body_xml => " + :remote_id => 5, :body_xml => " Ron Buell