1
已解决
薛乘志
初级启示者
初级启示者
可以下载**云上的音乐
源码及程序下载地址:https://github.com/xgugugu/neteasemusic/releases/tag/1
请按图中配置编译参数及语言标准:
代码参考(注意,该代码不包含使用的c++库,无法直接编译,请下载源码包):
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <urlmon.h>
#include <mmsystem.h>
#include "configor/json.hpp"
#pragma comment (lib, "winmm.lib")
#pragma comment (lib, "urlmon.lib")
using namespace std;
using namespace configor;
void gotoxy(short x, short y) {
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void hideC() {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(handle, &CursorInfo);
}
string ToUtf8(const char *strAnsi) {
UINT nLen = MultiByteToWideChar(936, 0, strAnsi, -1, NULL, 0);
WCHAR *wszBuffer = new WCHAR[nLen + 1];
nLen = MultiByteToWideChar(936, 0, strAnsi, -1, wszBuffer, nLen);
wszBuffer[nLen] = 0;
nLen = WideCharToMultiByte(CP_UTF8, 0, wszBuffer, -1, NULL, 0, NULL, NULL);
CHAR *szBuffer = new CHAR[nLen + 1];
nLen = WideCharToMultiByte(CP_UTF8, 0, wszBuffer, -1, szBuffer, nLen, NULL, NULL);
szBuffer[nLen] = 0;
string s = szBuffer;
delete []wszBuffer;
delete []szBuffer;
return s;
}
string ToAnsi(const char *szU8) {
int wcsLen = MultiByteToWideChar(CP_UTF8, 0, szU8, strlen(szU8), 0, 0);
wchar_t* wszMultiByte = new wchar_t[wcsLen + 1];
MultiByteToWideChar(CP_UTF8, 0, szU8, strlen(szU8), wszMultiByte, wcsLen);
wszMultiByte[wcsLen] = '\0';
int ansiLen = WideCharToMultiByte(CP_ACP, 0, wszMultiByte, wcslen(wszMultiByte), 0, 0, 0, 0);
char* szAnsi = new char[ansiLen + 1];
szAnsi[ansiLen] = '\0';
WideCharToMultiByte(CP_ACP, 0, wszMultiByte, wcslen(wszMultiByte), szAnsi, ansiLen, 0, 0);
string s = szAnsi;
delete []szAnsi;
delete []wszMultiByte;
return s;
}
string NetHex(const char *webFileName) {
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char *pString = new char[strlen(webFileName)*sizeof(TCHAR) * 3];
strcpy(pString, ToUtf8(webFileName).c_str());
int nLength = strlen(pString);
char pszEncode[2048];
ZeroMemory(pszEncode, 2048);
int pos = 0;
for (int i = 0; i < nLength; i++) {
unsigned char c = pString[i];
if (c > 0x20 && c < 0x7f) {
pszEncode[pos] = c;
pos++;
} else if (c == 0x20) {
pszEncode[pos] = '+';
pos++;
} else {
pszEncode[pos] = '%';
pos++;
pszEncode[pos] = hex[c / 16];
pos++;
pszEncode[pos] = hex[c % 16];
pos++;
}
}
delete[] pString;
return pszEncode;
}
bool download(string url, string filename) {
HRESULT ret = URLDownloadToFileA(NULL, NetHex(url.c_str()).c_str(), filename.c_str(), 0, NULL);
if (ret != S_OK) {
return false;
} else {
return true;
}
}
bool download_progress(string url, string filename) {
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText) {
if (ulProgressMax != 0) {
double percentage = ulProgress * 1.0 / ulProgressMax * 100;
gotoxy(0, 0);
printf("进度:%.2f%%", percentage);
}
return S_OK;
}
};
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
HRESULT ret = URLDownloadToFileA(NULL, NetHex(url.c_str()).c_str(), filename.c_str(), 0, static_cast<IBindStatusCallback*>(&progress));
if (ret != S_OK) {
return false;
} else {
return true;
}
}
void print(string str, int len) {
for (int i = 0; i < len; i++) {
if (i >= str.size()) {
cout << " ";
} else {
if (str[i + 1] >= 128 || str[i + 1] < 0) {
cout << str[i] << str[i + 1];
i++;
} else {
cout << str[i];
}
}
}
}
string mname[105], artist[105], id[105];
int songid;
void getmusic(string idm) {
system("cls");
cout << "正在获取音乐地址...\n";
string link = "http://music.163.com/api/song/enhance/player/url?id=" + idm + "&ids=[" + idm + "]&br=3200000";
while (!download(link, "temp"));
ifstream jin("temp");
json j;
jin >> j;
jin.close();
system("cls");
string url = j["data"][0]["url"];
while (!download_progress(url, (string("[" + idm + "] " + mname[songid] + ".mp3")).c_str()));
system("cls");
cout << "下载完成!\n";
system("pause");
}
void search() {
system("cls");
cout << "输入搜索关键词:\n";
string name;
cin >> name;
system("cls");
cout << "获取数据中...\n";
string link = "http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s=" + name + " &type=1&offset=0&total=true&limit=20";
while (!download(link, "temp"));
ifstream jin("temp");
json j;
jin >> j;
jin.close();
for (int i = 0; i < j["result"]["songs"].size(); i++) {
mname[i] = ToAnsi(j["result"]["songs"][i]["name"].as_string().c_str());
artist[i] = ToAnsi(j["result"]["songs"][i]["artists"][0]["name"].as_string().c_str());
id[i] = j["result"]["songs"][i]["id"].as_string();
}
system("cls");
cout << "搜索结果(" << j["result"]["songs"].size() << "个):\n";
for (int i = 0; i < j["result"]["songs"].size(); i++) {
print(to_string(i), 3);
cout << "|";
print(mname[i], 50);
cout << "|" << artist[i] << endl;
}
cout << "选择:";
cin >> songid;
getmusic(id[songid]);
}
int main() {
system("**云音乐 1.0");
search();
return 0;
}
薛乘志在2021-12-15 19:56:29追加了内容
**=网 易
什么时候这也是屏蔽词了