Created
June 30, 2020 16:10
-
-
Save ityonemo/97c47f2b5ce5dea8d555d9f357eb3bc9 to your computer and use it in GitHub Desktop.
itinerary code
This file contains 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
using JSON | |
#make sure we have what we're looking for. | |
(length(ARGS) == 0) && throw(ErrorException("needs a file name")) | |
################################### | |
#rows iterator | |
type rows; tgt::Matrix; end | |
Base.start(r::rows) = 1 | |
Base.next(r::rows, idx) = (r.tgt[idx, :], idx + 1) | |
Base.done(r::rows, idx) = idx > size(r.tgt, 1) | |
Base.length(r::rows) = size(r.tgt,1) | |
################################### | |
api_key = #API_KEy | |
#set up a dictionary of locations. | |
location_dict = Dict{Symbol, String}() | |
known_distances = Dict{Tuple{Symbol, Symbol}, Float64}() | |
addresses = readcsv("Addresses.csv") | |
for row in rows(addresses) | |
location_dict[Symbol(row[1])] = row[2] | |
end | |
function api_lookup(strt_loc, dest_loc) | |
strt_addr = replace(location_dict[strt_loc],r"\s","+") | |
dest_addr = replace(location_dict[dest_loc],r"\s","+") | |
query = string("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=",strt_addr,"&destinations=",dest_addr,"&key=",api_key) | |
cr = readstring(`curl $query`) | |
j = JSON.parse(cr) | |
try | |
Float64(parse(split(j["rows"][1]["elements"][1]["distance"]["text"])[1])) | |
catch | |
return -1.0 | |
end | |
end | |
function lookup_distance(strt_loc, dest_loc) | |
if haskey(known_distances, (strt_loc, dest_loc)) | |
string(known_distances[(strt_loc, dest_loc)]) | |
else | |
#actually do API stuff here. | |
distance = api_lookup(strt_loc, dest_loc) | |
#distance = 1.0 | |
known_distances[(strt_loc, dest_loc)] = distance | |
string(distance) | |
end | |
end | |
#to limit querying of the google api, set dummyrun to true (this lets | |
#you debug the keywords). | |
dummyrun = false | |
###################################################################### | |
# actual processing. | |
list_filename = ARGS[1] #file to be read is the first argument | |
listtbl = readcsv(list_filename) #read the csv file. | |
listlen = size(listtbl)[1] #grab the length of the csv file | |
output_tbl = Matrix{String}(listlen,4) # output is a matrix of same length w/ 4 columns. | |
for (index, row) in enumerate(rows(listtbl[:,1:3])) | |
println(row) | |
if row[1] == "Date" #exception is the first row. | |
output_tbl[index, 1:3] = row | |
output_tbl[index,4] = "" | |
else | |
strt_loc = Symbol(row[2]) | |
dest_loc = Symbol(row[3]) | |
#make sure the strt_loc and dest_loc are in the location dictionary | |
haskey(location_dict, strt_loc) || throw(ErrorException("key for $strt_loc not found")) | |
haskey(location_dict, dest_loc) || throw(ErrorException("key for $dest_loc not found")) | |
#spit back out the first three columns of the original file | |
output_tbl[index, 1:3] = row | |
#for the fourth column, lookup the distance (unless we are just doing a dummy run). | |
output_tbl[index, 4] = dummyrun ? "10" : lookup_distance(strt_loc, dest_loc) | |
end | |
end | |
writecsv(string("final_", list_filename), output_tbl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment