public async void Create(int provider, string response,
                                 Dictionary <string, string> parameters, HttpListenerContext context)
        {
            var playlist = "#EXTM3U\r\n";
            var channels = ChannelProvider.GetChannels();

            if (channels.Count() != 0 && context != null)
            {
                foreach (var channel in channels)
                {
                    var server = channel.Servers[new Random().Next(0, (channel.Servers.Count - 1))].URL;
                    var name   = channel.Name;
                    var logo   = channel.Logo;
                    var type   = channel.Type;
                    var epgid  = channel.ID;

                    var prov = ChannelProvider.GetProviderNameByID(db, provider);
                    playlist += string.Format("#EXTINF:-1 tvg-name=\"{2}\" group-title=\"{3}\" tvg-id=\"{0}\",{2}\r\n",
                                              epgid == 0 ? string.Empty : epgid.AsString(), logo, name, prov.Result);

                    playlist += string.Format("{0}\r\n", (type != 2 || !server.EndsWith(".ts")) ? server :
                                              string.Format("http://{1}:83/tsproxy/?url={0}", server, context.Request.Headers["Host"].Split(':')[0]));
                }
            }

            Task.Run(() => Console.WriteLine("Playlist created!"));
            Task.Run(() => MakeResponse(provider, playlist, BackendAction.Create, parameters, context));
        }
Exemple #2
0
        async Task <string> GetEPGProviderURL(int list)
        {
            var url = string.Empty;

            if (db.Count("epg_lists", "id", list).Result != 0)
            {
                var urls = db.SQLQuery <ulong>(string.Format("SELECT url FROM epg_lists WHERE id='{0}'", list)).Result;
                lock (urls)
                {
                    var days = db.SQLQuery(string.Format("SELECT days FROM epg_lists WHERE id='{0}'", list), "days").Result;
                    lock (days)
                    {
                        var epg_timespan = DateTime.Today.AddDays(-1);
                        url = urls[(ulong)new Random().Next(0, urls.Count - 1)]["url"];

                        var IDs = ChannelProvider.GetChannelIDs();

                        url = url.Replace("[#EPGID#]", string.Join(",", IDs));
                        url = url.Replace("[#SDAY#]", Functions.formatNumberValue(epg_timespan.Day));
                        url = url.Replace("[#SMONTH#]", Functions.formatNumberValue(epg_timespan.Month));
                        url = url.Replace("[#YEAR#]", Functions.formatNumberValue(epg_timespan.Year));
                        url = url.Replace("[#EDAY#]", Functions.formatNumberValue(epg_timespan.AddDays(double.Parse(days)).Day));
                        url = url.Replace("[#EMONTH#]", Functions.formatNumberValue(epg_timespan.AddDays(double.Parse(days)).Month));
                    }
                }
            }

            return(url);
        }
Exemple #3
0
        public async void Download(int provider, string response, Dictionary <string, string> parameters, HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(parameters["epglist"]))
            {
                return;
            }

            lock (parameters)
            {
                var epg_url = GetEPGProviderURL(int.Parse(parameters["epglist"])).Result;

                if (!string.IsNullOrEmpty(epg_url))
                {
                    var prov = ChannelProvider.GetProviderNameByID(db, provider);
                    var wc   = new HTTPClient(epg_url);
                    wc.GetResponse(prov.Result);
                    wc.HTTPClientResponse += (sender, e) =>
                    {
                        var res = e.Data;

                        if (!string.IsNullOrEmpty(res))
                        {
                            EPGData = ImportFormJson(res, int.Parse(parameters["epglist"])).Result;
                        }
                    };
                }
            }

            Task.Run(() => MakeResponse(provider, response, BackendAction.Download, parameters, context));
        }
        public async void Update(int provider, string response, Dictionary <string, string> parameters, HttpListenerContext context)
        {
            foreach (var c in (from c in ChannelProvider.GetChannels() where c.Servers.Count != 0 select c).AsEnumerable())
            {
                foreach (var s in (from s in c.Servers where !s.URL.StartsWith("rtmp") select s).AsEnumerable())
                {
                    if (!TestChannelUrl(c.Name, s.URL, Functions.GetUseragentByID(db, s.UserAgent).Result).Result)
                    {
                        db.SQLInsert(string.Format("DELETE FROM servers WHERE url='{0}'", s.URL)).ConfigureAwait(false);
                    }
                }
            }

            Task.Run(() => MakeResponse(provider, string.Empty, BackendAction.Update, parameters, context));
        }
