Skip to content

Instantly share code, notes, and snippets.

@daveyeu
Created November 18, 2013 14:42
Show Gist options
  • Save daveyeu/7528904 to your computer and use it in GitHub Desktop.
Save daveyeu/7528904 to your computer and use it in GitHub Desktop.
Fetch GroupMe chat history
require "yajl"
GROUP_ID = ENV["GROUP_ID"]
TOKEN = ENV["TOKEN"]
file = File.open("Group-#{GROUP_ID}.txt", "w")
query_string = ""
results = []
loop do
res = `curl -s -H"X-Access-Token: #{TOKEN}" https://v2.groupme.com/groups/#{GROUP_ID}/messages#{query_string}`
break if res.size == 0
data = Yajl.load(res)
messages = data["response"]["messages"]
break if messages.empty?
results += messages
query_string = "?before_id=#{messages.last["id"]}"
print "."
end
name_width = results.map { |r| r["name"] }.map(&:size).max + 1
results.reverse.each do |result|
name = result["system"] ? "" : result["name"]
time = Time.at(result["created_at"])
time_string = "[#{time.strftime('%Y/%m/%d %I:%M%P')}] "
texts = []
if result["picture_url"] && result["picture_url"].size > 0
texts << result["picture_url"]
end
if result["text"] && result["text"].size > 0
result["text"].split("\n").each do |t|
texts << t
end
end
next if texts.empty?
texts.each_with_index do |t, i|
if i == 0
str = time_string + ("%-#{name_width}s:" % name)
else
str = (" " * time_string.size) + "%-#{name_width}s:" % ""
end
file.write(str + " " + t + "\n")
end
end
puts
puts "Results (size): #{results.size}"
file.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment