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)); }
async Task <bool> TestChannelUrl(string name, string url, string ua) { if (string.IsNullOrEmpty(url)) { return(false); } if (url.StartsWith("rtmp")) { return(true); } var result = false; var hc = new HTTPClient(url, "HEAD"); Task.Run(() => hc.GetResponse(name)); hc.HTTPClientResponse += (sender, e) => { switch (hc.StatusCode) { case HttpStatusCode.OK: case HttpStatusCode.Moved: result = true; break; default: result = false; break; } }; return(result); }
public async void Update(int provider, string response, Dictionary <string, string> parameters, HttpListenerContext context) { var channels = GetChannels(); foreach (var channel in channels) { foreach (var server in channel.Servers) { if (server.URL.StartsWith("rtmp")) { continue; } var hc = new HTTPClient(server.URL); hc.UserAgent = Functions.GetUseragentByID(db, server.UserAgent).Result; await Task.Run(() => hc.GetResponse(channel.Name)).ContinueWith(c => { if (hc.StatusCode != HttpStatusCode.OK && hc.StatusCode != HttpStatusCode.Moved) { lock (Channels.Members) Channels.Members[channel.Name].Servers.Remove(server); Task.Run(() => Console.WriteLine("Removing URL: {0} (Status Code: {1})...", server.URL, hc.StatusCode)); } }); } } foreach (var item in channels) { var n = item.Name.FirstCharUpper(db).Result; var ch_nameid = AddChannelName(db, n.Trim()).Result; var ch_logoid = AddChannelLogo(item.Logo, item.Provider).Result; var ch_epgid = AddChannelEPGID(item.ID, item.Provider).Result; if (db.Count("channels", "name", ch_nameid).Result == 0 && db.Count("channels", "logo", ch_logoid).Result == 0 && db.Count("channels", "epgid", ch_epgid).Result == 0) { var chan_id = InsertChannel(ch_nameid, ch_logoid, ch_epgid, item.Provider, item.Type, item.ChanNo); for (var i = 0; i < item.Servers.Count; i++) { if (db.Count("servers", "url", item.Servers[i].URL).Result == 0) { await Task.Run(() => db.SQLInsert(string.Format("INSERT INTO servers (url, channel, type) VALUES ('{0}','{1}','{2}')", item.Servers[i].URL, chan_id.Result, item.Servers[i].Type))); } } } } }
async void _downloadPlayList(string url, string file, string ua, int provider) { var importDir = Path.Combine(Directory.GetCurrentDirectory(), string.Format("Download/playlist/{0}", provider)); var u = Functions.ReplaceAndFixNames(db, url, false); var dl = new HTTPClient(u.Result); dl.UserAgent = ua; dl.GetResponse(url); dl.HTTPClientResponse += (sender, e) => { if (e.Data.Length != 0) { using (var fs = new Filesystem(importDir)) fs.Write(Filesystem.Combine(fs.Root, file), e.Data); Task.Run(() => Console.WriteLine("Downloaded Playlist: {0}...", u.Result)); } }; }
public async void Download(int provider, string response, Dictionary <string, string> parameters, HttpListenerContext context) { var prov = GetProviderNameByID(db, provider); var res = string.Empty; using (var fs = new Filesystem()) { if (parameters.ContainsKey("chanlist")) { var dir = string.Format("Download/channels/{0}", parameters["chanlist"]); Directory.CreateDirectory(dir); Task.Run(() => Console.WriteLine("Downloading Channel List for Provider: {0}", prov.Result)); var lists = db.SQLQuery <uint>(string.Format("SELECT * FROM channel_lists WHERE id='{0}' AND id != '0'", parameters["chanlist"])).Result; if (lists.Count != 0) { for (var i = uint.MinValue; i < lists.Count; i++) { var hc = new HTTPClient(Functions.ReplaceAndFixNames(db, lists[i]["url"], false).Result); hc.HTTPClientError += (sender, e) => Console.WriteLine(e.Message); hc.HTTPClientResponse += (sender, e) => { hc.GetResponse(prov.Result); if (e.Data.Length != 0) { res = e.Data; fs.Write(fs.ResolvePath(Filesystem.Combine(dir, Functions.ReplaceAndFixNames(db, lists[i]["file"]).Result)), res); } }; } } } } Task.Run(() => MakeResponse(provider, res, BackendAction.Download, parameters, context)); }
public async void Import(int provider, string response, Dictionary <string, string> parameters, HttpListenerContext context) { Task.Run(() => ImportChannelsFromDataBase()); var importDir = Path.Combine(Directory.GetCurrentDirectory(), string.Format("Download/playlist/{0}", provider)); Directory.CreateDirectory(importDir); var dirinfo = new DirectoryInfo(importDir); foreach (var playlist in dirinfo.GetFiles("*.m3u8", SearchOption.AllDirectories)) { Console.WriteLine("Adding Channels from: {0}...", playlist.Name); using (var reader = new StreamReader(playlist.FullName, true)) { var name = string.Empty; var status = 0; var logo = "--nologo--"; var url = string.Empty; var ua = Functions.GetUseragentByID(db, 1); var uaid = Functions.GetUseragentIDByName(db, ua.Result); var type = 0; while (!reader.EndOfStream) { var line = reader.ReadLineAsync().Result; if (!string.IsNullOrEmpty(line)) { if (line.Length > 10) { if (line.StartsWith("#EXTINF:")) { if (line.Contains(",")) { var parts = line.Split(','); if (parts.Length > 1) { name = Functions.ReplaceAndFixNames(db, parts[1].FirstCharUpper(db).Result.Trim()).Result; } if (parts[0].Contains("tvg-logo")) { var start = parts[0].IndexOf("tvg-logo=\"") + "tvg-logo=\"".Length; var end = parts[0].IndexOf("\"", start); var tmp = parts[0].Substring(start, (end - start)).Split('/'); logo = tmp[tmp.Length - 1]; } } } else { if (line.StartsWith("http") || line.StartsWith("rtmp") || line.StartsWith("udp")) { url = line; if (url.StartsWith("http")) { var hc = new HTTPClient(url, ua.Result); hc.HTTPClientError += (sender, e) => { Console.WriteLine(e.Message); status = 0; }; hc.GetResponse(name); hc.HTTPClientResponse += (sender, e) => { switch (hc.ContentType) { case "application/x-mpegurl": case "application/vnd.apple.mpegurl": status = 1; type = 1; break; case "video/mp2t": status = 1; type = 2; break; default: status = 0; type = 0; break; } if (status != 0) { Task.Run(() => { _addchannel(url, name, type, uaid.Result, provider, logo, int.Parse(parameters["logolist"])); }); } }; } else { if (url.StartsWith("rtmp") || url.StartsWith("udp")) { status = 1; type = 1; } else { status = 1; type = 0; } } if (status != 0) { await Task.Run(() => { _addchannel(url, name, type, uaid.Result, provider, logo, int.Parse(parameters["logolist"])); }); } } } } } } } } Task.Run(() => MakeResponse(provider, string.Empty, BackendAction.Import, parameters, context)); }
async Task <Dictionary <string, List <epg_entry> > > ImportFormJson(string input, int list) { var epginfo = new Dictionary <string, List <epg_entry> >(); using (var db = new SQLDatabase("channels.db")) { var url = db.SQLQuery(string.Format("SELECT infourl FROM epg_lists WHERE id='{0}' AND id != '0'", list), "infourl").Result; if (string.IsNullOrEmpty(url)) { return(epginfo); } var json_input = fix_json_input(input); var metadata = JsonConvert.DeserializeObject <Dictionary <string, List <epg_entry> > >(json_input); if (metadata.Count != 0) { foreach (var item in metadata.OrderBy(c => c.Key)) { Task.Run(() => { var dir = string.Format("Download/epg/proginfo/{0}/{1}", list, item.Key); var infolist = new List <epg_entry>(); infolist.Capacity = ushort.MaxValue; Directory.CreateDirectory(dir); for (var i = 0; i < item.Value.Count; i++) { using (var fs = new Filesystem()) { var res = string.Empty; var progid = item.Value[i].ProgID; var f = fs.ResolvePath(Filesystem.Combine(dir, string.Format("{0}.json", progid))); if (!fs.Exists(f) || fs.Length(f) == 0) { var hc = new HTTPClient(url.Replace("[#PROGID#]", progid)); hc.GetResponse(item.Value[i].title); hc.HTTPClientResponse += (sender, e) => { if (e.Data.Length != 0) { res = fix_json_input(e.Data); fs.Write(f, res); } }; } else { res = fs.ReadText(f, Encoding.UTF8).Result; } if (!string.IsNullOrEmpty(res)) { infolist.Add(JsonConvert.DeserializeObject <epg_entry>(res)); } } } epginfo.Add(item.Key, infolist); infolist.Clear(); }); } metadata.Clear(); } } return(epginfo); }