Beispiel #1
0
        private static async Task <PlayResource> ParseLiveData(AudioResource resource, string requestUrl)
        {
            List <M3uEntry>?webList = null;

            try
            {
                webList = await WebWrapper.Request(requestUrl).ToAction(async response =>
                                                                        await M3uReader.TryGetData(await response.Content.ReadAsStreamAsync())
                                                                        );
            }
            catch (Exception ex) { throw Error.Exception(ex).LocalStr(strings.error_media_internal_invalid); }

            const string AacHe = "mp4a.40.5";
            const string AacLc = "mp4a.40.2";

            var streamPref = from item in webList
                             let codecs = item.StreamMeta != null?StreamCodecMatch.Match(item.StreamMeta).Groups[1].Value : ""
                                          let codecPref = codecs.Contains(AacLc) ? 0
                                                                                                                                                                     : codecs.Contains(AacHe) ? 1
                                                                                                                                                                     : 2
                                                          let bitrate = item.StreamMeta != null?int.Parse(StreamBitrateMatch.Match(item.StreamMeta).Groups[1].Value) : int.MaxValue
                                                                        orderby codecPref, bitrate ascending
            select item;

            var streamSelect = streamPref.FirstOrDefault();

            if (streamSelect is null)
            {
                throw Error.LocalStr(strings.error_media_no_stream_extracted);
            }
            return(new PlayResource(streamSelect.TrackUrl, resource));
        }
Beispiel #2
0
        public R <Playlist, LocalStr> GetPlaylist(string url)
        {
            var foundUri = FindFile(url);

            if (foundUri != null && Directory.Exists(foundUri.OriginalString))
            {
                try
                {
                    var di        = new DirectoryInfo(foundUri.OriginalString);
                    var plist     = new Playlist(di.Name);
                    var resources = from file in di.EnumerateFiles()
                                    select ValidateFile(file.FullName) into result
                                        where result.Ok
                                    select result.Value into val
                                    select new AudioResource(val.FullUri, string.IsNullOrWhiteSpace(val.Title) ? val.FullUri : val.Title, FactoryFor) into res
                                    select new PlaylistItem(res);

                    plist.AddRange(resources);

                    return(plist);
                }
                catch (Exception ex)
                {
                    Log.Warn("Failed to load playlist \"{0}\", because {1}", url, ex.Message);
                    return(new LocalStr(strings.error_io_unknown_error));
                }
            }

            var m3uResult = R <IReadOnlyList <PlaylistItem>, string> .Err(string.Empty);

            if (foundUri != null && File.Exists(foundUri.OriginalString))
            {
                using (var stream = File.OpenRead(foundUri.OriginalString))
                    m3uResult = M3uReader.TryGetData(stream);
            }
            else if (foundUri != null)
            {
                var status = WebWrapper.GetResponseUnsafe(foundUri);
                if (status.Ok)
                {
                    using (var stream = status.Value)
                        m3uResult = M3uReader.TryGetData(stream);
                }
                else
                {
                    return(status.Error);
                }
            }

            if (m3uResult)
            {
                var m3uList = new Playlist(PlaylistManager.CleanseName(url));
                m3uList.AddRange(m3uResult.Value);
                return(m3uList);
            }

            return(new LocalStr(strings.error_media_file_not_found));
        }
