1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/KingOfSat.cs Fri May 15 13:23:52 2015 +0200
1.3 @@ -0,0 +1,130 @@
1.4 +using System;
1.5 +using System.Collections.Generic;
1.6 +using System.Linq;
1.7 +using System.Text;
1.8 +using System.Threading.Tasks;
1.9 +using CsQuery;
1.10 +using System.Diagnostics;
1.11 +using System.Net;
1.12 +
1.13 +namespace SatChanGen
1.14 +{
1.15 + class KingOfSat
1.16 + {
1.17 + //
1.18 + // Summary:
1.19 + // Create a new CQ object wrapping a single element.
1.20 + //
1.21 + // Parameters:
1.22 + // aUrl:
1.23 + // URL to a KingOfSat channel list. Typically a package list.
1.24 + //
1.25 + // Return:
1.26 + // List of channels parsed.
1.27 + public static List<Channel> Parse(string aUrl)
1.28 + {
1.29 + string kos = new WebClient().DownloadString(aUrl);
1.30 + //Debug.Write(kos);
1.31 +
1.32 + CQ dom = kos;
1.33 +
1.34 + //Get all the Frequency elements in our page
1.35 + CQ sats = dom[".frq"];
1.36 +
1.37 + //Create our list of channels
1.38 + List<Channel> channels = new List<Channel>();
1.39 +
1.40 + foreach (IDomObject frq in sats.ToList())
1.41 + {
1.42 + Channel common = new Channel();
1.43 +
1.44 + //Parse channel details
1.45 + common.OrbitalPosition = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td > a > font").Get(0).InnerText);
1.46 + common.Satellite = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(2) > a").Get(0).InnerText);
1.47 + common.Frequency = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(3)").Get(0).InnerText);
1.48 + common.Polarisation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(4)").Get(0).InnerText);
1.49 + common.Transponder = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(5) > a").Get(0).InnerText);
1.50 + common.Beam = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(6) > a").Get(0).InnerText);
1.51 + common.Standard = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(7)").Get(0).InnerText);
1.52 + common.Modulation = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(8)").Get(0).InnerText);
1.53 + common.SymbolRate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a").Get(0).InnerText);
1.54 + common.FEC = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(9) > a:nth-child(2)").Get(0).InnerText);
1.55 + try
1.56 + {
1.57 + common.Provider = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10) > b").Get(0).InnerText);
1.58 + }
1.59 + catch (Exception)
1.60 + {
1.61 + }
1.62 +
1.63 + common.Bitrate = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(10)").Get(0).InnerText);
1.64 + if (common.Bitrate.Substring(0, ", ".Length) == ", ")
1.65 + {
1.66 + common.Bitrate = common.Bitrate.Substring(", ".Length, common.Bitrate.Length - ", ".Length);
1.67 + }
1.68 + //
1.69 + common.NetworkID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(11)").Get(0).InnerText);
1.70 + common.NetworkID = common.NetworkID.Substring("NID:".Length, common.NetworkID.Length - "NID:".Length);
1.71 + //
1.72 + common.TransponderID = WebUtility.HtmlDecode(frq.Cq().Find("tbody > tr > td:nth-child(12)").Get(0).InnerText);
1.73 + common.TransponderID = common.TransponderID.Substring("TID:".Length, common.TransponderID.Length - "TID:".Length);
1.74 +
1.75 + //We got common properties for the coming channels
1.76 + //Debug.Write(common.ToString());
1.77 +
1.78 + //Now get all the channels for that frequency
1.79 + //Channel common = new Channel();
1.80 +
1.81 + CQ channelsDiv = frq.Cq().Next("div");
1.82 + CQ channelsTableRows = channelsDiv.Find("table.fl > tbody").Children("tr");
1.83 +
1.84 + foreach (IDomObject row in channelsTableRows)
1.85 + {
1.86 + Channel channel = new Channel();
1.87 + //Initialize this channel with common properties on this frequency
1.88 + channel = common;
1.89 +
1.90 + //Try and parse channel name
1.91 + CQ cqChannelName = row.Cq().Find("td:nth-child(3) > a");
1.92 + if (cqChannelName.Length == 0)
1.93 + {
1.94 + cqChannelName = row.Cq().Find("td:nth-child(3) > i");
1.95 + if (cqChannelName.Length == 0)
1.96 + {
1.97 + //Can't get channel name
1.98 + Debug.Write("WARNING: Can't find channel name! Skipping this channel");
1.99 + continue;
1.100 + }
1.101 + }
1.102 +
1.103 + channel.Name = WebUtility.HtmlDecode(cqChannelName.Get(0).InnerText).Trim();
1.104 + if (channel.Name == "Name")
1.105 + {
1.106 + //Skipping header rows
1.107 + continue;
1.108 + }
1.109 +
1.110 + //So we have a channel name get the other properties then
1.111 + channel.Country = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(4)").Get(0).InnerText).Trim();
1.112 + channel.Category = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(5)").Get(0).InnerText).Trim();
1.113 + //Skip the packages
1.114 + //Skip the encryptions
1.115 + channel.SID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(8)").Get(0).InnerText).Trim();
1.116 + channel.VPID = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(9)").Get(0).InnerText).Trim();
1.117 + //Skip audios
1.118 + channel.PMT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
1.119 + channel.PCR = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
1.120 + channel.TXT = WebUtility.HtmlDecode(row.Cq().Find("td:nth-child(11)").Get(0).InnerText).Trim();
1.121 +
1.122 + //Append that new channel to our list
1.123 + channels.Add(channel);
1.124 +
1.125 + //Show it in debug output
1.126 + Debug.Write(channel);
1.127 + } //For each channel
1.128 + } //For each frequency
1.129 +
1.130 + return channels;
1.131 + }
1.132 + }
1.133 +}