-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyinstants.cpp
More file actions
224 lines (195 loc) · 7.82 KB
/
Copy pathmyinstants.cpp
File metadata and controls
224 lines (195 loc) · 7.82 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
#include "myinstants.h"
#include "import_ffmpeg.h"
#include "httplib.h"
#include <cstdio>
#include <filesystem>
#include <windows.h>
#include <mmsystem.h>
#include <thread>
#include <atomic>
static std::string decode_html_entities(const std::string& s) {
struct Entity { const char* seq; size_t len; char ch; };
static const Entity table[] = {
{"'", 6, '\''}, {"/", 6, '/'}, {""", 6, '"'},
{"<", 6, '<'}, {">", 6, '>'}, {"&", 6, '&'},
{"'", 6, '\''}, {""", 6, '"'}, {"&", 5, '&'},
{"'", 5, '\''}, {""", 5, '"'}, {"&", 5, '&'},
{"<", 5, '<'}, {">", 5, '>'},
{"<", 4, '<'}, {">", 4, '>'},
};
std::string out;
out.reserve(s.size());
for (size_t i = 0; i < s.size(); i++) {
if (s[i] == '&') {
bool found = false;
for (const auto& e : table) {
if (s.compare(i, e.len, e.seq) == 0) {
out += e.ch;
i += e.len - 1;
found = true;
break;
}
}
if (!found) out += s[i];
} else {
out += s[i];
}
}
return out;
}
static std::atomic<bool> g_preview_playing{false};
static std::string g_preview_wav;
static std::string get_temp_dir() {
char buf[MAX_PATH];
GetTempPathA(MAX_PATH, buf);
return std::string(buf) + "JamBoard_Preview";
}
static std::string safe_filename(const std::string& name) {
std::string safe;
for (char c : name) {
if (c == '<' || c == '>' || c == ':' || c == '"' || c == '/' || c == '\\' || c == '|' || c == '?' || c == '*')
safe += '_';
else
safe += c;
}
return safe.empty() ? "preview" : safe;
}
std::vector<MyInstant> myinstants_search(const std::string& query) {
std::vector<MyInstant> results;
httplib::SSLClient cli("www.myinstants.com");
cli.set_connection_timeout(10);
cli.set_read_timeout(15);
cli.set_default_headers({
{"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{"Accept-Language", "en-US,en;q=0.5"},
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"}
});
auto res = cli.Get("/en/search/?name=" + query);
if (!res || res->status != 200) return results;
const std::string& html = res->body;
std::string::size_type pos = 0;
while (true) {
auto instant_pos = html.find("<div class=\"instant\">", pos);
if (instant_pos == std::string::npos) break;
pos = instant_pos + 21;
auto block_end = html.find("<div class=\"instant\">", pos);
if (block_end == std::string::npos) block_end = html.size();
std::string block = html.substr(instant_pos, block_end - instant_pos);
std::string mp3_url;
auto play_pos = block.find("play('");
if (play_pos != std::string::npos) {
auto start = play_pos + 6;
auto end = block.find("'", start);
if (end != std::string::npos) {
std::string path = block.substr(start, end - start);
if (path.find(".mp3") != std::string::npos || path.find(".wav") != std::string::npos)
mp3_url = "https://www.myinstants.com" + path;
}
}
std::string name;
auto link_pos = block.find("instant-link");
if (link_pos != std::string::npos) {
auto gt = block.find(">", link_pos);
if (gt != std::string::npos) {
auto close = block.find("</a>", gt);
if (close != std::string::npos) {
name = block.substr(gt + 1, close - gt - 1);
while (!name.empty() && (name.front() == ' ' || name.front() == '\n' || name.front() == '\r' || name.front() == '\t'))
name.erase(name.begin());
while (!name.empty() && (name.back() == ' ' || name.back() == '\n' || name.back() == '\r' || name.back() == '\t'))
name.pop_back();
name = decode_html_entities(name);
}
}
}
if (!name.empty() && !mp3_url.empty()) {
MyInstant item;
item.name = name;
item.mp3_url = mp3_url;
results.push_back(item);
}
if (results.size() >= 20) break;
}
return results;
}
bool myinstants_download(const MyInstant& item, const std::string& out_dir) {
std::string host = "www.myinstants.com";
std::string path = item.mp3_url;
auto scheme_end = item.mp3_url.find("://");
if (scheme_end != std::string::npos) {
auto host_start = scheme_end + 3;
auto path_start = item.mp3_url.find("/", host_start);
if (path_start != std::string::npos) {
host = item.mp3_url.substr(host_start, path_start - host_start);
path = item.mp3_url.substr(path_start);
}
}
httplib::SSLClient cli(host);
cli.set_connection_timeout(10);
cli.set_read_timeout(30);
cli.set_default_headers({
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
});
auto res = cli.Get(path);
if (!res || res->status != 200) return false;
std::string safe = safe_filename(item.name);
std::string ext = ".mp3";
auto dot_pos = item.mp3_url.rfind('.');
if (dot_pos != std::string::npos) {
std::string url_ext = item.mp3_url.substr(dot_pos);
if (url_ext.size() <= 5) ext = url_ext;
}
std::string out_path = out_dir + "\\" + safe + ext;
FILE* fp = fopen(out_path.c_str(), "wb");
if (!fp) return false;
fwrite(res->body.data(), 1, res->body.size(), fp);
fclose(fp);
return true;
}
void myinstants_preview(const MyInstant& item) {
if (g_preview_playing) return;
g_preview_playing = true;
std::string temp_dir = get_temp_dir();
std::filesystem::create_directories(temp_dir);
std::string safe = safe_filename(item.name);
std::thread([item, temp_dir, safe]() {
// Download MP3 to temp
std::string host = "www.myinstants.com";
std::string path = item.mp3_url;
auto scheme_end = item.mp3_url.find("://");
if (scheme_end != std::string::npos) {
auto host_start = scheme_end + 3;
auto path_start = item.mp3_url.find("/", host_start);
if (path_start != std::string::npos) {
host = item.mp3_url.substr(host_start, path_start - host_start);
path = item.mp3_url.substr(path_start);
}
}
httplib::SSLClient cli(host);
cli.set_connection_timeout(10);
cli.set_read_timeout(30);
cli.set_default_headers({
{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
});
auto res = cli.Get(path);
if (!res || res->status != 200) { g_preview_playing = false; return; }
std::string mp3_path = temp_dir + "\\" + safe + ".mp3";
FILE* fp = fopen(mp3_path.c_str(), "wb");
if (!fp) { g_preview_playing = false; return; }
fwrite(res->body.data(), 1, res->body.size(), fp);
fclose(fp);
// Convert to WAV
std::string wav_path = temp_dir + "\\" + safe + ".wav";
ffmpeg_convert_audio(mp3_path, wav_path);
// Play
g_preview_wav = wav_path;
PlaySoundA(wav_path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
// Wait for playback to finish (estimate: file size / 176400 bytes/sec for 44.1kHz stereo 16-bit)
// Then clean up the MP3
Sleep(500);
DeleteFileA(mp3_path.c_str());
// Auto-stop flag after a reasonable time
for (int i = 0; i < 30 && g_preview_playing; i++) Sleep(100);
g_preview_playing = false;
}).detach();
}