예제 #1
0
        public M3uFile Parse(string data)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new Exception();
            }

            if (data.IndexOf("#EXTM3U") != 0)
            {
                throw new Exception();
            }

            var file = new M3uFile
            {
                Medias = new List <M3uMedia>()
            };

            var matches = Regex.Matches(data, "#[^#]*");

            for (var i = 1; i < matches.Count; i++)
            {
                TryAddMedia(file, matches[i].Value);
            }

            return(file);
        }
예제 #2
0
        private void TryAddMedia(M3uFile file, string data)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentException();
            }

            var match = _mediaRegex.Match(data);

            if (match.Success)
            {
                var media = new M3uMedia
                {
                    Title      = match.Groups["title"].Value.Trim(),
                    Duration   = decimal.Parse(match.Groups["duration"].Value.Trim()),
                    Url        = match.Groups["url"].Value.Trim(),
                    Attributes = new Dictionary <string, string>()
                };

                var attributes = _attributesRegex.Matches(match.Groups["attributes"].Value);
                foreach (Match attr in attributes)
                {
                    media.Attributes.Add(attr.Groups["key"].Value, attr.Groups["value"].Value);
                }

                file.Medias.Add(media);
            }
            else
            {
                // TODO: Add warning.
            }
        }