KingOfSat.cs
author StephaneLenclud
Fri, 15 May 2015 15:16:18 +0200
changeset 2 d73e99c5333c
child 3 01a00603ec28
permissions -rw-r--r--
First MP export draft.
     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)
    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);
    43 				common.Satellite = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(2) > a").Get(0).InnerText);
    44 				common.Frequency = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(3)").Get(0).InnerText);
    45 				common.Polarisation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(4)").Get(0).InnerText);
    46 				common.Transponder = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(5) > a").Get(0).InnerText);
    47 				common.Beam = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(6) > a").Get(0).InnerText);
    48 				common.Standard = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(7)").Get(0).InnerText);
    49 				common.Modulation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(8)").Get(0).InnerText);
    50 				common.SymbolRate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a").Get(0).InnerText);
    51 				common.FEC = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a:nth-child(2)").Get(0).InnerText);
    52 				try
    53 				{
    54 					common.Provider = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10) > b").Get(0).InnerText);
    55 				}
    56 				catch (Exception)
    57 				{
    58 				}
    59 
    60 				common.Bitrate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10)").Get(0).InnerText);
    61 				if (common.Bitrate.Substring(0, ", ".Length) == ", ")
    62 				{
    63 					common.Bitrate = common.Bitrate.Substring(", ".Length, common.Bitrate.Length - ", ".Length);
    64 				}
    65 				//
    66 				common.NetworkID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(11)").Get(0).InnerText);
    67 				common.NetworkID = common.NetworkID.Substring("NID:".Length, common.NetworkID.Length - "NID:".Length);
    68 				//
    69 				common.TransponderID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(12)").Get(0).InnerText);
    70 				common.TransponderID = common.TransponderID.Substring("TID:".Length, common.TransponderID.Length - "TID:".Length);
    71 
    72 				//We got common properties for the coming channels
    73 				//Debug.Write(common.ToString());
    74 
    75 				//Now get all the channels for that frequency
    76 				//Channel common = new Channel();
    77 
    78 				CQ channelsDiv = frq.Cq().Next("div");
    79 				CQ channelsTableRows = channelsDiv.Find("table.fl > tbody").Children("tr");
    80 
    81 				foreach (IDomObject row in channelsTableRows)
    82 				{
    83 					Channel channel = new Channel();
    84 					//Initialize this channel with common properties on this frequency
    85 					channel = common;
    86 
    87 					//Try and parse channel name
    88 					CQ cqChannelName = row.Cq().Find("td:nth-child(3) > a");
    89 					if (cqChannelName.Length == 0)
    90 					{
    91 						cqChannelName = row.Cq().Find("td:nth-child(3) > i");
    92 						if (cqChannelName.Length == 0)
    93 						{
    94 							//Can't get channel name
    95 							Debug.Write("WARNING: Can't find channel name! Skipping this channel");
    96 							continue;
    97 						}
    98 					}
    99 
   100 					channel.Name = WebUtility.HtmlDecode(cqChannelName.Get(0).InnerText).Trim();
   101 					if (channel.Name == "Name")
   102 					{
   103 						//Skipping header rows
   104 						continue;
   105 					}
   106 
   107 					//So we have a channel name get the other properties then
   108 					channel.Country = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(4)").Get(0).InnerText).Trim();
   109 					channel.Category = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(5)").Get(0).InnerText).Trim();
   110 					//Skip the packages
   111 					//Skip the encryptions
   112 					channel.SID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(8)").Get(0).InnerText).Trim();
   113 					channel.VPID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(9)").Get(0).InnerText).Trim();
   114 					//Skip audios
   115 					channel.PMT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   116 					channel.PCR = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   117 					channel.TXT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
   118 
   119 					//Append that new channel to our list
   120 					channels.Add(channel);
   121 
   122 					//Show it in debug output
   123 					Debug.Write(channel);
   124 				} //For each channel
   125 			} //For each frequency
   126 
   127 			return channels;
   128 		}
   129 	}
   130 }