📄 m3uhandler.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using System.Threading;
namespace M3UDownloader
{
public class m3uHandler
{
struct Song
{
public String name;
public String url;
}
StreamReader m3ufile;
List<Song> mp3FileList = new List<Song>();
Song song;
// Define the delegate and event
public delegate void DownloadHandler(String name, int TotalSize, int Downloaded);
public event DownloadHandler CompleteEvent;
public m3uHandler(String m3uName)
{
m3ufile = new StreamReader(m3uName, Encoding.GetEncoding("gb2312"));
}
public void parse()
{
String str;
Regex srhMp3 = new Regex(@"mp3$");
Regex srhTitle = new Regex(@"^#EXTINF:");
Match match;
// Read m3u header tag
if ((str = m3ufile.ReadLine()) != null)
{
if (!str.Equals("#EXTM3U"))
return;
}
while ((str = m3ufile.ReadLine()) != null)
{
match = srhTitle.Match(str);
if (match.Success)
{
song.name = str.Substring(match.Length);
}
match = srhMp3.Match(str);
if (match.Success)
{
song.url = str;
mp3FileList.Add(song);
}
}
}
public void download()
{
int count = mp3FileList.Count;
int index = 0;
do
{
saveToFile(mp3FileList[index].url, mp3FileList[index].name);
index = index + 1;
} while (index < count);
}
private void saveToFile(String mp3, string title)
{
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(mp3);
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
// Open a file to write
FileStream SaveStream = new FileStream(title + ".MP3", FileMode.OpenOrCreate, FileAccess.Write);
byte[] ByteArray = new byte[102400];
int bytes;
int Downloaded=0;
while ((bytes = resStream.Read(ByteArray, 0, ByteArray.Length)) > 0) // any data read?
{
// write in a file
SaveStream.Write(ByteArray, 0, bytes);
Downloaded += bytes;
Notify(title,(int) response.ContentLength,Downloaded);
}
resStream.Close();
SaveStream.Close();
}
public void Notify(String Mp3Name, int TotalSize, int Downloaded)
{
if (CompleteEvent != null)
{
CompleteEvent(Mp3Name,TotalSize,Downloaded);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -