xref: /MusicPlayer2/MusicPlayer2/InternetCommon.h (revision 9b32c1f65086ce3ea42d441b2f3350b622a575e7)
1 #pragma once
2 #include "Common.h"
3 
4 enum DownloadResult     //下载结果
5 {
6     DR_SUCCESS,             //成功
7     DR_NETWORK_ERROR,       //网络连接失败
8     DR_DOWNLOAD_ERROR       //下载失败
9 };
10 
11 #define  NORMAL_CONNECT INTERNET_FLAG_KEEP_CONNECTION
12 #define  SECURE_CONNECT NORMAL_CONNECT | INTERNET_FLAG_SECURE
13 #define  NORMAL_REQUEST INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE
14 #define  SECURE_REQUEST NORMAL_REQUEST | INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID
15 
16 class CInternetCommon
17 {
18 public:
19     enum HttpResult
20     {
21         SUCCESS = 0,	// 操作成功
22         FAILURE = 1,	// 操作失败
23         OUTTIME = 2,	// 操作超时
24     };
25 
26 	//一个搜索结果的信息
27 	struct ItemInfo
28 	{
29 		wstring id;		//歌曲的ID
30 		wstring title;		//歌曲的标题
31 		wstring artist;		//歌曲的艺术家
32 		wstring album;		//歌曲的唱片集
33         int duration{};     //时长
34 	};
35 
36 	CInternetCommon();
37 	~CInternetCommon();
38 
39 	//将一个字符串转换成URL编码(以UTF8编码格式)
40 	static wstring URLEncode(const wstring& wstr);
41 
42 	static bool GetURL(const wstring& str_url, wstring& result, bool custom_ua = false, bool allow_other_codes = false);
43     //向指定的url发送http post请求,结果保存在result中
44     static int HttpPost(const wstring& str_url, wstring& result);
45     //向指定的url发送http post请求,结果保存在result中
46     static int HttpPost(const wstring& str_url, wstring& result, const string& body, wstring& headers, bool custom_ua = false);
47     //向指定的url发送http post请求,结果保存在result中
48     static int HttpPost(const wstring& str_url, wstring& result, const wstring& body, wstring& headers, bool custom_ua = false);
49 
50 	static void DeleteStrSlash(wstring& str);		//如果字符串中的“\"”,删除字符串中的反斜杠
51 	static void DisposeSearchResult(vector<ItemInfo>& down_list, const wstring& search_result, int result_count = 30);		//从搜索结果search_result中提取出歌曲的信息,并保存在down_list容器里
52 
53 	//判断两个字符的匹配度
54 	static double CharacterSimilarDegree(wchar_t ch1, wchar_t ch2);
55 
56 	/// <summary>
57 	/// 字符串相似度算法-编辑距离法
58 	/// </summary>
59 	/// <returns>返回的值为0~1,越大相似度越高</returns>
60 	static double StringSimilarDegree_LD(const wstring& srcString, const wstring& matchString);
61 
62 	//根据参数提供的歌曲标题、艺术家、唱片集和文件名,在down_list容器中查找最匹配的一项,并返回索引的值
63 	static int SelectMatchedItem(const vector<ItemInfo>& down_list, const wstring& title, const wstring& artist, const wstring& album, const wstring& filename, bool write_log = false);
64 
65 	//自动搜索歌曲并返回最佳匹配项的ID,如果message为true,则会在失败时弹出提示
66 	static ItemInfo SearchSongAndGetMatched(const wstring& title, const wstring& artist, const wstring& album, const wstring& file_name, bool message = true, DownloadResult* result = nullptr);
67 };
68 
69