public static List <string[]> DownloadYoutube(string cmd, string sender, MatrixRoom room) { JObject[] videos = Downloaders.YoutubeGetData(cmd); List <string[]> output = new List <string[]>(videos.Length); List <Task> tasks = new List <Task> (); foreach (JObject data in videos) { //Check Length int seconds = data["duration"].ToObject <int>(); int max = int.Parse(Configuration.Config["youtube"]["maxlength"]); if (seconds > max) { throw new Exception("Video exceeds duration limit of " + Math.Round(max / 60f, 1) + " minutes"); } string filename = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(data["fulltitle"].ToObject <string>())); Task t = new Task(() => { Downloaders.YoutubeDownload(data["webpage_url"].ToObject <string>(), filename); }); t.Start(); tasks.Add(t); output.Add(new string[2] { filename + ".ogg", data["title"].ToObject <string>() }); } System.Threading.Tasks.Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(20 * videos.Length)); return(output); }
public static void SearchYTForTrack(string cmd, string sender, MatrixRoom room) { string query = cmd.Replace("search ", ""); if (string.IsNullOrWhiteSpace(query)) { return; } try { string url = Downloaders.GetYoutubeURLFromSearch(query); if (url != null) { DownloadTrack(url, sender, room); } else { throw new Exception("No videos matching those terms were found"); } } catch (Exception e) { room.SendNotice("There was an issue with that request, " + sender + ": " + e.Message); Console.Error.WriteLine(e); } }
public static void DownloadTrack(string cmd, string sender, MatrixRoom room) { try { List <string[]> videos = new List <string[]>(); if (Downloaders.YoutubeGetIDFromURL(cmd) != "") { videos = DownloadYoutube(cmd, sender, room); } else if (Uri.IsWellFormedUriString(cmd, UriKind.Absolute)) { videos = new List <string[]>(); videos.Add(DownloadGeneric(cmd, sender, room)); } else { room.SendNotice("Sorry, that type of URL isn't supported right now :/"); return; } Program.MPCClient.RequestLibraryUpdate(); //Program.MPCClient.Idle("update");//Wait for it to start Program.MPCClient.Idle("update"); //Wait for it to finish foreach (string[] res in videos) { Program.MPCClient.AddFile(res[0]); } Program.MPCClient.Status(); #if DEBUG Console.WriteLine(JObject.FromObject(Program.MPCClient.lastStatus)); #endif int position = Program.MPCClient.lastStatus.playlistlength; if (position == 1) { Program.MPCClient.Play(); room.SendNotice("Started playing " + videos[0][1] + " | " + Configuration.Config["mpc"]["streamurl"]); } else { room.SendNotice(videos[0][1] + " has been queued at position " + position + "."); } } catch (Exception e) { room.SendNotice("There was an issue with that request, " + sender + ": " + e.Message); Console.Error.WriteLine(e); } }
public static void LyricSearch(string cmd, string sender, MatrixRoom room) { string suggestion = Downloaders.GetSongNameByLyric(cmd.Replace("lyrics ", "")); if (suggestion == null) { room.SendNotice("I couldn't find any songs with that lyric :("); } else { room.SendNotice(String.Format("Matched '{0}'. Checking Youtube for it", suggestion)); SearchYTForTrack("search " + suggestion, sender, room); } }
public static string[] DownloadGeneric(string cmd, string sender, MatrixRoom room) { Uri uri; if (!Uri.TryCreate(cmd, UriKind.Absolute, out uri)) { throw new Exception("Not a url :("); } FileInfo info = new FileInfo(uri.Segments.Last()); string filename = Convert.ToBase64String(Encoding.UTF8.GetBytes(info.Name)) + info.Extension; Downloaders.GenericDownload(cmd, filename); return(new string[2] { filename, uri.Segments.Last() }); }