示例#1
0
        public void Create_correct_parser_from_PlaylistType()
        {
            var playlistType = PlaylistType.M3U;
            var parser       = PlaylistParserFactory.GetPlaylistParser(playlistType);

            Assert.IsNotNull(parser);
            Assert.IsInstanceOfType(parser, typeof(M3uContent));
        }
示例#2
0
        public void Create_correct_parser_from_known_filetype()
        {
            var filetype = ".m3u";
            var parser   = PlaylistParserFactory.GetPlaylistParser(filetype);

            Assert.IsNotNull(parser);
            Assert.IsInstanceOfType(parser, typeof(M3uContent));
        }
示例#3
0
        public void RefreshList(string _url)
        {
            if (!string.IsNullOrEmpty(_url))
            {
                url = _url;
            }
            string contents;

            try
            {
                using (var wc = new WebClient())
                {
                    contents = wc.DownloadString(url);
                }

                var           parser   = PlaylistParserFactory.GetPlaylistParser(".m3u");
                IBasePlaylist playlist = parser.GetFromString(contents);
                M3uPlaylist   m3uList  = (M3uPlaylist)playlist;
                channelsInfo.Clear();
                groupsInfo.Clear();
                int channelNumber      = 0;
                SeenResumeChannels src = SeenResumeChannels.Get();
                foreach (M3uPlaylistEntry entry in m3uList.PlaylistEntries)
                {
                    if (entry.CustomProperties.Count > 0)
                    {
                        ChannelInfo channelInfo = new ChannelInfo(entry);
                        channelInfo.seen           = src.IsSeen(channelInfo.Title);
                        channelInfo.currentPostion = src.GetCurrentPosition(channelInfo.Title);
                        channelsInfo.Add(channelNumber, channelInfo);
                        channelInfo.ChNumber = channelNumber;
                        GrpInfo groupInfo = new GrpInfo();
                        groupInfo.Title = channelInfo.TVGGroup;
                        groupInfo.Show  = channelInfo.ChannelType == ChType.SHOW;
                        if (!groupsInfo.ContainsKey(groupInfo))
                        {
                            groupsInfo[groupInfo] = new List <ChannelInfo>();
                        }
                        groupsInfo[groupInfo].Add(channelInfo);
                        channelNumber++;
                    }
                }
                if (File.Exists(Utils.CONF_PATH + "channelCache.json"))
                {
                    File.Delete(Utils.CONF_PATH + "channelCache.json");
                }
                using (StreamWriter file = File.CreateText(Utils.CONF_PATH + "channelCache.json"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, channelsInfo.Values);
                }
                needRefresh = true;
                Task <string> stats = Utils.GetAsync("http://amian.es:5085/stats?ctype=connected&app=net&chn=CONNECT");
            } catch (Exception ex) {
                Console.WriteLine("Some error occur");
                needRefresh = false;
            }
        }
示例#4
0
        // This will be used in the future to allow for playlist types beyond M3U.
        // public virtual void LoadPlaylist() { }

        /// <summary>
        /// Load an M3U playlist.
        /// </summary>
        /// <returns>Returns a list of playlist information.</returns>
        /// <param name="userPlaylist">The user specified playlist path.</param>
        public List <string> LoadPlaylist(string userPlaylist)
        {
            var parser = PlaylistParserFactory.GetPlaylistParser(".m3u");

            // convert string to stream
            byte[]       byteArray = Encoding.UTF8.GetBytes(userPlaylist);
            MemoryStream stream    = new MemoryStream(byteArray);

            IBasePlaylist playlist = parser.GetFromStream(stream);

            return(playlist.GetTracksPaths());
        }
