private void BtnDownload_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (radioJSON.Checked) { saveFileDialog.Filter = "JSON Files | *.json"; } else { saveFileDialog.Filter = "TXT Files | *.txt"; } saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { ChatDownloadInfo info; if (isVod) { int startTime = 0; int duration = 0; if (checkCropStart.Checked) { TimeSpan start = new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value); startTime = (int)Math.Round(start.TotalSeconds); } if (checkCropEnd.Checked) { TimeSpan end = new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value); duration = (int)Math.Ceiling(end.TotalSeconds - startTime); } else { TimeSpan vodLength = TimeSpan.Parse(Regex.Replace(videoData["data"][0]["duration"].ToString(), @"[^\d]", ":").TrimEnd(':')); duration = (int)Math.Ceiling(vodLength.TotalSeconds); } info = new ChatDownloadInfo(isVod, textUrl.Text, saveFileDialog.FileName, videoData["data"][0]["id"].ToString(), startTime, duration, radioJSON.Checked, labelStreamer.Text, streamerId); } else { info = new ChatDownloadInfo(isVod, textUrl.Text, saveFileDialog.FileName, videoData["vod"]["id"].ToString(), videoData["vod"]["offset"].ToObject <int>(), videoData["duration"].ToObject <double>(), radioJSON.Checked, labelStreamer.Text, streamerId); } toolStatus.Text = "Downloading"; btnGetInfo.Enabled = false; SetEnabled(false, false); backgroundDownloadManager.RunWorkerAsync(info); } }
private void BackgroundDownloadManager_DoWork(object sender, DoWorkEventArgs e) { ChatDownloadInfo clipInfo = (ChatDownloadInfo)e.Argument; using (WebClient client = new WebClient()) { client.Encoding = Encoding.UTF8; client.Headers.Add("Accept", "application/vnd.twitchtv.v5+json; charset=UTF-8"); client.Headers.Add("Client-Id", "kimne78kx3ncx6brgo4mv6wki5h1ko"); bool isFirst = true; string cursor = ""; double latestMessage = clipInfo.offset - 1; double videoStart = clipInfo.offset; double videoDuration = clipInfo.duration; JObject result = new JObject(); JArray comments = new JArray(); JObject streamer = new JObject(); streamer["name"] = clipInfo.streamer_name; streamer["id"] = clipInfo.streamer_id; while (latestMessage < (videoStart + videoDuration)) { string response; if (isFirst) { response = client.DownloadString(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?content_offset_seconds={1}", clipInfo.vod_id, clipInfo.offset)); } else { response = client.DownloadString(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?cursor={1}", clipInfo.vod_id, cursor)); } JObject res = JObject.Parse(response); foreach (var comment in res["comments"]) { if (latestMessage < (videoStart + videoDuration)) { comments.Add(comment); } latestMessage = comment["content_offset_seconds"].ToObject <double>(); } if (res["_next"] == null) { break; } else { cursor = res["_next"].ToString(); } int percent = (int)Math.Floor((latestMessage - videoStart) / videoDuration * 100); backgroundDownloadManager.ReportProgress(percent, String.Format("Downloading {0}%", percent)); if (isFirst) { isFirst = false; } } result["streamer"] = streamer; result["comments"] = comments; using (StreamWriter sw = new StreamWriter(clipInfo.path)) { if (clipInfo.is_json) { sw.Write(result.ToString(Newtonsoft.Json.Formatting.Indented)); } else { foreach (var comment in result["comments"]) { string username = comment["commenter"]["display_name"].ToString(); string message = comment["message"]["body"].ToString(); sw.WriteLine(String.Format("{0}: {1}", username, message)); } } sw.Flush(); sw.Close(); } } }
private void btnDownload_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); if (radioJson.IsChecked == true) { saveFileDialog.Filter = "JSON Files | *.json"; } else { saveFileDialog.Filter = "TXT Files | *.txt"; } saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == true) { try { ChatDownloadInfo info; if (downloadType == DownloadType.Video) { int startTime = 0; int duration = 0; if (checkStart.IsChecked == true) { TimeSpan start = new TimeSpan((int)numStartHour.Value, (int)numStartMinute.Value, (int)numStartSecond.Value); startTime = (int)Math.Round(start.TotalSeconds); } if (checkEnd.IsChecked == true) { TimeSpan end = new TimeSpan((int)numEndHour.Value, (int)numEndMinute.Value, (int)numEndSecond.Value); duration = (int)Math.Ceiling(end.TotalSeconds - startTime); } else { TimeSpan vodLength = TimeSpan.FromSeconds(videoData["length"].ToObject <int>()); duration = (int)Math.Ceiling(vodLength.TotalSeconds); } info = new ChatDownloadInfo(downloadType, textUrl.Text, saveFileDialog.FileName, videoData["_id"].ToString().Substring(1), startTime, duration, (bool)radioJson.IsChecked, textStreamer.Text, streamerId); } else { info = new ChatDownloadInfo(downloadType, textUrl.Text, saveFileDialog.FileName, videoData["vod"]["id"].ToString(), videoData["vod"]["offset"].ToObject <int>(), videoData["duration"].ToObject <double>(), (bool)radioJson.IsChecked, textStreamer.Text, streamerId); } statusMessage.Text = "Downloading"; btnGetInfo.IsEnabled = false; SetEnabled(false, false); BackgroundWorker backgroundDownloadManager = new BackgroundWorker(); backgroundDownloadManager.WorkerReportsProgress = true; backgroundDownloadManager.DoWork += BackgroundDownloadManager_DoWork; backgroundDownloadManager.ProgressChanged += BackgroundDownloadManager_ProgressChanged; backgroundDownloadManager.RunWorkerCompleted += BackgroundDownloadManager_RunWorkerCompleted; SetImage("Images/ppOverheat.gif", true); statusMessage.Text = "Downloading"; backgroundDownloadManager.RunWorkerAsync(info); } catch (Exception ex) { AppendLog("ERROR: " + ex.Message); } } }