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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "TidalTrackRequest.hxx"
#include "TidalErrorParser.hxx"
#include "lib/yajl/Callbacks.hxx"
using Wrapper = Yajl::CallbacksWrapper<TidalTrackRequest::ResponseParser>;
static constexpr yajl_callbacks parse_callbacks = {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
Wrapper::String,
nullptr,
Wrapper::MapKey,
Wrapper::EndMap,
nullptr,
nullptr,
};
class TidalTrackRequest::ResponseParser final : public YajlResponseParser {
enum class State {
NONE,
URLS,
} state = State::NONE;
std::string url;
public:
explicit ResponseParser() noexcept
:YajlResponseParser(&parse_callbacks, nullptr, this) {}
std::string &&GetUrl() {
if (url.empty())
throw std::runtime_error("No url in track response");
return std::move(url);
}
/* yajl callbacks */
bool String(StringView value) noexcept;
bool MapKey(StringView value) noexcept;
bool EndMap() noexcept;
};
static std::string
MakeTrackUrl(const char *base_url, const char *track_id,
const char *audioquality) noexcept
{
return std::string(base_url)
+ "/tracks/"
+ track_id
+ "/urlpostpaywall?assetpresentation=FULL&audioquality="
+ audioquality + "&urlusagemode=STREAM";
}
TidalTrackRequest::TidalTrackRequest(CurlGlobal &curl,
const char *base_url, const char *token,
const char *session,
const char *track_id,
const char *audioquality,
TidalTrackHandler &_handler)
:request(curl, MakeTrackUrl(base_url, track_id, audioquality).c_str(),
*this),
handler(_handler)
{
request_headers.Append((std::string("X-Tidal-Token:")
+ token).c_str());
request_headers.Append((std::string("X-Tidal-SessionId:")
+ session).c_str());
request.SetOption(CURLOPT_HTTPHEADER, request_headers.Get());
}
TidalTrackRequest::~TidalTrackRequest() noexcept
{
request.StopIndirect();
}
std::unique_ptr<CurlResponseParser>
TidalTrackRequest::MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers)
{
if (status != 200)
return std::make_unique<TidalErrorParser>(status, headers);
auto i = headers.find("content-type");
if (i == headers.end() || i->second.find("/json") == i->second.npos)
throw std::runtime_error("Not a JSON response from Tidal");
return std::make_unique<ResponseParser>();
}
void
TidalTrackRequest::FinishParser(std::unique_ptr<CurlResponseParser> p)
{
assert(dynamic_cast<ResponseParser *>(p.get()) != nullptr);
auto &rp = (ResponseParser &)*p;
handler.OnTidalTrackSuccess(rp.GetUrl());
}
void
TidalTrackRequest::OnError(std::exception_ptr e) noexcept
{
handler.OnTidalTrackError(e);
}
inline bool
TidalTrackRequest::ResponseParser::String(StringView value) noexcept
{
switch (state) {
case State::NONE:
break;
case State::URLS:
if (url.empty())
url.assign(value.data, value.size);
break;
}
return true;
}
inline bool
TidalTrackRequest::ResponseParser::MapKey(StringView value) noexcept
{
if (value.Equals("urls"))
state = State::URLS;
else
state = State::NONE;
return true;
}
inline bool
TidalTrackRequest::ResponseParser::EndMap() noexcept
{
state = State::NONE;
return true;
}
|