using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
namespace MusicAPI
{
/// <summary>
/// 音乐搜索API
/// </summary>
///作者Q944095635
///类型:音乐搜索
public class MusicSearch
{
#region 实体类
/// <summary>
/// 搜搜音乐实体类
/// </summary>
public class SouSouMusic
{
/// <summary>
/// 歌曲名字
/// </summary>
public string name { get; set; }
/// <summary>
/// 歌曲mp3路径
/// </summary>
public List<string> mp3path { get; set; }
/// <summary>
/// 歌曲wma路径
/// </summary>
public List<string> wmapath { get; set; }
/// <summary>
/// 歌曲作者
/// </summary>
public string by { get; set; }
}
#endregion
#region 音乐搜索接口
/// <summary>
/// 搜搜接口
/// </summary>
/// <param name="KeyWord">关键字</param>
/// <param name="page">i=1,返回20条数据,i=2,返回40条</param>
/// 正则学的不好。Substring处理的数据,可以自行修改
public static List<SouSouMusic> SouSouSearch(string KeyWord,int pageindex)
{
List<SouSouMusic> list = new List<SouSouMusic>();
for (int id = 1; id <= pageindex; id++)
{
#region 数据处理
try
{
string strAPI = "http://cgi.music.soso.com/fcgi-bin/m.q?w=";
strAPI = strAPI + UrlEncode(KeyWord);//关键字编码转换
strAPI = strAPI + "&p=" + id;
Uri url = new Uri(strAPI);
WebClient MyWebClient = new WebClient();
string Web = MyWebClient.DownloadString(url);
while (Web.IndexOf("<td class=\"data\">") >= 0)
{
string Web1 = Web.Substring(Web.IndexOf("<td class=\"data\">") + 17, Web.Length - 18 - Web.IndexOf("<td class=\"data\">"));
//当前歌曲所以信息
string Web2 = Web1.Substring(0, Web1.IndexOf("</td>"));
Web = Web1;
int i = Web2.IndexOf("@@");
//音乐名字
string MusicName = Web2.Substring(Web2.IndexOf("@@") + 2, Web2.Length - Web2.IndexOf("@@") - 5);
MusicName = MusicName.Substring(0, MusicName.IndexOf("@@"));
//作者名字
string use = Web2.Substring(Web2.IndexOf("@@") + 2, Web2.Length - Web2.IndexOf("@@") - 3);
use = use.Substring(use.IndexOf("@@") + 2, use.Length - use.IndexOf("@@") - 3);
use = use.Substring(use.IndexOf("@@") + 2, use.Length - use.IndexOf("@@") - 3);
use = use.Substring(0, use.IndexOf("@@"));
//歌曲连接 分为MP3 和 WMA
List<string> mp3path = new List<string>();
List<string> wmapath = new List<string>();
try
{
while (Web2.IndexOf("@@FI") >= 0)
{
string path = Web2.Substring(Web2.IndexOf("@@FI") + 4, Web2.Length - Web2.IndexOf("@@FI") - 4);
Web2 = path;
path = path.Substring(0, path.IndexOf(".wma") + 4);
wmapath.Add(path);
}
while (Web2.IndexOf("SI") >= 0)
{
string path = Web2.Substring(Web2.IndexOf("SI") + 2, Web2.Length - Web2.IndexOf("SI") - 2);
Web2 = path;
path = path.Substring(0, path.IndexOf(".mp3") + 4);
mp3path.Add(path);
}
}
catch (Exception)
{
}
//int s = Web2.IndexOf("@@FI");
// int d = Web2.IndexOf(".wma");
SouSouMusic so = new SouSouMusic();
so.name = MusicName.Replace("@", "");
so.by = use.Replace("@", "");
so.mp3path = mp3path;
so.wmapath = wmapath;
list.Add(so);
}
}
catch (WebException)
{
}
#endregion
}
return list;
}
#endregion
#region 字符编码转化
/// <summary>
/// //字符编码转化
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.Default.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr, 16));
} return (sb.ToString());
}
#endregion
#region 音乐下载
/// <summary>
/// 下载音乐
/// </summary>
/// <param name="DownUrl"></param>
/// <param name="SavePath"></param>
/// <returns></returns>
public static bool DownMusic(string DownUrl,string SavePath)
{
try
{
Thread th = new Thread(new ParameterizedThreadStart(Down));
DownFileInfo d = new DownFileInfo(DownUrl,SavePath);
th.IsBackground = true;
th.Start(d);
}
catch (Exception)
{
return false;
}
return true;
}
/// <summary>
/// 线程
/// </summary>
private static void Down(object obj)
{
DownFileInfo d = (DownFileInfo)obj;
WebClient wb = new WebClient();
wb.DownloadFile(d.DownUrl, d.SavePath);
}
/// <summary>
/// 下载文件属性类
/// </summary>
public class DownFileInfo : Object
{
public DownFileInfo(string DownUrl, string SavePath)
{
this.SavePath = SavePath;
this.DownUrl = DownUrl;
}
public string SavePath { get; set; }
public string DownUrl { get; set; }
}
#endregion
}
}
cload 发表于 2014-5-10 14:27
不下载,但也要支持LZ!
欢迎光临 苏飞论坛 (http://www.sufeinet.com/) | Powered by Discuz! X3.4 |