/** This code is distributed by GNU GENERAL PUBLIC LICENSE Version 2, June 1991 and was created and programed by Felipe SJA. Have fun ... also is something breaks because of this code, ell not my fault it could be broken. */ using System; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; using EtiVo; using System.Security.Cryptography.X509Certificates; using System.Runtime.InteropServices; using System.Collections; using System.Configuration; namespace ConsoleApplication1 { class DownloadInfo{ public string _name; public string _url; public ulong _size; public string _date; public string _id; public DownloadInfo(string name, string url, string size,string date){ _name=name; _url=url; _size=parseSize(size); _date= date; _id = ConsoleApplication1.Class1.singleReg("id=(\\d+)",url); } ulong parseSize(string s){ string[] all = s.Split(new char[]{' '},2); //Console.WriteLine(all[0]); double d = Double.Parse(all[0]); return (ulong)(d * (all[1]=="GB"?1000000:1000)); } } public class MyPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint , X509Certificate certificate , WebRequest request , int certificateProblem) { //Return True to force the certificate to be accepted. return true; } // end CheckValidationResult } // class MyPolicy class Class1 { // static CookieContainer ccContainer = null; static string sid=null; [DllImport("kernel32.dll")] public static extern bool GetDiskFreeSpaceEx( string lpDirectoryName, out UInt64 lpFreeBytesAvailable, out UInt64 lpTotalNumberOfBytes, out UInt64 lpTotalNumberOfFreeBytes); public static ulong getFreeSpace(){ ulong freeBytesAvailable = 0; ulong totalNumberOfBytes = 0; ulong totalNumberOfFreeBytes = 0; GetDiskFreeSpaceEx( "c:\\", out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes); return freeBytesAvailable; } public static string qReplace(string source, string pat, string rep) { Regex r = new Regex(pat); return r.Replace(source,rep,-1,0); } public static HttpWebRequest GenerateWebRequest(string url, string method, string postData) { if ((method.ToUpper()!="GET") && (method.ToUpper()!="POST")) method="GET"; HttpWebRequest req = null; Uri uri = new Uri(url); req=(HttpWebRequest)WebRequest.Create(uri); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; " + "Windows NT 5.0; .NET CLR 1.0.3705)"; if (method.ToUpper() == "POST"){ byte[] bytes = Encoding.UTF8.GetBytes(postData); req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength=postData.Length; Stream reqStream = req.GetRequestStream(); reqStream.Write(bytes,0,bytes.Length); reqStream.Close(); } return req; } public static bool binWget(string url,string fileName) { string newFile = qReplace(fileName,":| ","-"); if (File.Exists(newFile)) return false; HttpWebRequest req = GenerateWebRequest(url,"GET",null); req.Credentials = genGredentials(url); req.CookieContainer = new CookieContainer(); //req.CookieContainer.Add(ccContainer.GetCookies(new Uri(url))); if (sid!=null) req.CookieContainer.Add(new Uri(url),new Cookie("sid",sid,"/")); else Console.WriteLine("binSucks"); FileStream outt = null; try { HttpWebResponse resp= (HttpWebResponse)req.GetResponse(); byte[] buff = new byte[2048]; Stream inn =resp.GetResponseStream(); string nsid = singleReg("sid=(.*?);", resp.Headers.ToString()); if (nsid != null) sid=nsid; //string newFile = qReplace(fileName,":| ","-"); outt = File.Create(qReplace(fileName,":| ","-")); int datas = 0; while ((datas= inn.Read(buff,0,2048)) > 0){ outt.Write(buff,0,datas); } //while (respStream.Read( outt.Close(); return true; } catch(Exception e) { if (outt != null) outt.Close(); Console.WriteLine(e.ToString()); } return false; } public static CredentialCache genGredentials(string url){ CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri(url), "Digest", new NetworkCredential("tivo",getStringConf("mediakey"),"TiVo DVR")); return myCache; } public static string wGet(string url) { HttpWebRequest req = GenerateWebRequest(url,"GET",null); req.Credentials = genGredentials(url); req.CookieContainer = new CookieContainer(); if (sid!=null) req.CookieContainer.Add(new Uri(url),new Cookie("sid",sid,"/")); else Console.WriteLine("Sucks"); //Console.WriteLine(url); try { HttpWebResponse resp= (HttpWebResponse)req.GetResponse(); string nsid = singleReg("sid=(.*?);", resp.Headers.ToString()); if (nsid != null) sid=nsid; StreamReader respStream = new StreamReader(resp.GetResponseStream(),Encoding.UTF8); string html = respStream.ReadToEnd(); respStream.Close(); return html; } catch(Exception e){ Console.WriteLine(e.ToString()); } return null; } /// /// The main entry point for the application. /// public static string[] getLinks(string html,string linkPattern){ string patern = "(.*?)"; Regex RE = new Regex(patern, RegexOptions.Multiline); MatchCollection colo = RE.Matches(html); ArrayList ret = new ArrayList(); for(int i=0;i|(.*?)",RegexOptions.Multiline); MatchCollection colo2=re2.Matches(m.Groups[1].Value); //Console.WriteLine(m.Groups[1].Value); // Console.WriteLine(colo2.Count); if (colo2.Count > 5) { // Console.WriteLine("Name" + colo2[1].Groups[1]); // Console.WriteLine("Desc" + colo2[2].Groups[1]); string desc = singleReg("(.*?)",colo2[2].Groups[1].Value); // Console.WriteLine("Desc:" + desc); string date = colo2[3].Groups[1].Value; date = date.Replace("
",""); date = date.Replace("/","-"); Console.WriteLine("Date:" + date); string size = singleReg("
(.*)",colo2[4].Groups[1].Value); // Console.WriteLine("Size:" + size); string link = singleReg("",colo2[5].Groups[1].Value); // Console.WriteLine("Link:" + link); if (link != null) l.Add(new DownloadInfo(desc, link, size,date)); else Console.WriteLine("Null URL:"+desc); } else{ Console.WriteLine("No match "+colo2.Count+":"+m.Groups[1].Value); for (int k=0;k:" + getStringConf("pattern")); ShowConfig(); while(true){ try { execDls(); } catch(Exception e) { Console.WriteLine("There was an error ... I will go on"); Console.WriteLine(e.ToString()); } System.Threading.Thread.Sleep(1000000); } //Console.WriteLine("HeyHEyHey"); } static void execDls(){ // little test FileStream fs4 = File.Create(getStringConf("targetdir")+"test.lock"); fs4.Close(); File.Delete(getStringConf("targetdir")+"test.lock"); Console.WriteLine(System.DateTime.Now); //EtiVo.Interop.DShowFilterDump d = new EtiVo.Interop.DShowFilterDump(); EtiVo.Interop.DShowFilterDump d = new EtiVo.Interop.DShowFilterDump(); ServicePointManager.CertificatePolicy = new MyPolicy(); string root = "https://"+getStringConf("tivoip")+"/nowplaying/"; string html = wGet(root); if (html == null) return; string pattern = getStringConf("pattern"); string mpeg_path = getStringConf("targetdir"); //Console.WriteLine(html); string[] firstLevel = getLinks(html,"folder"); ArrayList allDls = new ArrayList(); foreach(string l1 in firstLevel){ // Console.WriteLine(">>"+root+l1); // we need to replace & for & ... why? because this way it works ... string inHtml = wGet(root+qReplace(l1,"&","&")); //Console.WriteLine(inHtml); ArrayList downloadElements = parseTable(inHtml); allDls.InsertRange(allDls.Count,downloadElements); } ulong freeSpace = getFreeSpace(); foreach (DownloadInfo di in allDls){ Console.WriteLine("================================="); string file_base_name = genName(di); bool DL_ALL = true; Console.WriteLine(file_base_name); if (di._url == null) { Console.WriteLine("Url null"); continue; } if (pattern != null && !(Regex.Match(di._name,pattern).Success)) { Console.WriteLine("Not a match"); continue; } try { if ((File.Exists(file_base_name+".TiVo")) || (!binWget(di._url,file_base_name+".TiVo"))) { Console.WriteLine("File exists or DL failed"); continue; } } catch(Exception){ // Don't panic ... delete the file and go on try {File.Delete(file_base_name+".TiVo");} catch(Exception){} continue; } Console.WriteLine(getFreeSpace() + " ? " +di._size); if (getFreeSpace() > di._size && !File.Exists(file_base_name+".mpeg")) { string inputName = file_base_name+".TiVo"; string outputName = mpeg_path+file_base_name+".mpeg"; if (!File.Exists(inputName)) Console.WriteLine("huh? no file? WTF!! " + inputName); else Console.WriteLine("Have file:>>" + inputName+"<<"); try { // Create an empty lock file FileStream fs = File.Create(outputName+".lock"); fs.Close(); d = new EtiVo.Interop.DShowFilterDump(); d.GenerateOutput(inputName,outputName); d.Dispose(); File.Delete(outputName+".lock"); } catch(Exception e){ try{File.Delete(outputName+".lock");} catch(Exception){} try{File.Delete(outputName);} catch(Exception){} try{File.Delete(inputName);} catch(Exception){} // Ok ... so it failed .. I better delete stuff Console.WriteLine(e.ToString()); Console.ReadLine(); return; } File.Delete(file_base_name+".TiVo"); // This is just to avoid repeated FileStream fs2 = File.Create(file_base_name+".TiVo"); fs2.Close(); } else{ Console.WriteLine("Sorry ... got no more space"); } Console.WriteLine(di._size + " " + di._name); } Console.WriteLine("One round:"+System.DateTime.Now); //Console.ReadLine(); } } }