From 5f86c834220b4b1630b40103a68466b9913846d6 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Tue, 16 Feb 2016 04:21:58 +0100 Subject: [PATCH] m3u pls and asx support (hopefuly) --- NadekoBot/Modules/Music.cs | 73 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/NadekoBot/Modules/Music.cs b/NadekoBot/Modules/Music.cs index 3eafec6e..23e99484 100644 --- a/NadekoBot/Modules/Music.cs +++ b/NadekoBot/Modules/Music.cs @@ -10,6 +10,7 @@ using Timer = System.Timers.Timer; using System.Threading.Tasks; using NadekoBot.Classes; using Discord.Audio; +using System.Text.RegularExpressions; namespace NadekoBot.Modules { class Music : DiscordModule { @@ -257,6 +258,15 @@ namespace NadekoBot.Modules { await e.Send("💢 You need to be in the voice channel on this server."); return; } + + if (string.IsNullOrWhiteSpace(query) || query.Length < 3) + return; + + if (IsRadioLink(query)) { + radio = true; + query = await HandleStreamContainers(query); + } + if (musicPlayers.ContainsKey(e.Server) == false) { float? vol = null; float throwAway; @@ -268,8 +278,6 @@ namespace NadekoBot.Modules { return; } } - if (query == null || query.Length < 3) - return; var player = musicPlayers[e.Server]; @@ -320,5 +328,66 @@ namespace NadekoBot.Modules { return; } } + + private bool IsRadioLink(string query) => + (query.StartsWith("http") || + query.StartsWith("ww")) + && + (query.Contains(".pls") || + query.Contains(".m3u") || + query.Contains(".asx")); + + private async Task HandleStreamContainers(string query) { + string file = null; + try { + file = await SearchHelper.GetResponseAsync(query); + } + catch { + return query; + } + if (query.Contains(".pls")) { + //File1=http://armitunes.com:8000/ + //Regex.Match(query) + try { + var m = Regex.Match(file, "File1=(?.*?)\\n"); + var res = m.Groups["url"]?.ToString(); + return res; + } + catch { + Console.WriteLine($"Failed reading .pls:\n{file}"); + return null; + } + } + else if (query.Contains(".m3u")) { + /* + # This is a comment + C:\xxx4xx\xxxxxx3x\xx2xxxx\xx.mp3 + C:\xxx5xx\x6xxxxxx\x7xxxxx\xx.mp3 + */ + try { + var m = Regex.Match(file, "^[^#](?.*)"); + var res = m.Groups["url"]?.ToString(); + return res; + } + catch { + Console.WriteLine($"Failed reading .m3u:\n{file}"); + return null; + } + + } + else if (query.Contains(".asx")) { + // + try { + var m = Regex.Match(file, ".*?)\""); + var res = m.Groups["url"]?.ToString(); + return res; + } + catch { + Console.WriteLine($"Failed reading .asx:\n{file}"); + return null; + } + } + return query; + } } }