VRFS-3936 rake task to generate mobile jtx json file

This commit is contained in:
Jonathan Kolyer 2017-05-18 23:33:03 -07:00
parent af4678cbed
commit 5c87e38022
1 changed files with 98 additions and 0 deletions

98
web/lib/tasks/mobile.rake Normal file
View File

@ -0,0 +1,98 @@
require 'csv'
namespace :mobile do
task :jam_tracks_json => :environment do |task, args|
fname = 'all-jamkazam-jamtracks.csv'
tmp_fname = "/tmp/#{fname}"
File.delete(tmp_fname) if File.exists?(tmp_fname)
unless File.exists?(tmp_fname)
cmd = "curl -s https://s3.amazonaws.com/jamkazam-public/public/lists/#{fname} > #{tmp_fname}"
`#{cmd}`
end
if File.exists?(tmp_fname)
jtx_json = {
jtx_data: [],
jtx_indices: {
alphanum: {
A: [],
B: [],
C: [],
D: [],
E: [],
F: [],
G: [],
H: [],
I: [],
J: [],
K: [],
L: [],
M: [],
N: [],
O: [],
P: [],
Q: [],
R: [],
S: [],
T: [],
U: [],
V: [],
W: [],
X: [],
Y: [],
Z: [],
'#': [],
},
artist: {},
}
}
CSV.foreach(tmp_fname) do |row|
jtartist = row[0]
jtname = row[1]
jt = JamTrack.
where(name: jtname, original_artist: jtartist).
limit(1).
first
next if jt.blank?
jtdata = {
artist: jtartist,
genre: jt.genres.map(&:description).join(', '),
id: jt.id,
name: jtname,
plan_code: jt.plan_code,
year: jt.year
}
jtx_json[:jtx_data] << jtdata
indices = jtx_json[:jtx_indices]
first_char = jtartist[0]
if (1..9) === first_char.to_i || '0' == first_char
indices[:alphanum][:'#'] << jtdata
else
indices[:alphanum][first_char.to_sym] << jtdata
end
idx_artist = indices[:artist][jtartist]
unless idx_artist
idx_artist = []
indices[:artist][jtartist] = idx_artist
end
idx_artist << jtdata
end
puts jtx_json.to_json
else
puts "*** ERROR: download failed: `#{cmd}`"
end
end
end