using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
struct DownloadInfo
{
public string filename;
public string temp;
public string url;
}
static void Main(string[] args)
{
Console.WriteLine("Fernsehkritik-TV Downloader");
Console.WriteLine("(C) 2011 - KN4CK3R");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://fernsehkritik.tv/tv-magazin/");
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
int maxEpisode = -1;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string raw = sr.ReadToEnd();
raw = raw.Substring(raw.IndexOf(">Folge ") + ">Folge ".Length);
raw = raw.Substring(0, raw.IndexOf(' '));
maxEpisode = int.Parse(raw);
}
Console.Write("Folge auswählen (aktuell: " + maxEpisode + "): ");
int downloadEpisode = -1;
if (!int.TryParse(Console.ReadLine(), out downloadEpisode))
{
return;
}
if (downloadEpisode < 1 || downloadEpisode > maxEpisode)
{
return;
}
request = (HttpWebRequest)WebRequest.Create("http://fernsehkritik.tv/folge-" + downloadEpisode + "/play/");
request.Proxy = null;
response = (HttpWebResponse)request.GetResponse();
List<DownloadInfo> fileList = new List<DownloadInfo>();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string raw = sr.ReadToEnd();
raw = raw.Substring(raw.IndexOf("type=\"video/mp4\" src=\"") + "type=\"video/mp4\" src=\"".Length);
string baseUrl = raw.Substring(0, raw.IndexOf('"'));
DownloadInfo info = new DownloadInfo();
info.filename = "FernsehKritik-TV_E" + downloadEpisode + ".mp4";
info.url = baseUrl;
info.temp = Application.StartupPath + @"\" + info.filename;
DownloadFile(info);
}
Console.WriteLine();
Console.WriteLine("Vorgang abgeschlossen");
}
static void DownloadFile(DownloadInfo info)
{
if (!File.Exists(info.temp))
{
Console.WriteLine("Download file '" + info.filename + "'");
using (WebClientTimeOut client = new WebClientTimeOut())
{
client.Proxy = null;
client.TimeOut = int.MaxValue;
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs eventArgs)
{
Console.Write("\r{0}%", eventArgs.ProgressPercentage);
});
client.DownloadFileTaskAsync(new Uri(info.url), info.temp).Wait();
//client.DownloadFile(info.url, info.temp);
}
}
//Console.WriteLine("Download file '" + info.filename + "' complete");
}
static void JoinFiles(string outfile, List<DownloadInfo> fileList)
{
if (!File.Exists(outfile))
{
Process bind = new Process();
bind.StartInfo.FileName = "FlvBind.exe";
bind.StartInfo.Arguments = outfile;
foreach (DownloadInfo info in fileList)
{
bind.StartInfo.Arguments += " \"" + info.temp + "\"";
}
bind.StartInfo.UseShellExecute = false;
bind.StartInfo.RedirectStandardOutput = true;
bind.Start();
Console.WriteLine(bind.StandardOutput.ReadToEnd());
bind.WaitForExit();
}
}
static void ConvertToMP4(string source, string destination)
{
Process convert = new Process();
convert.StartInfo.FileName = "ffmpeg.exe";
convert.StartInfo.Arguments = "-i \"" + source + "\" -vcodec libx264 -acodec libvo_aacenc -ab 96 -ac 2 -ar 22050 \"" + destination + "\"";
convert.StartInfo.UseShellExecute = false;
convert.StartInfo.RedirectStandardOutput = true;
convert.Start();
Console.WriteLine(convert.StandardOutput.ReadToEnd());
convert.WaitForExit();
}
}
public class WebClientTimeOut : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest webRequest = base.GetWebRequest(address);
webRequest.Timeout = TimeOut;
return webRequest;
}
public int TimeOut { set; get; }
}
}