VRFS-3936 creating mobile jamtrack list json

This commit is contained in:
Jonathan Kolyer 2017-05-24 18:44:08 -07:00
parent 5c87e38022
commit cc6da7127a
2 changed files with 97 additions and 83 deletions

View File

@ -69,15 +69,26 @@ class ApiSearchController < ApiController
def jam_tracks
if request.get?
if params[:iso639]
render(json: JamTrackSearch.all_languages.to_json, status: 200) and return
render(json: JamTrackSearch.all_languages.to_json, status: 200)
elsif params[:indexed]
fname = "#{Rails.root}/tmp/jtx_indices.json"
unless File.exists?(fname) && File.size(fname) > 0
SampleApp::Application.load_tasks
Rake::Task["mobile:jam_tracks_json"].invoke
end
render(file: fname)
else
render(json: {}, status: 200) and return
render(json: {}, status: 200)
end
elsif request.post?
jts = JamTrackSearch.new
filter = request.params[:api_search]
result = jts.search_results_page(filter)
render(json: result.to_json, status: 200) and return
render(json: result.to_json, status: 200)
end
end

View File

@ -1,7 +1,6 @@
require 'csv'
namespace :mobile do
task :jam_tracks_json => :environment do |task, args|
fname = 'all-jamkazam-jamtracks.csv'
tmp_fname = "/tmp/#{fname}"
@ -13,86 +12,90 @@ namespace :mobile do
`#{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
unless File.exists?(tmp_fname)
puts "*** ERROR: download failed: `#{cmd}`"
return
end
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].upcase
if (1..9) === first_char.to_i || '0' == first_char
indices[:alphanum][:'#'] << jtdata
else
aa = indices[:alphanum][first_char.to_sym]
indices[:alphanum][first_char.to_sym] = aa = [] unless aa
aa << jtdata
end
idx_artist = indices[:artist][jtartist]
unless idx_artist
idx_artist = []
indices[:artist][jtartist] = idx_artist
end
idx_artist << jtdata
end
File.open("#{Rails.root}/tmp/jtx_indices.json", 'w') do |ff|
ff.write jtx_json.to_json
end
end
end
# puts jtx_json.to_json
end # task
end # namespace