示例#5
0
        private async Task Cmd(string cmd)
        {
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.BackgroundColor = ConsoleColor.Black;
            if (cmd.StartsWith("vol "))
            {
                float.TryParse(cmd.Substring(4), out targetVolume);
                Console.Write("set volume to " + targetVolume.ToString());
                return;
            }
            if (cmd.StartsWith("pos"))
            {
                Console.Write("current pos is " + wasapiOut.WaveSource.GetPosition().ToString());
                return;
            }
            if (cmd.StartsWith("playlist "))
            {
                if (initialized)
                {
                    wasapiOut.Stop();
                    initialized = false;
                }
                string path       = cmd.Substring(9);
                bool   onComputer = true;
                if (!File.Exists(@path))
                {
                    onComputer = false;
                }
                Stream stream = Stream.Null;
                if (onComputer)
                {
                    stream = File.OpenRead(@path);
                }
                else
                {
                    try
                    {
                        HttpWebRequest  req      = (HttpWebRequest)WebRequest.Create(path);
                        HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                        stream = response.GetResponseStream();
                    } catch { }
                }
                if (stream == Stream.Null)
                {
                    Console.Write("couldn't read " + path);
                    return;
                }
                string extension = Path.GetExtension(path);
                IPlaylistParser <IBasePlaylist> parser = PlaylistParserFactory.GetPlaylistParser(extension);
                IBasePlaylist playlist = parser.GetFromStream(stream);
                foreach (string str in playlist.GetTracksPaths())
                {
                    currentStream = new Mp3WebStream(str, false);
                    ISampleSource source             = currentStream.ToSampleSource().AppendSource(x => new PitchShifter(x), out pitchShifter);
                    var           notificationSource = new SingleBlockNotificationStream(source);
                    notificationSource.SingleBlockRead += (s, a) =>
                    {
                        leftPitch  = Math.Abs(a.Left) * 10;
                        rightPitch = Math.Abs(a.Right) * 10;
                    };
                    currentStream = notificationSource.ToWaveSource();
                    currentPath   = path;
                    wasapiOut.Initialize(currentStream);
                    wasapiOut.Volume = 0.0f;
                    initialized      = true;
                }
                Console.Write("set playlist to " + path);
                return;
            }
            if (cmd.StartsWith("thread"))
            {
                string board = "a";
                if (cmd.Length > 6)
                {
                    board = cmd.Substring(7);
                }

                Dictionary <int, int> a_threads    = new Dictionary <int, int>();
                Dictionary <int, int> smug_threads = new Dictionary <int, int>();
                using (HttpClient a_client = new HttpClient())
                    using (HttpResponseMessage a_response = await a_client.GetAsync("https://8ch.net/" + board + "/catalog.html"))
                        using (HttpContent a_content = a_response.Content)
                        {
                            string soykaf = await a_content.ReadAsStringAsync();

                            string pattern = "data-reply=\"";
                            for (int i = 0; i < soykaf.Length - pattern.Length; ++i)
                            {
                                if (soykaf.Substring(i, pattern.Length) == pattern)
                                {
                                    int    replyCountEnd   = FindNext(soykaf.Substring(i + pattern.Length), "\"");
                                    string replyCount      = soykaf.Substring(i + pattern.Length, replyCountEnd);
                                    int    threadIdBegin   = i + pattern.Length + FindNext(soykaf.Substring(i + pattern.Length), "data-id=\"");
                                    string threadId        = soykaf.Substring(threadIdBegin + 9, FindNext(soykaf.Substring(threadIdBegin + 9), "\""));
                                    int    threadNameBegin = threadIdBegin + 9 + FindNext(soykaf.Substring(threadIdBegin + 9), "data-subject=\"");
                                    string threadName      = soykaf.Substring(threadNameBegin + 14, FindNext(soykaf.Substring(threadNameBegin + 14), "\""));

                                    if (FindNext(threadName.ToLower(), "r/a/dio") >= 0 || FindNext(threadName.ToLower(), "radio") >= 0)
                                    {
                                        int.TryParse(threadId, out int ID);
                                        int.TryParse(replyCount, out int REPLY);
                                        a_threads.Add(ID, REPLY);
                                    }
                                }
                            }
                        }
                Console.Write("got " + a_threads.Count + " r/a/dio thread" + (a_threads.Count > 1 ? "s" : "") + " from 8/" + board + "/");
                if (board == "a")
                {
                    using (HttpClient smug_client = new HttpClient())
                        using (HttpResponseMessage smug_response = await smug_client.GetAsync("https://smuglo.li/a/catalog.html"))
                            using (HttpContent smug_content = smug_response.Content)
                            {
                                string soykaf = await smug_content.ReadAsStringAsync();

                                string pattern = "data-reply=\"";
                                for (int i = 0; i < soykaf.Length - pattern.Length; ++i)
                                {
                                    if (soykaf.Substring(i, pattern.Length) == pattern)
                                    {
                                        int    replyCountEnd   = FindNext(soykaf.Substring(i + pattern.Length), "\"");
                                        string replyCount      = soykaf.Substring(i + pattern.Length, replyCountEnd);
                                        int    threadIdBegin   = i + pattern.Length + FindNext(soykaf.Substring(i + pattern.Length), "data-id=\"");
                                        string threadId        = soykaf.Substring(threadIdBegin + 9, FindNext(soykaf.Substring(threadIdBegin + 9), "\""));
                                        int    threadNameBegin = threadIdBegin + 9 + FindNext(soykaf.Substring(threadIdBegin + 9), "data-subject=\"");
                                        string threadName      = soykaf.Substring(threadNameBegin + 14, FindNext(soykaf.Substring(threadNameBegin + 14), "\""));

                                        if (FindNext(threadName.ToLower(), "r/a/dio") >= 0 || FindNext(threadName.ToLower(), "radio") >= 0)
                                        {
                                            if (int.TryParse(threadId, out int ID) && int.TryParse(replyCount, out int REPLY))
                                            {
                                                smug_threads.Add(ID, REPLY);
                                            }
                                        }
                                    }
                                }
                            }
                    Console.Write("\ngot " + smug_threads.Count + " r/a/dio thread" + (smug_threads.Count > 1 ? "s" : "") + " from the bunker");
                }
                Thread.Sleep(500);
                Console.Write("\nopening the most active thread(s)");
                Thread.Sleep(1000);
                foreach (var x in a_threads)
                {
                    Process.Start("https://8ch.net/a/res/" + x.Key + ".html");
                    break;
                }
                foreach (var x in smug_threads)
                {
                    Process.Start("https://smuglo.li/a/res/" + x.Key + ".html");
                    break;
                }
                return;
            }
            if (cmd.StartsWith("play"))
            {
                if (M3uCheck())
                {
                    return;
                }

                wasapiOut.Play();
                Console.Write("started playing");
                return;
            }
            if (cmd.StartsWith("stop"))
            {
                if (M3uCheck())
                {
                    return;
                }

                wasapiOut.Stop();
                Console.Write("stopped playing");
                return;
            }
            if (cmd.StartsWith("pause"))
            {
                if (M3uCheck())
                {
                    return;
                }

                wasapiOut.Pause();
                Console.Write("paused playing");
                return;
            }
            if (cmd.StartsWith("resume"))
            {
                if (M3uCheck())
                {
                    return;
                }

                wasapiOut.Resume();
                Console.Write("resumed playing");
                return;
            }
            if (cmd.StartsWith("help"))
            {
                Console.Write(HELP_MESSAGE);
                return;
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.BackgroundColor = ConsoleColor.Red;
            Console.Write("nANI!?");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Write("?");
            return;
        }
示例#6
0
        public void Throw_exception_when_create_parser_from_unknown_filetype()
        {
            var filetype = ".abc";

            Assert.ThrowsException <ArgumentException>(() => PlaylistParserFactory.GetPlaylistParser(filetype));
        }
        public void RefreshList(UrlObject _url)
        {
            if (_url != null && _url != url)
            {
                url = _url;
            }
            string contents;

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Get, _url.URL);
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
                request.Headers.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0");

                M3uPlaylist m3UList;
                var         parser = PlaylistParserFactory.GetPlaylistParser(".m3u");
                using (var client = new HttpClient())
                {
                    var response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
                    m3UList = (M3uPlaylist)parser.GetFromStream(response.Content.ReadAsStreamAsync().Result);
                }

                channelsInfo.Clear();
                groupsInfo.Clear();
                int channelNumber      = 0;
                SeenResumeChannels src = SeenResumeChannels.Get();
                foreach (M3uPlaylistEntry entry in m3UList.PlaylistEntries)
                {
                    if (entry.CustomProperties.Count > 0)
                    {
                        ChannelInfo channelInfo = new ChannelInfo(entry);
                        channelInfo.seen           = src.IsSeen(channelInfo.Title);
                        channelInfo.currentPostion = src.GetCurrentPosition(channelInfo.Title);
                        channelsInfo.Add(channelNumber, channelInfo);
                        channelInfo.ChNumber = channelNumber;
                        GrpInfo groupInfo = new GrpInfo();
                        groupInfo.Title = channelInfo.TVGGroup;
                        groupInfo.Show  = channelInfo.ChannelType == ChType.SHOW;
                        if (!groupsInfo.ContainsKey(groupInfo))
                        {
                            groupsInfo[groupInfo] = new List <ChannelInfo>();
                        }
                        groupsInfo[groupInfo].Add(channelInfo);
                        channelNumber++;
                    }
                }
                if (!Directory.Exists(Utils.CONF_PATH + "\\lists\\"))
                {
                    Directory.CreateDirectory(Utils.CONF_PATH + "\\lists\\");
                }
                if (File.Exists(Utils.CONF_PATH + "\\lists\\" + _url.Name + "_cache.json"))
                {
                    File.Delete(Utils.CONF_PATH + "\\lists\\" + _url.Name + "_cache.json");
                }
                using (StreamWriter file = File.CreateText(Utils.CONF_PATH + "\\lists\\" + _url.Name + "_cache.json"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, channelsInfo.Values);
                }
                needRefresh = true;
                Task <string> stats = Utils.GetAsync("http://amiansito.ddns.net:5087/stats?ctype=connected&app=net&chn=CONNECT");
            } catch (Exception ex) {
                Logger.Current.Error("Some error occur downloading the list: " + ex.Message.ToString());
                MessageBox.Show(
                    "Error: " + ex.Message + ". URL=" + url.URL,
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
                needRefresh = false;
            }
        }
        /// <summary>
        /// Read a playlist file from a give path and returns a PlaylistFile (Data Model) complete with absolute paths
        /// well as reading the meta
        /// </summary>
        /// <param name="path">Path to playlist file</param>
        /// <returns>PlaylistFile (Data Model)</returns>
        public static PlaylistFile ReadPlaylistFile(string path)
        {
            var playlistFile = new PlaylistFile();

            try
            {
                string playlistExtension = Path.GetExtension(path).ToLower();
                var    fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                using (var sr = new StreamReader(fs))
                {
                    switch (playlistExtension)
                    {
                    case ".wpl":
                        var content_wpl  = new WplContent();
                        var playlist_wpl = content_wpl.GetFromStream(sr.BaseStream);
                        playlistFile.Title = playlist_wpl.Title.Trim();
                        break;

                    case ".zpl":
                        var content_zpl  = new ZplContent();
                        var playlist_zpl = content_zpl.GetFromStream(sr.BaseStream);
                        playlistFile.Title = playlist_zpl.Title.Trim();
                        break;

                    default:
                        playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                        break;
                    }

                    if (string.IsNullOrEmpty(playlistFile.Title))
                    {
                        playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                    }

                    sr.BaseStream.Position = 0;
                    var parser = PlaylistParserFactory.GetPlaylistParser(playlistExtension);

                    if (sr.BaseStream.Length == 0) // empty playlist
                    {
                        throw new ApplicationException("Empty Playlist");
                    }

                    var playlist = parser.GetFromStream(sr.BaseStream);
                    var paths    = playlist.GetTracksPaths();

                    if ((paths == null || paths.Count == 0) && playlistExtension.In(".m3u", ".m3u8"))
                    {
                        paths = M3uPlaylist.GetPlaylistFromCorruptM3u(path);
                    }

                    playlistFile.Path             = path;
                    playlistFile.LastModifiedDate = new FileInfo(path).LastWriteTime;

                    var musicFileRepo = new MusicFileRepo();

                    foreach (string musicFilePath in paths)
                    {
                        try
                        {
                            string absolutePath = Utils.IsAbsolutePath(musicFilePath)
                               ? musicFilePath
                               : Utils.MakeAbsolutePath(Path.GetDirectoryName(path), musicFilePath);

                            if (absolutePath.StartsWith("file://"))
                            {
                                absolutePath = new Uri(absolutePath).LocalPath;
                            }

                            if (Path.GetExtension(absolutePath).ToLower().In(Global.SupportedMusicFiles) &&
                                File.Exists(absolutePath))
                            {
                                playlistFile.PlaylistItems.Add(new PlaylistFile.PlaylistFileItem
                                {
                                    Path = absolutePath
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            // Invalid path - Ignore - Don't add
                            var _ = e;
                            Logger.LogWarning(
                                "ReadPlaylistFile",
                                "Invalid path detected in playlist file and will be ignored: " + path);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogError(
                    "ReadPlaylistFile",
                    "Error reading playlist file: " + path + ": " + e.Message);
            }

            return(playlistFile);
        }
示例#9
0
        /// <summary>
        /// Read a playlist file from a give path and returns a PlaylistFile (Data Model) complete with absolute paths
        /// well as reading the meta
        /// </summary>
        /// <param name="path">Path to playlist file</param>
        /// <returns>PlaylistFile (Data Model)</returns>
        public static PlaylistFile ReadPlaylistFile(string path)
        {
            var    playlistFile      = new PlaylistFile();
            string playlistExtension = Path.GetExtension(path).ToLower();

            var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            using (var sr = new StreamReader(fs))
            {
                switch (playlistExtension)
                {
                case ".wpl":
                    var content_wpl  = new WplContent();
                    var playlist_wpl = content_wpl.GetFromStream(sr.BaseStream);
                    playlistFile.Title = playlist_wpl.Title.Trim();
                    break;

                case ".zpl":
                    var content_zpl  = new ZplContent();
                    var playlist_zpl = content_zpl.GetFromStream(sr.BaseStream);
                    playlistFile.Title = playlist_zpl.Title.Trim();
                    break;

                default:
                    playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                    break;
                }

                if (string.IsNullOrEmpty(playlistFile.Title))
                {
                    playlistFile.Title = Path.GetFileNameWithoutExtension(path).Trim();
                }

                sr.BaseStream.Position = 0;
                var parser   = PlaylistParserFactory.GetPlaylistParser(playlistExtension);
                var playlist = parser.GetFromStream(sr.BaseStream);
                var paths    = playlist.GetTracksPaths();

                if ((paths == null || paths.Count == 0) && playlistExtension.In(".m3u", ".m3u8"))
                {
                    paths = M3uPlaylist.GetPlaylistFromCorruptM3u(path);
                }

                playlistFile.Path             = path;
                playlistFile.LastModifiedDate = new FileInfo(path).LastWriteTime;

                var musicFileRepo = new MusicFileRepo();

                foreach (string musicFilePath in paths)
                {
                    string absolutePath = Utils.IsAbsolutePath(musicFilePath)
                                                ? musicFilePath
                                                : Utils.MakeAbsolutePath(Path.GetDirectoryName(path), musicFilePath);

                    if (absolutePath.StartsWith("file://"))
                    {
                        absolutePath = new Uri(absolutePath).LocalPath;
                    }

                    if (Path.GetExtension(absolutePath).ToLower().In(Global.SupportedMusicFiles) &&
                        File.Exists(absolutePath))
                    {
                        playlistFile.PlaylistItems.Add(new PlaylistFile.PlaylistFileItem
                        {
                            Path = absolutePath
                        });
                    }
                }
            }

            return(playlistFile);
        }