MediaPortal.cs
author StephaneLenclud
Sat, 06 Oct 2018 14:07:31 +0200
changeset 9 b77b09f680e7
parent 5 29ccfbf98e54
permissions -rw-r--r--
SatIndex grabber now working.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Xml;
     8 using System.IO;
     9 
    10 namespace SatChanGen
    11 {
    12 	class MediaPortal
    13 	{
    14 
    15 		public static string Polarisation(Channel aChannel)
    16 		{
    17 			switch (aChannel.Polarisation)
    18 			{
    19 				case "H":
    20 					return "1";
    21 				case "V":
    22 					return "2";
    23 				case "L": //Check that on KingOfSat
    24 					return "3";
    25 				case "R": //Check that on KingOfSat
    26 					return "4";
    27 
    28 				default:
    29 					{
    30 						Debug.Write("WARNING: Unknown Polarisation " + aChannel.Polarisation + " for " + aChannel.Name + "\n");
    31 						return "-1";
    32 					}
    33 			}
    34 
    35 			/*
    36 			"Not Set",
    37             "Not Defined",
    38             "Horizontal",
    39             "Vertical",
    40             "Circular Left",
    41             "Circular Right"
    42 			 */
    43 		}
    44 
    45 		public static string Modulation(Channel aChannel)
    46 		{
    47 			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
    48 
    49 			switch (aChannel.Modulation)
    50 			{
    51 				case "QPSK":
    52 					return "20";
    53 				case "8PSK":
    54 					return "27";
    55 				default:
    56 				{
    57 					Debug.Write("WARNING: Unknown modulation " + aChannel.Modulation + " for " + aChannel.Name + "\n");
    58 					return "-1";
    59 				}
    60 			}
    61 
    62 
    63 			/*
    64 			"Not Set",
    65 			"Not Defined",
    66 			"16 QAM",
    67 			"32 QAM",
    68 			"64 QAM",
    69 			"80 QAM",
    70 			"96 QAM",
    71 			"112 QAM",
    72 			"128 QAM",
    73 			"160 QAM",
    74 			"192 QAM",
    75 			"224 QAM",
    76 			"256 QAM",
    77 			"320 QAM",
    78 			"384 QAM",
    79 			"448 QAM",
    80 			"512 QAM",
    81 			"640 QAM",
    82 			"768 QAM",
    83 			"896 QAM",
    84 			"1024 QAM",
    85 			"QPSK",
    86 			"BPSK",
    87 			"OQPSK",
    88 			"8 VSB",
    89 			"16 VSB",
    90 			"Analog Amplitude",
    91 			"Analog Frequency",
    92 			"8 PSK",
    93 			"RF",
    94 			"16 APSK",
    95 			"32 APSK",
    96 			"QPSK2 (DVB-S2)",
    97 			"8 PSK2 (DVB-S2)",
    98 			"DirectTV"
    99 			 */
   100 		}
   101 
   102 
   103 		public static string FEC(Channel aChannel)
   104 		{
   105 			//Those can be worked out from TvLibrary FormDVBSTuningDetail.Designer.cs
   106 
   107 			switch (aChannel.FEC)
   108 			{
   109 				case "1/2":
   110 					return "1";
   111 				case "2/3":
   112 					return "2";
   113 				case "3/4":
   114 					return "3";
   115 				case "3/5":
   116 					return "4";
   117 				case "4/5":
   118 					return "5";
   119 				case "5/6":
   120 					return "6";
   121 				case "5/11":
   122 					return "7";
   123 				case "7/8":
   124 					return "8";
   125 				case "1/4":
   126 					return "9";
   127 				case "1/3":
   128 					return "10";
   129 				case "2/5":
   130 					return "11";
   131 				case "6/7":
   132 					return "12";
   133 				case "8/9":
   134 					return "13";
   135 				case "9/10":
   136 					return "14";
   137 
   138 				default:
   139 				{
   140 					Debug.Write("WARNING: Unknown FEC " + aChannel.FEC + " for " + aChannel.Name + "\n");
   141 					return "-1";
   142 				}
   143 			}
   144 
   145 
   146 		}
   147 
   148         //
   149         //
   150         //
   151 		public static void Export(List<Channel> aChannels, List<string> aTunerCards,  string aFileName, bool aAddMapping)
   152 		{
   153             //Create a dictionary for our groups
   154             Dictionary<string, List<Channel>> groups = new Dictionary<string, List<Channel>>();
   155 
   156 			XmlDocument xmlDoc = new XmlDocument();
   157             XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
   158             xmlDoc.AppendChild(xmlDeclaration);
   159 
   160 			XmlNode rootElement = xmlDoc.CreateElement("tvserver");			
   161 			AddAttribute(rootElement, "version", "1.0");
   162 			xmlDoc.AppendChild(rootElement);
   163 			//Create our channel element
   164 			XmlNode nodechannels = xmlDoc.CreateElement("channels");			
   165 			rootElement.AppendChild(nodechannels);
   166 
   167 			int channelId = 0;
   168             int channelMapId = 0;
   169 			foreach (Channel channel in aChannels)
   170 			{
   171                 //Create group if needed
   172                 if (!groups.ContainsKey(channel.Category)) 
   173                 {
   174                     //KeyValuePair<string, List<Channel>> entry=new KeyValuePair<string, List<Channel>>();
   175                     groups.Add(channel.Category, new List<Channel>());
   176                 }
   177 
   178                 //Add that channel to its group
   179                 groups[channel.Category].Add(channel);
   180 
   181                 //Create and populate channel element
   182 				channelId++;
   183 				XmlNode nodechannel = xmlDoc.CreateElement("channel");
   184 				AddAttribute(nodechannel, "GrabEpg", "True");
   185 				AddAttribute(nodechannel, "IdChannel", channelId);
   186 				AddAttribute(nodechannel, "IsRadio", "False");
   187 				AddAttribute(nodechannel, "IsTv", "True");
   188                 AddAttribute(nodechannel, "LastGrabTime", "2000-1-1 0:0:0");
   189 				//AddAttribute(nodechannel, "SortOrder", channel.SortOrder);
   190                 //AddAttribute(nodechannel, "TimesWatched", "0");
   191                 AddAttribute(nodechannel, "TotalTimeWatched", "2000-1-1 0:0:0");
   192                 AddAttribute(nodechannel, "VisibleInGuide", !channel.Name.StartsWith("."));
   193 				AddAttribute(nodechannel, "DisplayName", channel.Name);
   194 				AddAttribute(nodechannel, "ChannelNumber", channelId);
   195 
   196                 if (aAddMapping)
   197                 {
   198                     //I don't think we can get mapping to work without having corresponding server and card definition elements
   199                     XmlNode nodeMaps = xmlDoc.CreateElement("mappings");
   200                     foreach (string tuner in aTunerCards)
   201                     {
   202                         channelMapId++;
   203                         XmlNode nodeMap = xmlDoc.CreateElement("map");
   204                         AddAttribute(nodeMap, "IdCard", tuner);
   205                         AddAttribute(nodeMap, "IdChannel", channelId);
   206                         AddAttribute(nodeMap, "IdChannelMap", channelMapId);
   207                         nodeMaps.AppendChild(nodeMap);
   208                     }
   209                     nodechannel.AppendChild(nodeMaps);
   210                 }
   211 
   212 				XmlNode nodeTuningDetails = xmlDoc.CreateElement("TuningDetails");
   213 				XmlNode nodeTune = xmlDoc.CreateElement("tune");
   214 				AddAttribute(nodeTune, "IdChannel", channelId);
   215 				AddAttribute(nodeTune, "IdTuning", channelId);
   216 				AddAttribute(nodeTune, "Bandwidth", "8"); //Weird
   217 				AddAttribute(nodeTune, "ChannelNumber", "10000");
   218 				AddAttribute(nodeTune, "ChannelType", "3"); //Bad hard coding, does it means DVB-S
   219 				AddAttribute(nodeTune, "CountryId", "31"); //Is that Germany?
   220 				AddAttribute(nodeTune, "Diseqc", "0"); //Disabled
   221 				AddAttribute(nodeTune, "FreeToAir", "False"); //Can we get that from KingOfSat? Does it matter?
   222 				AddAttribute(nodeTune, "Frequency", ((Int32)(Convert.ToDouble(channel.Frequency)*1000)).ToString());
   223 				AddAttribute(nodeTune, "MajorChannel", "-1");
   224 				AddAttribute(nodeTune, "MinorChannel", "-1");
   225 				AddAttribute(nodeTune, "Modulation", Modulation(channel));
   226 				AddAttribute(nodeTune, "Name", channel.Name);
   227 				AddAttribute(nodeTune, "NetworkId", channel.NetworkID);
   228 				AddAttribute(nodeTune, "PmtPid", channel.PMT);
   229 				AddAttribute(nodeTune, "Polarisation", Polarisation(channel));
   230 				AddAttribute(nodeTune, "Provider", channel.Provider); //Does that matter? Should really be SKY or CSAT
   231 				AddAttribute(nodeTune, "ServiceId", channel.SID);
   232 				AddAttribute(nodeTune, "SwitchingFrequency", "11700000"); //Does it matter?
   233 				AddAttribute(nodeTune, "Symbolrate", channel.SymbolRate);
   234 				AddAttribute(nodeTune, "TransportId", channel.TransponderID);
   235 				AddAttribute(nodeTune, "TuningSource", "0"); //Is that needed?
   236 				AddAttribute(nodeTune, "VideoSource", "0"); //Is that needed?
   237 				AddAttribute(nodeTune, "AudioSource", "0"); //Is that needed?
   238 				AddAttribute(nodeTune, "IsVCRSignal", "False");
   239 				AddAttribute(nodeTune, "SatIndex", "-1");
   240 				AddAttribute(nodeTune, "InnerFecRate", FEC(channel));
   241 				AddAttribute(nodeTune, "Band", "0"); //Needed?
   242 				AddAttribute(nodeTune, "Pilot", "-1");
   243 				AddAttribute(nodeTune, "RollOff", "-1");
   244 				AddAttribute(nodeTune, "Url", "");
   245 				AddAttribute(nodeTune, "Bitrate", "0"); //Should we bother putting it in there?
   246 				nodeTuningDetails.AppendChild(nodeTune);
   247 					
   248 				nodechannel.AppendChild(nodeTuningDetails);
   249 
   250 				nodechannels.AppendChild(nodechannel);				
   251 			}
   252 
   253             //Create groups element
   254             XmlNode nodeChannelGroups = xmlDoc.CreateElement("channelgroups");
   255             rootElement.AppendChild(nodeChannelGroups);
   256 
   257             int groupSortOrder = 0;
   258             foreach (KeyValuePair<string, List<Channel>> group in groups)
   259             {
   260                 //Create group element
   261                 XmlNode nodeChannelGroup = xmlDoc.CreateElement("channelgroup");
   262                 AddAttribute(nodeChannelGroup, "GroupName", group.Key);
   263                 AddAttribute(nodeChannelGroup, "SortOrder", groupSortOrder.ToString());
   264                 XmlNode nodeGroupMap = xmlDoc.CreateElement("mappings");
   265                 nodeChannelGroup.AppendChild(nodeGroupMap);
   266                 nodeChannelGroups.AppendChild(nodeChannelGroup);
   267 
   268                 //Sort by name
   269                 List<Channel> sortedChannels = group.Value.OrderBy(o => o.Name).ToList();
   270 
   271                 //Add each channel to its group
   272                 int channelSortOrder = 0;
   273                 foreach (Channel channel in sortedChannels)
   274                 {
   275                     XmlNode nodeMap = xmlDoc.CreateElement("map");
   276                     AddAttribute(nodeMap, "ChannelName", channel.Name);
   277                     AddAttribute(nodeMap, "SortOrder", channelSortOrder.ToString());
   278                     nodeGroupMap.AppendChild(nodeMap);
   279                     channelSortOrder++;
   280                 }
   281 
   282                 groupSortOrder++;
   283             }
   284 
   285             using (TextWriter sw = new StreamWriter(aFileName, false, Encoding.UTF8)) //Set encoding
   286             {
   287                 xmlDoc.Save(sw);
   288             }
   289 			//xmlDoc.Save(aFileName);
   290 		}
   291 
   292 		private static void AddAttribute(XmlNode node, string tagName, string tagValue)
   293 		{
   294 			XmlAttribute attr = node.OwnerDocument.CreateAttribute(tagName);
   295 			attr.InnerText = tagValue;
   296 			node.Attributes.Append(attr);
   297 		}
   298 
   299 		private static void AddAttribute(XmlNode node, string tagName, int tagValue)
   300 		{
   301 			AddAttribute(node, tagName, tagValue.ToString());
   302 		}
   303 
   304 		// store DateTime Values as strings. Improves readability
   305 		private static void AddAttribute(XmlNode node, string tagName, DateTime tagValue)
   306 		{
   307 			AddAttribute(node, tagName,
   308 						 String.Format("{0}-{1}-{2} {3}:{4}:{5}", tagValue.Year, tagValue.Month, tagValue.Day, tagValue.Hour,
   309 									   tagValue.Minute, tagValue.Second));
   310 		}
   311 
   312 		private static void AddAttribute(XmlNode node, string tagName, bool tagValue)
   313 		{
   314 			AddAttribute(node, tagName, tagValue.ToString());
   315 		}
   316 	}
   317 }