Last active
March 16, 2017 09:55
-
-
Save craigeley/e5dbe8c29a3abad7fee9 to your computer and use it in GitHub Desktop.
An example of moving Reporter entries to Airtable in near-real time using Reporter's JSON output and the Airtable API.
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
#!/usr/bin/env ruby | |
# Scipt Example for Moving Reporter Entries to Airtable Rows in near-real time | |
require 'time' | |
require 'json' | |
# To prevent encoding errors on OSX | |
if RUBY_VERSION =~ /2.*.*/ | |
Encoding.default_external = Encoding::UTF_8 | |
Encoding.default_internal = Encoding::UTF_8 | |
end | |
# Set Personal Variables - Path to Dropbox; Airtable Table ID; Airtable API Key | |
filepath = "/path/to/Dropbox/Apps/Reporter Beta/" | |
table = "TABLE_ID" | |
key = "API_KEY" | |
# Set and Reset Script Variables | |
tag = [] | |
name = [] | |
location = '' | |
hash = {:fields => {}} | |
tagid = '' | |
nameid = '' | |
placeid = '' | |
# Get the current file and parse it | |
datepath = Time.now.strftime("%Y-%m-%d") | |
file = File.open(filepath+datepath+"-reporter-export.json", encoding: 'UTF-8') | |
json = file.read | |
data = JSON.parse(json) | |
# Grab the data - default answers for sleep and wake, user input for everything else | |
imp = data["snapshots"][-1]["reportImpetus"].to_i | |
if imp == 3 | |
tag = tag.push("Sleep") | |
act = "Heading to bed" | |
elsif imp == 4 | |
tag = tag.push("Sleep") | |
act = "Waking up" | |
else | |
data["snapshots"][-1]["responses"].each do |response| | |
if response["questionPrompt"] =~ /tag/ | |
response["tokens"].each do |text| | |
tag.push(text["text"]) | |
end | |
elsif response["questionPrompt"] =~ /doing/ | |
act = response["tokens"][0]["text"] | |
elsif response["questionPrompt"] =~ /Where/ && response["locationResponse"] != nil | |
location = response["locationResponse"]["text"] | |
elsif response["questionPrompt"] =~ /Who/ && response["tokens"] != nil | |
response["tokens"].each do |text| | |
name.push(text["text"]) | |
end | |
end | |
end | |
end | |
t = data["snapshots"][-1]["date"] | |
time = Time.parse(t).utc.iso8601 | |
lat = data["snapshots"][-1]["location"]["latitude"] | |
long = data["snapshots"][-1]["location"]["longitude"] | |
if name.empty? | |
name = name.push("Alone") | |
end | |
# Start Building Airtable JSON | |
hash[:fields][:Activities] = act | |
hash[:fields][:lat] = "#{lat}" | |
hash[:fields][:lon] = "#{long}" | |
hash[:fields][:Time] = time | |
# Get Tag IDs, or create them if they don't exist | |
tag.each do |tag| | |
is_tagged = false | |
tagged = %x{curl -sS "https://api.airtable.com/v0/#{table}/Tags?&view=Main%20View" \ | |
-H "Authorization: Bearer #{key}"} | |
tags = JSON.parse(tagged) | |
tags["records"].each do |record| | |
if record["fields"]["Name"] == tag | |
is_tagged = true | |
tagid = record["id"] | |
(hash[:fields][:'Activity Tag'] ||= []) << tagid | |
end | |
end | |
if is_tagged == false && tag.length > 0 | |
newID = `curl -sS -v -XPOST https://api.airtable.com/v0/#{table}/Tags \ | |
-H "Authorization: Bearer #{key}" \ | |
-H "Content-type: application/json" \ | |
-d '{ | |
"fields": { | |
"Name": "#{tag}", | |
"Activities": [ | |
], | |
"Description": "" | |
} | |
}'` | |
tagid = JSON.parse(newID) | |
tagid = tagid["id"] | |
(hash[:fields][:'Activity Tag'] ||= []) << tagid | |
end | |
end | |
# Get Person Name IDs, or create them if they don't exist | |
name.each do |name| | |
is_named = false | |
named = %x{curl -sS "https://api.airtable.com/v0/#{table}/People?&view=Main%20View" \ | |
-H "Authorization: Bearer #{key}"} | |
names = JSON.parse(named) | |
names["records"].each do |record| | |
if record["fields"]["Name"] == name | |
is_named = true | |
nameid = record["id"] | |
(hash[:fields][:People] ||= []) << nameid | |
end | |
end | |
if is_named == false && name.length > 0 | |
newID = `curl -sS -v -XPOST https://api.airtable.com/v0/#{table}/People \ | |
-H "Authorization: Bearer #{key}" \ | |
-H "Content-type: application/json" \ | |
-d '{ | |
"fields": { | |
"Name": "#{name}", | |
"Activities": [ | |
] | |
} | |
}'` | |
nameid = JSON.parse(newID) | |
nameid = nameid["id"] | |
(hash[:fields][:People] ||= []) << nameid | |
end | |
end | |
# Get Location IDs, or create them if they don't exist | |
is_located = false | |
located = %x{curl -sS "https://api.airtable.com/v0/#{table}/Locations?&view=Main%20View" \ | |
-H "Authorization: Bearer #{key}"} | |
spots = JSON.parse(located) | |
spots["records"].each do |record| | |
if record["fields"]["Name"] == location | |
is_located = true | |
placeid = [record["id"]] | |
hash[:fields][:Location] = placeid | |
end | |
end | |
if is_located == false && location.length > 0 | |
newID = `curl -sS -v -XPOST https://api.airtable.com/v0/#{table}/Locations \ | |
-H "Authorization: Bearer #{key}" \ | |
-H "Content-type: application/json" \ | |
-d '{ | |
"fields": { | |
"Name": "#{location}", | |
"Activities": [ | |
] | |
} | |
}'` | |
placeid = JSON.parse(newID) | |
placeid = [placeid["id"]] | |
hash[:fields][:Location] = placeid | |
end | |
# Build and push JSON to the Airtable API | |
output = JSON.pretty_generate(hash) | |
`curl -v -XPOST https://api.airtable.com/v0/#{table}/Activities \ | |
-H "Authorization: Bearer #{key}" \ | |
-H "Content-type: application/json" \ | |
-d '#{output}'` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment