Guest User

Download m3u songs

a guest
Jun 14th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. var text = File.ReadAllText(@"e:\kefirPls.m3u", Encoding.GetEncoding(1251));
  2. Func<string,string> getFileName = s =>
  3. {
  4.     foreach (char c in Path.GetInvalidFileNameChars().Concat(Path.GetInvalidPathChars()).Concat(new[]{'.'}))
  5.     {
  6.         s = s.Replace(c.ToString(), "");
  7.     }
  8.     return s.Length > 100 ? s.Substring(0, 100) : s;
  9. };
  10.  
  11. text.Substring(0, 200).Dump();
  12.  
  13. var fileInfos = Regex.Matches(text, "#EXTINF:[0-9]+,(.*?)\n(.*)")
  14. .Cast<Match>()
  15. .Select(m=>new{Title=m.Groups[1].Value, Url=m.Groups[2].Value, FileName=getFileName(m.Groups[1].Value) + ".mp3"});
  16.  
  17. foreach(var f in fileInfos)
  18. {
  19.     f.Dump();
  20.     var targetFile = Path.Combine(@"e:\KefirPls", f.FileName);
  21.     if (!File.Exists(targetFile))
  22.     {
  23.         new WebClient().DownloadFile(f.Url, targetFile);
  24.     }
  25. }
Add Comment
Please, Sign In to add comment