Beispiel #3
0
        public R <Playlist> GetPlaylist(string url)
        {
            var foundUri = FindFile(url);

            if (foundUri != null && Directory.Exists(foundUri.OriginalString))
            {
                try
                {
                    var di        = new DirectoryInfo(foundUri.OriginalString);
                    var plist     = new Playlist(di.Name);
                    var resources = from file in di.EnumerateFiles()
                                    select ValidateFile(file.FullName) into result
                                        where result.Ok
                                    select result.Value into val
                                    select new AudioResource(val.FullUri, string.IsNullOrWhiteSpace(val.Title) ? val.FullUri : val.Title, FactoryFor) into res
                                    select new PlaylistItem(res);

                    plist.AddRange(resources);

                    return(plist);
                }
                // TODO: correct errors
                catch (PathTooLongException) { return(R <Playlist> .Err(RResultCode.AccessDenied.ToString())); }
                catch (ArgumentException) { return(R <Playlist> .Err(RResultCode.MediaFileNotFound.ToString())); }
            }

            var m3uResult = R <IReadOnlyList <PlaylistItem> > .Err(string.Empty);

            if (foundUri != null && File.Exists(foundUri.OriginalString))
            {
                using (var stream = File.OpenRead(foundUri.OriginalString))
                    m3uResult = M3uReader.TryGetData(stream);
            }
            else if (foundUri != null)
            {
                var status = WebWrapper.GetResponseUnsafe(foundUri);
                if (status.Ok)
                {
                    using (var stream = status.Value)
                        m3uResult = M3uReader.TryGetData(stream);
                }
                else
                {
                    return(status.Error);
                }
            }

            if (m3uResult)
            {
                var m3uList = new Playlist(PlaylistManager.CleanseName(url));
                m3uList.AddRange(m3uResult.Value);
                return(m3uList);
            }

            return(R <Playlist> .Err(RResultCode.MediaFileNotFound.ToString()));
        }
Beispiel #4
0
        public R <Playlist> GetPlaylist(string url)
        {
            if (Directory.Exists(url))
            {
                try
                {
                    var di        = new DirectoryInfo(url);
                    var plist     = new Playlist(di.Name);
                    var resources = from file in di.EnumerateFiles()
                                    select ValidateFile(file.FullName) into result
                                        where result.Ok
                                    select result.Value into val
                                    select new AudioResource(val.FullUri, string.IsNullOrWhiteSpace(val.Title) ? val.FullUri : val.Title, FactoryFor) into res
                                    select new PlaylistItem(res);

                    plist.AddRange(resources);

                    return(plist);
                }
                // TODO: correct errors
                catch (PathTooLongException) { return(R <Playlist> .Err(RResultCode.AccessDenied.ToString())); }
                catch (ArgumentException) { return(R <Playlist> .Err(RResultCode.MediaFileNotFound.ToString())); }
            }

            var m3uresult = R <IReadOnlyList <PlaylistItem> > .Err(string.Empty);

            if (File.Exists(url))
            {
                using (var stream = File.OpenRead(url))
                    m3uresult = M3uReader.TryGetData(stream);
            }
            else if (Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                var ret = WebWrapper.GetResponse(uri, resp =>
                {
                    using (var stream = resp.GetResponseStream())
                        m3uresult = M3uReader.TryGetData(stream);
                });
                if (ret != ValidateCode.Ok)
                {
                    return(R <Playlist> .Err(ret.ToString()));
                }
            }

            if (m3uresult)
            {
                var m3ulist = new Playlist(PlaylistManager.CleanseName(url));
                m3ulist.AddRange(m3uresult.Value);
                return(m3ulist);
            }

            return(R <Playlist> .Err(RResultCode.MediaFileNotFound.ToString()));
        }
Beispiel #5
0
        public void ListWithoutMetaTagsTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"
C:\PepeHands.jpg
./do/I/look/like/I/know/what/a/Jaypeg/is
"
                                                                   ))).Result;

            Assert.AreEqual(2, result.Count);

            Assert.AreEqual(null, result[0].Title);
            Assert.AreEqual(null, result[1].Title);

            Assert.AreEqual(@"C:\PepeHands.jpg", result[0].TrackUrl);
            Assert.AreEqual("./do/I/look/like/I/know/what/a/Jaypeg/is", result[1].TrackUrl);
        }
Beispiel #6
0
        public void ListWithoutMetaTagsTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"
C:\PepeHands.jpg
./do/I/look/like/I/know/what/a/Jaypeg/is
"
                                                                   )));

            Assert.That(result.Ok);
            Assert.AreEqual(2, result.Value.Count);

            Assert.AreEqual(@"C:\PepeHands.jpg", result.Value[0].DisplayString);
            Assert.AreEqual(@"./do/I/look/like/I/know/what/a/Jaypeg/is", result.Value[1].DisplayString);

            Assert.AreEqual(@"C:\PepeHands.jpg", result.Value[0].Resource.ResourceId);
            Assert.AreEqual(@"./do/I/look/like/I/know/what/a/Jaypeg/is", result.Value[1].Resource.ResourceId);
        }