Exemple #5
0
        public BackendHub(ref SQLDatabase db, Network.Backend backend)
        {
            channel = new ChannelProvider(this);
            channel.ChannelProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.context;
                evArgs.Target   = BackendTarget.Channel;

                switch (e.Action)
                {
                case BackendAction.Create:
                    MakeRequest(BackendTarget.Channel, BackendAction.Import, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Epg, BackendAction.Download, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    MakeRequest(BackendTarget.Epg, BackendAction.Remove, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Epg, e.Action, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Download, e.context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            playlist = new PlayListProvider(ref db, this);
            playlist.PlayListProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.Context;
                evArgs.Target   = BackendTarget.Playlist;

                switch (e.Action)
                {
                case BackendAction.Create:
                    if (e.Context != null)
                    {
                        BackendHubResponse?.Invoke(this, evArgs);
                    }
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Channel, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Channel, BackendAction.Import, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    BackendHubResponse?.Invoke(this, evArgs);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            epg = new EPGProvider(ref db, this);
            epg.EPGProviderResponse += (sender, e) =>
            {
                var evArgs = new BackendHubResponseEventArgs();
                evArgs.Response = e.Response;
                evArgs.Context  = e.Context;
                evArgs.Target   = BackendTarget.Epg;

                switch (e.Action)
                {
                case BackendAction.Create:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Import:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Update, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Remove:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Remove, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Update:
                    MakeRequest(BackendTarget.Playlist, BackendAction.Update, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                case BackendAction.Download:
                    MakeRequest(BackendTarget.Epg, BackendAction.Create, e.Context, e.Parameters, e.Response, e.Provider);
                    break;

                default:
                    break;
                }
            };

            backend.BackendHubRequest += (sender, e) =>
            {
                MakeRequest(e.Target, e.Action, e.Context, e.Parameters, e.Response, 0);
            };

            var providers = db.SQLQuery <uint>("SELECT * FROM providers WHERE id !='0' AND skip !='1'").Result;

            if (providers.Count != 0)
            {
                for (var i = uint.MinValue; i < providers.Count; i++)
                {
                    var x = new Dictionary <string, string>();
                    x.Add("chanlist", providers[i]["channel_list"]);
                    x.Add("logolist", providers[i]["logo_list"]);
                    x.Add("epglist", providers[i]["epg_list"]);
                    x.Add("playlists", providers[i]["playlist_urls"]);

                    var clist    = providers[i]["channel_list"];
                    var provider = int.Parse(providers[i]["id"]);

                    Task.Run(() => MakeRequest((clist != "0") ? BackendTarget.Channel :
                                               BackendTarget.Playlist, BackendAction.Download, null, x, string.Empty, provider));
                }
            }
            else
            {
                Task.Run(() => MakeRequest(BackendTarget.Playlist, BackendAction.Download,
                                           null, new Dictionary <string, string>(), string.Empty, 0));
            }
        }
Exemple #6
0
        public async void Create(int provider, string response,
                                 Dictionary <string, string> parameters, HttpListenerContext context)
        {
            if (EPGData != null)
            {
                var xml  = new XmlDocument();
                var root = xml.CreateElement("tv");

                root.SetAttribute("generator-info-name", string.Format("Generated from {0} EPG data by Netvision IPTV Backend",
                                                                       ChannelProvider.GetProviderNameByID(db, provider).Result));

                root.SetAttribute("generator-info-url", "https://github.com/LipkeGu/Netvision/");

                var channels = ChannelProvider.GetChannels();

                foreach (var channel in channels)
                {
                    if (channel.ID == 0)
                    {
                        continue;
                    }

                    var cnode = xml.CreateElement("channel");
                    cnode.SetAttribute("id", channel.ID.AsString());

                    var dn = xml.CreateElement("display-name");
                    dn.SetAttribute("lang", "de");
                    dn.InnerText = channel.Name;
                    cnode.AppendChild(dn);

                    root.AppendChild(cnode);
                }

                Task.Run(() => Console.WriteLine("Generating epgdata.xml.gz..."));
                var epgchannels = (from c in EPGData where c.Value.Count() != 0 select c).OrderBy(c => c.Key);
                if (epgchannels.Count() != 0)
                {
                    foreach (var item in epgchannels)
                    {
                        Task.Run(() =>
                        {
                            for (var i = 0; i < item.Value.Count(); i++)
                            {
                                lock (item.Value[i])
                                {
                                    var programm = xml.CreateElement("programme");
                                    programm.SetAttribute("start", item.Value[i].Start);
                                    programm.SetAttribute("stop", item.Value[i].Stop);
                                    programm.SetAttribute("channel", item.Key);

                                    var title = xml.CreateElement("title");
                                    title.SetAttribute("lang", "de");
                                    title.InnerText = item.Value[i].Title;
                                    programm.AppendChild(title);

                                    if (!string.IsNullOrEmpty(item.Value[i].Description))
                                    {
                                        var desc = xml.CreateElement("desc");
                                        desc.SetAttribute("lang", "de");
                                        desc.InnerText = item.Value[i].Description;
                                        programm.AppendChild(desc);
                                    }

                                    if (item.Value[i].Person != null)
                                    {
                                        if (item.Value[i].Person.Count != 0)
                                        {
                                            var credits = xml.CreateElement("credits");

                                            var actors = (from a in item.Value[i].Person
                                                          where a.kind == "actor"
                                                          select a.name);

                                            foreach (var a in actors)
                                            {
                                                var actor       = xml.CreateElement("actor");
                                                actor.InnerText = a;
                                                credits.AppendChild(actor);
                                            }

                                            var directors = (from a in item.Value[i].Person
                                                             where a.kind == "director"
                                                             select a.name);

                                            foreach (var d in directors)
                                            {
                                                var director       = xml.CreateElement("director");
                                                director.InnerText = d;

                                                credits.AppendChild(director);
                                            }

                                            programm.AppendChild(credits);
                                        }
                                    }

                                    var icon = item.Value[i].Images;
                                    if (!string.IsNullOrEmpty(icon))
                                    {
                                        var ico = xml.CreateElement("icon");
                                        ico.SetAttribute("src", icon);

                                        programm.AppendChild(ico);
                                    }


                                    if (!string.IsNullOrEmpty(item.Value[i].Series_Episodes))
                                    {
                                        var episode = xml.CreateElement("episode-num");
                                        episode.SetAttribute("system", "onscreen");
                                        episode.InnerText = string.Format("S{0} E{1}/{2}",
                                                                          item.Value[i].Series_Season,
                                                                          item.Value[i].Series_Episode,
                                                                          item.Value[i].Series_Episodes);

                                        programm.AppendChild(episode);
                                    }

                                    if (!string.IsNullOrEmpty(item.Value[i].Date))
                                    {
                                        var year       = xml.CreateElement("date");
                                        year.InnerText = item.Value[i].Date;

                                        programm.AppendChild(year);
                                    }

                                    if (!string.IsNullOrEmpty(item.Value[i].Country))
                                    {
                                        var country       = xml.CreateElement("country");
                                        country.InnerText = item.Value[i].Country;

                                        programm.AppendChild(country);
                                    }

                                    foreach (var cat in item.Value[i].Categories)
                                    {
                                        var c = xml.CreateElement("category");
                                        c.SetAttribute("lang", "de");
                                        c.InnerText = cat.text;

                                        programm.AppendChild(c);
                                    }

                                    if (!string.IsNullOrEmpty(item.Value[i].SubTitle))
                                    {
                                        var ss = xml.CreateElement("sub-title");
                                        ss.SetAttribute("lang", "de");
                                        ss.InnerText = item.Value[i].SubTitle;

                                        programm.AppendChild(ss);
                                    }

                                    root.AppendChild(programm);
                                }
                            }
                        });
                    }
                }

                xml.AppendChild(root);
                xml.InsertBefore(xml.CreateXmlDeclaration("1.0", "UTF-8", null), root);

                fs.Delete("epgdata.xml");
                xml.Save("epgdata.xml");

                Task.Run(() =>
                {
                    fs.GZipCompress("epgdata.xml");
                    Console.WriteLine("EPG informations saved to: epgdata.xml.gz...");
                });
            }

            if (EPGData != null)
            {
                EPGData.Clear();
            }

            Task.Run(() => MakeResponse(provider, string.Empty, BackendAction.Create, parameters, context));
        }