瀏覽代碼

add m3u unit test

Luke Pulverenti 8 年之前
父節點
當前提交
1cb7a1b49c

+ 24 - 8
Emby.Server.Implementations/LiveTv/TunerHosts/M3uParser.cs

@@ -43,6 +43,17 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
             }
         }
 
+        public List<M3UChannel> ParseString(string text, string channelIdPrefix, string tunerHostId)
+        {
+            var urlHash = "text".GetMD5().ToString("N");
+
+            // Read the file and display it line by line.
+            using (var reader = new StringReader(text))
+            {
+                return GetChannels(reader, urlHash, channelIdPrefix, tunerHostId);
+            }
+        }
+
         public Task<Stream> GetListingsStream(string url, CancellationToken cancellationToken)
         {
             if (url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
@@ -59,7 +70,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
         }
 
         const string ExtInfPrefix = "#EXTINF:";
-        private List<M3UChannel> GetChannels(StreamReader reader, string urlHash, string channelIdPrefix, string tunerHostId)
+        private List<M3UChannel> GetChannels(TextReader reader, string urlHash, string channelIdPrefix, string tunerHostId)
         {
             var channels = new List<M3UChannel>();
             string line;
@@ -122,16 +133,17 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
             var nameParts = extInf.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
             var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
 
-            var numberString = nameParts[0];
+            string numberString = null;
 
             //Check for channel number with the format from SatIp
-            int number;
+            // #EXTINF:0,84. VOX Schweiz
             if (!string.IsNullOrWhiteSpace(nameInExtInf))
             {
                 var numberIndex = nameInExtInf.IndexOf('.');
                 if (numberIndex > 0)
                 {
-                    if (int.TryParse(nameInExtInf.Substring(0, numberIndex), out number))
+                    double number;
+                    if (double.TryParse(nameInExtInf.Substring(0, numberIndex), NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out number))
                     {
                         numberString = number.ToString();
                     }
@@ -150,7 +162,11 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
                 string value;
                 if (attributes.TryGetValue("tvg-id", out value))
                 {
-                    numberString = value;
+                    double doubleValue;
+                    if (double.TryParse(value, NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out doubleValue))
+                    {
+                        numberString = value;
+                    }
                 }
             }
 
@@ -209,16 +225,16 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts
             var nameInExtInf = nameParts.Length > 1 ? nameParts.Last().Trim() : null;
 
             //Check for channel number with the format from SatIp
-            int number;
             if (!string.IsNullOrWhiteSpace(nameInExtInf))
             {
                 var numberIndex = nameInExtInf.IndexOf('.');
                 if (numberIndex > 0)
                 {
-                    if (int.TryParse(nameInExtInf.Substring(0, numberIndex), out number))
+                    double number;
+                    if (double.TryParse(nameInExtInf.Substring(0, numberIndex), NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out number))
                     {
                         //channel.Number = number.ToString();
-                        nameInExtInf = nameInExtInf.Substring(numberIndex + 1);
+                        nameInExtInf = nameInExtInf.Substring(numberIndex + 1).Trim();
                     }
                 }
             }

+ 44 - 0
MediaBrowser.Tests/M3uParserTest.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Emby.Common.Implementations.Cryptography;
+using Emby.Server.Implementations.LiveTv.TunerHosts;
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Model.Logging;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace MediaBrowser.Tests
+{
+    [TestClass]
+    public class M3uParserTest
+    {
+        [TestMethod]
+        public void TestFormat1()
+        {
+            BaseExtensions.CryptographyProvider = new CryptographyProvider();
+
+            var result = new M3uParser(new NullLogger(), null, null, null).ParseString("#EXTINF:0,84. VOX Schweiz\nhttp://mystream", "-", "-");
+            Assert.AreEqual(1, result.Count);
+
+            Assert.AreEqual("VOX Schweiz", result[0].Name);
+            Assert.AreEqual("84", result[0].Number);
+        }
+        [TestMethod]
+        public void TestFormat2()
+        {
+            BaseExtensions.CryptographyProvider = new CryptographyProvider();
+
+            var input = "#EXTINF:-1 tvg-id=\"\" tvg-name=\"ABC News 04\" tvg-logo=\"\" group-title=\"ABC Group\",ABC News 04";
+            input += "\n";
+            input += "http://mystream";
+
+            var result = new M3uParser(new NullLogger(), null, null, null).ParseString(input, "-", "-");
+            Assert.AreEqual(1, result.Count);
+
+            Assert.AreEqual("ABC News 04", result[0].Name);
+            Assert.IsNull(result[0].Number);
+        }
+    }
+}

+ 12 - 0
MediaBrowser.Tests/MediaBrowser.Tests.csproj

@@ -37,6 +37,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Emby.Common.Implementations">
+      <HintPath>..\ThirdParty\emby\Emby.Common.Implementations.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.XML" />
   </ItemGroup>
@@ -58,12 +61,21 @@
     <Compile Include="ConsistencyTests\TextIndexing\WordIndex.cs" />
     <Compile Include="ConsistencyTests\TextIndexing\WordOccurrence.cs" />
     <Compile Include="ConsistencyTests\TextIndexing\WordOccurrences.cs" />
+    <Compile Include="M3uParserTest.cs" />
     <Compile Include="MediaEncoding\Subtitles\AssParserTests.cs" />
     <Compile Include="MediaEncoding\Subtitles\SrtParserTests.cs" />
     <Compile Include="MediaEncoding\Subtitles\VttWriterTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\Emby.Server.Implementations\Emby.Server.Implementations.csproj">
+      <Project>{e383961b-9356-4d5d-8233-9a1079d03055}</Project>
+      <Name>Emby.Server.Implementations</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\MediaBrowser.Common\MediaBrowser.Common.csproj">
+      <Project>{9142eefa-7570-41e1-bfcc-468bb571af2f}</Project>
+      <Name>MediaBrowser.Common</Name>
+    </ProjectReference>
     <ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj">
       <Project>{17e1f4e6-8abd-4fe5-9ecf-43d4b6087ba2}</Project>
       <Name>MediaBrowser.Controller</Name>