Beispiel #7
0
        public void ListWithM3uHeaderTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"#EXTM3U
#EXTINF:1337,Never gonna give you up
C:\Windows\System32\firewall32.cpl
#EXTINF:1337,Never gonna let you down
C:\Windows\System32\firewall64.cpl"
                                                                   ))).Result;

            Assert.AreEqual(2, result.Count);

            Assert.AreEqual("Never gonna give you up", result[0].Title);
            Assert.AreEqual("Never gonna let you down", result[1].Title);

            Assert.AreEqual(@"C:\Windows\System32\firewall32.cpl", result[0].TrackUrl);
            Assert.AreEqual(@"C:\Windows\System32\firewall64.cpl", result[1].TrackUrl);
        }
Beispiel #8
0
        public void ListWithM3uHeaderTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"#EXTM3U
#EXTINF:1337,Never gonna give you up
C:\Windows\System32\firewall32.cpl
#EXTINF:1337,Never gonna let you down
C:\Windows\System32\firewall64.cpl"
                                                                   )));

            Assert.That(result.Ok);
            Assert.AreEqual(2, result.Value.Count);

            Assert.AreEqual("Never gonna give you up", result.Value[0].DisplayString);
            Assert.AreEqual("Never gonna let you down", result.Value[1].DisplayString);

            Assert.AreEqual(@"C:\Windows\System32\firewall32.cpl", result.Value[0].Resource.ResourceId);
            Assert.AreEqual(@"C:\Windows\System32\firewall64.cpl", result.Value[1].Resource.ResourceId);
        }
Beispiel #9
0
        public void SimpleListTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"#EXTINF:197,Delain - Delain - We Are The Others
/opt/music/bad/Delain.mp3
#EXTINF:314,MONO - MONO - The Hand That Holds the Truth
/opt/music/bad/MONO.mp3
#EXTINF:223,Deathstars - Deathstars - Opium
/opt/music/bad/Opium.mp3"
                                                                   ))).Result;

            Assert.AreEqual(3, result.Count);

            Assert.AreEqual("Delain - Delain - We Are The Others", result[0].Title);
            Assert.AreEqual("MONO - MONO - The Hand That Holds the Truth", result[1].Title);
            Assert.AreEqual("Deathstars - Deathstars - Opium", result[2].Title);

            Assert.AreEqual("/opt/music/bad/Delain.mp3", result[0].TrackUrl);
            Assert.AreEqual("/opt/music/bad/MONO.mp3", result[1].TrackUrl);
            Assert.AreEqual("/opt/music/bad/Opium.mp3", result[2].TrackUrl);
        }
Beispiel #10
0
        public void SimpleListTest()
        {
            var result = M3uReader.TryGetData(new MemoryStream(Encoding.UTF8.GetBytes(
                                                                   @"#EXTINF:197,Delain - Delain - We Are The Others
/opt/music/bad/Delain.mp3
#EXTINF:314,MONO - MONO - The Hand That Holds the Truth
/opt/music/bad/MONO.mp3
#EXTINF:223,Deathstars - Deathstars - Opium
/opt/music/bad/Opium.mp3"
                                                                   )));

            Assert.That(result.Ok);
            Assert.AreEqual(3, result.Value.Count);

            Assert.AreEqual("Delain - Delain - We Are The Others", result.Value[0].DisplayString);
            Assert.AreEqual("MONO - MONO - The Hand That Holds the Truth", result.Value[1].DisplayString);
            Assert.AreEqual("Deathstars - Deathstars - Opium", result.Value[2].DisplayString);

            Assert.AreEqual("/opt/music/bad/Delain.mp3", result.Value[0].Resource.ResourceId);
            Assert.AreEqual("/opt/music/bad/MONO.mp3", result.Value[1].Resource.ResourceId);
            Assert.AreEqual("/opt/music/bad/Opium.mp3", result.Value[2].Resource.ResourceId);
        }