-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreams_callback_handler.cpp
More file actions
44 lines (38 loc) · 1.58 KB
/
streams_callback_handler.cpp
File metadata and controls
44 lines (38 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-License-Identifier: MIT
// Receive AudD stream callbacks via an HTTP server (cpp-httplib).
//
// ./streams_callback_handler # listens on :8080/audd-callback
#include <iostream>
#include <httplib.h>
#include <audd/audd.hpp>
int main() {
httplib::Server server;
server.Post("/audd-callback", [](const httplib::Request& req, httplib::Response& res) {
try {
auto ev = audd::handle_callback(req.body);
std::visit([&](auto&& v) {
using T = std::decay_t<decltype(v)>;
if constexpr (std::is_same_v<T, audd::StreamCallbackMatch>) {
std::cout << "recognized: " << v.song.artist
<< " — " << v.song.title
<< " (radio " << v.radio_id
<< ", score " << v.song.score << ")\n";
for (const auto& alt : v.alternatives) {
std::cout << " alt: " << alt.artist
<< " — " << alt.title << "\n";
}
} else if constexpr (std::is_same_v<T, audd::StreamCallbackNotification>) {
std::cout << "notification " << v.notification_code
<< ": " << v.notification_message << "\n";
}
}, ev);
res.status = 200;
} catch (const audd::AudDError& e) {
res.status = 400;
res.set_content(e.what(), "text/plain");
}
});
std::cout << "listening on :8080\n";
server.listen("0.0.0.0", 8080);
return 0;
}