Scope issues. Moved to separate functions.

This commit is contained in:
Josh W 2020-02-10 12:19:28 -05:00
parent 95c24cd23d
commit 092894c7f9

View file

@ -59,16 +59,35 @@ def smart_cross(~start_next=5.,~fade_in=3.,~fade_out=3.,
conservative=conservative, transition,s) conservative=conservative, transition,s)
end end
def get_http_data(headers, url) =
if string.contains(prefix='https', url) then
let (status, _, data) = https.get(headers=headers, url)
let (_, status_code, _) = status
(status_code, data)
else
let (status, _, data) = http.get(headers=headers, url)
let (_, status_code, _) = status
(status_code, data)
end
end
def post_http_data(headers, postdata, url) =
if string.contains(prefix='https', url) then
let (status, _, data) = https.post(headers=headers, data=postdata, url)
let (_, status_code, _) = status
(status_code, data)
else
let (status, _, data) = http.post(headers=headers, data=postdata, url)
let (_, status_code, _) = status
(status_code, data)
end
end
def next_song() = def next_song() =
log = log(label="next_song") log = log(label="next_song")
log("Requesting the next song from #{url_next}") log("Requesting the next song from #{url_next}")
if string.contains(prefix='https', url_next) then let (status_code, data) = get_http_data(api_headers_next, url_next)
let (status, _, data) = https.get(headers=api_headers_next, url_next)
else
let (status, _, data) = http.get(headers=api_headers_next, url_next)
end
let (_, status_code, _) = status
log(data) log(data)
@ -103,12 +122,7 @@ def just_played(m) =
if m["req_id"] != "" then if m["req_id"] != "" then
log('Letting server know we played request ID #{m["req_id"]} here: #{url_played}') log('Letting server know we played request ID #{m["req_id"]} here: #{url_played}')
played_song = json_of(compact=true, [("song_request", int_of_string(m["req_id"]))]) played_song = json_of(compact=true, [("song_request", int_of_string(m["req_id"]))])
if string.contains(prefix='https', url_played) then let (status_code, data) = post_http_data(api_headers_played, played_song, url_played)
let (status, _, data) = https.post(headers=api_headers_played, data=played_song, url_played)
else
let (status, _, data) = http.post(headers=api_headers_played, data=played_song, url_played)
end
let (_, status_code, _) = status
if status_code == 204 then if status_code == 204 then
log('Successfully reported that the song was played.') log('Successfully reported that the song was played.')