KingOfSat.cs
author StephaneLenclud
Fri, 15 May 2015 17:28:51 +0200
changeset 3 01a00603ec28
parent 1 f203331a8f4a
child 4 8372aa8d6292
permissions -rw-r--r--
First verified MediaPortal export.
Adding orbital position filter.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using CsQuery;
     7 using System.Diagnostics;
     8 using System.Net;
     9 
    10 namespace SatChanGen
    11 {
    12 	class KingOfSat
    13 	{
    14 		//
    15 		// Summary:
    16 		//     Create a new CQ object wrapping a single element.
    17 		//
    18 		// Parameters:
    19 		//   aUrl:
    20 		//     URL to a KingOfSat channel list. Typically a package list.
    21 		//
    22 		// Return:
    23 		//   List of channels parsed.		
    24 		public static List<Channel> Parse(string aUrl, string aOrbitalPosition)
    25 		{
    26 			string kos = new WebClient().DownloadString(aUrl);
    27 			//Debug.Write(kos);
    28 
    29 			CQ dom = kos;
    30 
    31 			//Get all the Frequency elements in our page
    32 			CQ sats = dom[".frq"];
    33 
    34 			//Create our list of channels
    35 			List<Channel> channels = new List<Channel>();
    36 
    37 			foreach (IDomObject frq in sats.ToList())
    38 			{
    39 				Channel common = new Channel();
    40 
    41 				//Parse channel details
    42 				common.OrbitalPosition = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td > a > font").Get(0).InnerText).Trim();
    43 			    if (common.OrbitalPosition != aOrbitalPosition)
    44 			    {
    45                     //Wrong sat, skip
    46 			        continue;
    47 			    }
    48 				common.Satellite = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(2) > a").Get(0).InnerText);
    49 				common.Frequency = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(3)").Get(0).InnerText);
    50 				common.Polarisation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(4)").Get(0).InnerText);
    51 				common.Transponder = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(5) > a").Get(0).InnerText);
    52 				common.Beam = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(6) > a").Get(0).InnerText);
    53 				common.Standard = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(7)").Get(0).InnerText);
    54 				common.Modulation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(8)").Get(0).InnerText);
    55 				common.SymbolRate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a").Get(0).InnerText);
    56 				common.FEC = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a:nth-child(2)").Get(0).InnerText);
    57 				try
    58 				{
    59 					common.Provider = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10) > b").Get(0).InnerText);
    60 				}
    61 				catch (Exception)
    62 				{
    63 				}
    64 
    65 				common.Bitrate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10)").Get(0).InnerText);
    66 				if (common.Bitrate.Substring(0, ", ".Length) == ", ")
    67 				{
    68 					common.Bitrate = common.Bitrate.Substring(", ".Length, common.Bitrate.Length - ", ".Length);
    69 				}
    70 				//
    71 				common.NetworkID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(11)").Get(0).InnerText);
    72 				common.NetworkID = common.NetworkID.Substring("NID:".Length, common.NetworkID.Length - "NID:".Length);
    73 				//
    74 				common.TransponderID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(12)").Get(0).InnerText);
    75 				common.TransponderID = common.TransponderID.Substring("TID:".Length, common.TransponderID.Length - "TID:".Length);
    76 
    77 				//We got common properties for the coming channels
    78 				//Debug.Write(common.ToString());
    79 
    80 				//Now get all the channels for that frequency
    81 				//Channel common = new Channel();
    82 
    83 				CQ channelsDiv = frq.Cq().Next("div");
    84 				CQ channelsTableRows = channelsDiv.Find("table.fl > tbody").Children("tr");
    85 
    86 				foreach (IDomObject row in channelsTableRows)
    87 				{
    88 					Channel channel = new Channel();
    89 					//Initialize this channel with common properties on this frequency
    90                     channel.Copy(common);
    91 
    92 					//Try and parse channel name
    93 					CQ cqChannelName = row.Cq().Find("td:nth-child(3) > a");
    94 					if (cqChannelName.Length == 0)
    95 					{
    96 						cqChannelName = row.Cq().Find("td:nth-child(3) > i");
    97 						if (cqChannelName.Length == 0)
    98 						{
    99 							//Can't get channel name
   100 							Debug.Write("WARNING: Can't find channel name! Skipping this channel");
   101 							continue;
   102 						}
   103 					}
   104 
   105 					channel.Name = WebUtility.HtmlDecode(cqChannelName.Get(0).InnerText).Trim();
   106 					if (channel.Name == "Name")
   107 					{
   108 						//Skipping header rows
   109 						continue;
   110 					}
   111 
   112 					//So we have a channel name get the other properties then
   113 					channel.Country = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(4)").Get(0).InnerText).Trim();
   114 					channel.Category = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(5)").Get(0).InnerText).Trim();
   115 					//Skip the packages
   116 					//Skip the encryptions
   117 					channel.SID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(8)").Get(0).InnerText).Trim();
   118 					channel.VPID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(9)").Get(0).InnerText).Trim();
   119 					//Skip audios
   120 					channel.PMT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   121 					channel.PCR = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   122 					channel.TXT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   123 
   124 					//Append that new channel to our list
   125 					channels.Add(channel);
   126 
   127 					//Show it in debug output
   128 					Debug.Write(channel);
   129 				} //For each channel
   130 			} //For each frequency
   131 
   132 			return channels;
   133 		}
   134 	}
   135 }