-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramrepository.cpp
More file actions
352 lines (281 loc) · 10.9 KB
/
programrepository.cpp
File metadata and controls
352 lines (281 loc) · 10.9 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright (C) 2009–2014 jakago
Copyright (C) 2018–2026 CSReviser Team
This file is part of CaptureStream2, a recorder that supports HLS for
NHK radio language courses.
CaptureStream2 is a modified version of CaptureStream, originally
developed by jakago.
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, see <https://www.gnu.org/licenses/gpl-2.0.html>.
*/
#include "programrepository.h"
#include "utility.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QEventLoop>
//#include <QNetworkAccessManager>
#include <QNetworkReply>
ProgramRepository& ProgramRepository::instance()
{
static ProgramRepository repo;
return repo;
}
ProgramRepository::ProgramRepository()
{
}
void ProgramRepository::updatePrograms()
{
m_pendingRequests = 0; // new_arrivals の 1 リクエスト
m_pendingRequests++;
QStringList kozaList = { "まいにちイタリア語", "まいにちスペイン語",
"まいにちドイツ語", "まいにちフランス語",
"まいにちロシア語" };
// QNetworkAccessManager* manager = new QNetworkAccessManager(this);
const QUrl url("https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/corners/new_arrivals");
QNetworkReply* reply = m_client.get(url);
connect(reply, &QNetworkReply::finished, this, [=]() {
QByteArray response_data = reply->readAll();
reply->deleteLater();
QJsonDocument jsonResponse = QJsonDocument::fromJson(response_data);
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["corners"].toArray();
for (const QJsonValue& value : jsonArray) {
QJsonObject objxx = value.toObject();
QString title = objxx["title"].toString();
QString corner_name = objxx["corner_name"].toString();
QString series_site_id = objxx["series_site_id"].toString();
QString corner_site = objxx["corner_site_id"].toString();
QString thumbnail_url = objxx["thumbnail_url"].toString();
QString program_name = formatProgramName(title, corner_name);
QString url_id = series_site_id + "_" + corner_site;
id_map.insert(url_id, program_name);
name_map.insert(program_name, url_id);
thumbnail_map.insert(url_id, thumbnail_url);
}
fetchKozaSeries(kozaList);
checkIfAllRequestsFinished();
});
/*
QNetworkRequest request(url);
QNetworkReply* reply = manager->get(request);
connect(reply, &QNetworkReply::finished, this, [=]() {
QByteArray response_data = reply->readAll();
reply->deleteLater();
QJsonDocument jsonResponse = QJsonDocument::fromJson(response_data);
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["corners"].toArray();
for (const QJsonValue& value : jsonArray) {
QJsonObject objxx = value.toObject();
QString title = objxx["title"].toString();
QString corner_name = objxx["corner_name"].toString();
QString series_site_id = objxx["series_site_id"].toString();
QString corner_site = objxx["corner_site_id"].toString();
QString thumbnail_url = objxx["thumbnail_url"].toString();
QString program_name = formatProgramName(title, corner_name);
QString url_id = series_site_id + "_" + corner_site;
id_map.insert(url_id, program_name);
name_map.insert(program_name, url_id);
thumbnail_map.insert(url_id, thumbnail_url);
}
fetchKozaSeries(kozaList);
checkIfAllRequestsFinished();
*/
}
void ProgramRepository::fetchKozaSeries(const QStringList& kozaList)
{
for (const QString& koza : kozaList) {
if (!name_map.contains(koza)) continue;
m_pendingRequests++; // 追加の HTTP リクエスト
QString url = name_map[koza];
int l = url.length() != 13 ? url.length() - 3 : 10;
QString fullUrl = "https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=" +
url.left(l) + "&corner_site_id=" + url.right(2);
// QNetworkAccessManager* manager = new QNetworkAccessManager(this);
// QNetworkRequest request(fullUrl);
// QNetworkReply* reply = manager->get(request);
QNetworkReply* reply = m_client.get(QUrl(fullUrl));
connect(reply, &QNetworkReply::finished, this, [=]() {
QByteArray response_data = reply->readAll();
reply->deleteLater();
QJsonDocument jsonResponse = QJsonDocument::fromJson(response_data);
QJsonObject jsonObject = jsonResponse.object();
QJsonArray jsonArray = jsonObject["episodes"].toArray();
for (const QJsonValue& value : jsonArray) {
QJsonObject objxx = value.toObject();
QString file_title = objxx["program_title"].toString();
QString temp1, temp2;
if (file_title.contains("入門編")) {
temp1 = koza + "【入門編】";
temp2 = url.left(l) + "_x1";
}
if (file_title.contains("初級編")) {
temp1 = koza + "【初級編】";
temp2 = url.left(l) + "_x1";
}
if (file_title.contains("応用編")) {
temp1 = koza + "【応用編】";
temp2 = url.left(l) + "_y1";
}
if (file_title.contains("中級編")) {
temp1 = koza + "【中級編】";
temp2 = url.left(l) + "_y1";
}
if (!temp1.isEmpty() && !temp2.isEmpty()) {
name_map.insert(temp1, temp2);
id_map.insert(temp2, temp1);
}
}
// emit programListUpdated();
checkIfAllRequestsFinished();
});
}
}
void ProgramRepository::start()
{
if (m_started)
return;
m_started = true;
updatePrograms(); // ← 今まで呼んでいたやつ
}
bool ProgramRepository::waitUntilReady()
{
if (m_ready)
return true;
QEventLoop loop;
QObject::connect(this, &ProgramRepository::programListUpdated,
&loop, &QEventLoop::quit);
loop.exec();
return m_ready;
}
bool ProgramRepository::isReady() const
{
return m_ready;
}
void ProgramRepository::checkIfAllRequestsFinished()
{
m_pendingRequests--;
if (m_pendingRequests == 0) {
m_ready = true;
emit programListUpdated();
}
}
QString ProgramRepository::getProgramNameById(const QString& url)
{
// キャッシュ優先
if (id_map.contains(url))
return id_map.value(url);
// 未取得なら取得
QString json = fetchProgramJson(url);
if (json.isEmpty())
return QString();
auto [title, corner] = parseProgramJson(json);
QString name = formatProgramName(title, corner);
id_map.insert(url, name);
name_map.insert(name, url);
return name;
}
QString ProgramRepository::fetchProgramJson(const QString& url)
{
int l = (url.length() != 13) ? url.length() - 3 : 10;
QString api =
"https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=" +
url.left(l) + "&corner_site_id=" + url.right(2);
int timer = 100;
for (int i = 0; i < 20; ++i) {
QByteArray res = m_client.getSync(QUrl(api), timer, 1);
if (!res.isEmpty())
return QString::fromUtf8(res);
if (timer < 500) timer += 50;
else if (timer < 5000) timer += 100;
}
return QString();
}
/*
QString ProgramRepository::fetchProgramJson(const QString& url)
{
int l = (url.length() != 13) ? url.length() - 3 : 10;
QString api =
"https://www.nhk.or.jp/radio-api/app/v1/web/ondemand/series?site_id=" +
url.left(l) + "&corner_site_id=" + url.right(2);
int timer = 100;
for (int i = 0; i < 20; ++i) {
QString res = Utility::getJsonFile(api, timer);
if (res != "error")
return res;
if (timer < 500) timer += 50;
else if (timer < 5000) timer += 100;
}
return QString();
}
*/
std::tuple<QString, QString>
ProgramRepository::parseProgramJson(const QString& str)
{
QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8());
QJsonObject obj = doc.object();
QString title = obj["title"].toString().replace(" ", " ");
QString corner = obj["corner_name"]
.toString()
.remove("を聴く")
.replace(" ", " ");
return { title, corner };
}
QString ProgramRepository::formatProgramName(
const QString& title,
const QString& corner_name)
{
QString attribute = buildBaseProgramName(title, corner_name);
normalizeWidth(attribute);
// attribute = normalizeProgramName(attribute);
return attribute;
}
void ProgramRepository::normalizeWidth(QString& s)
{
for (ushort i = 0xFF1A; i < 0xFF5F; ++i)
s.replace(QChar(i), QChar(i - 0xFEE0));
for (ushort i = 0xFF10; i < 0xFF1A; ++i)
s.replace(QChar(i - 0xFEE0), QChar(i));
}
QString ProgramRepository::buildBaseProgramName(
const QString& title,
const QString& corner_name)
{
QString attribute = title;
attribute.replace(" ", " ");
if (!corner_name.isEmpty()) {
if (corner_name.contains("曜日放送", Qt::CaseInsensitive) ||
corner_name.contains("曜放送", Qt::CaseInsensitive) ||
corner_name.contains("特集", Qt::CaseInsensitive)) {
attribute = title + "-" + corner_name;
} else {
attribute = corner_name;
}
}
return attribute;
}
QString ProgramRepository::normalizeProgramName(QString attribute)
{
normalizeWidth(attribute);
attribute.remove("【らじる文庫】");
attribute.remove("より");
attribute.remove("カルチャーラジオ ");
attribute.remove("【恋する朗読】");
attribute.remove("【ラジオことはじめ】");
attribute.remove("【生朗読!】");
attribute.remove("NHK高校講座");
attribute.remove("まいにち朗読");
attribute.replace(" 初級編", "【初級編】");
attribute.replace(" 入門編", "【入門編】");
attribute.replace(" 中級編", "【中級編】");
attribute.replace(" 応用編", "【応用編】");
return attribute;
}