Last active
December 17, 2015 18:18
-
-
Save ox/5651704 to your computer and use it in GitHub Desktop.
Fetch all of your gchat messages and threads and stuff them into couchdb.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendAllGChatThreadsToURL(url) { | |
var total = 0 | |
, offset = 500 // the max | |
Logger.log('starting...') | |
for (var start = 0; ; start += offset) { | |
var threads = GmailApp.getChatThreads(start, offset) | |
if (threads.length === 0) { | |
Logger.log('no more threads left') | |
break | |
} | |
for (var i = 0; i < threads.length; i++) { | |
// if/when you run out of execution time, use the last logged value + 1 | |
// as the next `start` for the script. | |
Logger.log('thread: ' + (start + i)) | |
var thread = threads[i] | |
, messages = thread.getMessages() | |
var payload = []; | |
for (var j = messages.length - 1; j >= 0 ; j--) { | |
var message = messages[j] | |
if (!message) { | |
continue | |
} | |
payload.push({ | |
id: message.getId() | |
, thread: thread.getId() | |
, from: message.getFrom() | |
, to: message.getTo() | |
, body: Utilities.base64Encode(message.getBody()) | |
, date: message.getDate() | |
}) | |
total += 1 | |
} | |
var params = { | |
method: "post" | |
, payload: { | |
messages: Utilities.jsonStringify(payload) | |
} | |
} | |
UrlFetchApp.fetch(url, params) | |
} | |
} | |
Logger.log('sent ' + total + ' messages') | |
} | |
function main() { | |
var url = "http://XXXX.localtunnel.com/gchat" | |
sendAllGChatThreadsToURL(url) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json/ext' | |
require 'couchrest' | |
require 'cgi' | |
require 'base64' | |
require 'pp' | |
require 'sinatra' | |
db = CouchRest.database!('http://localhost:5984/gchat') | |
post '/gchat' do | |
return if params.empty? or params['messages'].nil? | |
messages = JSON.parse(params['messages']) | |
messages.each do |message| | |
message['body'] = CGI.unescapeHTML(Base64.decode64(message['body'])) | |
message['date'] = Time.parse(message['date']) | |
message['day'] = message['date'].day | |
message['month'] = message['date'].month | |
message['year'] = message['date'].year | |
message['from'] = message['from'].split('<').first.strip | |
message['to'] = message['to'].split('<').first.strip | |
db.save_doc message | |
end | |
return :ok | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for converting xml backups from SMS Backup and Restore Android app into | |
# CouchDB documents | |
require 'json/ext' | |
require 'nori' | |
require 'couchrest' | |
require 'nokogiri' | |
require 'pp' | |
@parser = Nori.new | |
@db = CouchRest.database!('http://localhost:5984/sms') | |
Dir['SMSBackups/*'].each do |file| | |
f = File.open(file) | |
doc = Nokogiri::XML(f).css('sms') | |
f.close | |
doc.each do |message| | |
m = @parser.parse(message.to_s)['sms'] | |
m.keys.each do |k| | |
m[k.gsub('@','')] = m[k] | |
end | |
m['address'] = m['address'].gsub /([ -])/, '' | |
m['date'] = Time.at(m['date'][0..-4].to_i) | |
m['date_sent'] = Time.at(m['date_sent'][0..-4].to_i) | |
m['day'] = m['date'].day | |
m['month'] = m['date'].month | |
m['year'] = m['date'].year | |
next if (@db.view 'texts/all', key: m['date'])['rows'].size > 0 | |
@db.save_doc m | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment