Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lay295 committed Nov 15, 2019
1 parent 7f1d2d6 commit 2f683ab
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

### TwitchDownloader
A Twitch VOD/Clip/Chat downloader I wrote, as well as a chat render feature

A Twitch VOD/Clip/Chat downloader I wrote, as well as a chat render feature.
**THIS USES UNDOCUMENTED API ENDPOINTS, MAY BREAK EASILY. I'LL TRY AND UPDATE WHEN IT DOES. THIS PROGRAM REQUIRES [C++ 2015 Redistributable](https://www.microsoft.com/en-us/download/details.aspx?id=52685) TO RENDER CHAT.**
![](https://i.imgur.com/8XyVD00.gif)

## Chat render example
Expand All @@ -18,4 +18,4 @@ https://www.youtube.com/watch?v=0W3MhfhnYjk

## Things still needed to be done
- Fix bugs that slipped by
- More options for chat rendering
- More options for chat rendering
7 changes: 7 additions & 0 deletions TwitchDownloaderWPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
<ResourceDictionary Source="Themes/IG/IG.MSControls.Core.Implicit.xaml" />
<ResourceDictionary Source="Themes/IG/IG.MSControls.Toolkit.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush x:Key="FixBrush" EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#FFFAFBFB"
Offset="0" />
<GradientStop Color="#FFF4F4F4"
Offset="1" />
</LinearGradientBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
6 changes: 6 additions & 0 deletions TwitchDownloaderWPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace TwitchDownloaderWPF
{
Expand All @@ -13,5 +15,9 @@ namespace TwitchDownloaderWPF
/// </summary>
public partial class App : Application
{
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
File.AppendAllText("errors.txt", e.Exception.ToString());
}
}
}
2 changes: 2 additions & 0 deletions TwitchDownloaderWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand Down Expand Up @@ -29,6 +30,7 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}

private void btnVodDownload_Click(object sender, RoutedEventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderWPF/PageChatDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private string ValidateUrl(string text)
else if (text.Contains("twitch.tv/videos/"))
{
int number;
Uri url = new Uri(text);
Uri url = new UriBuilder(text).Uri;
string path = String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
bool success = Int32.TryParse(Regex.Match(path, @"\d+").Value, out number);
if (success)
Expand All @@ -151,7 +151,7 @@ private string ValidateUrl(string text)
}
else if (text.Contains("clips.twitch.tv/") || clipRegex.IsMatch(text))
{
Uri url = new Uri(text);
Uri url = new UriBuilder(text).Uri;
string path = String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
return path.Split('/').Last();
}
Expand Down
7 changes: 4 additions & 3 deletions TwitchDownloaderWPF/PageChatRender.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
xmlns:local="clr-namespace:TwitchDownloaderWPF"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="800"
Title="PageChatRender" Initialized="Page_Initialized" Unloaded="Page_Unloaded">
Title="PageChatRender" Initialized="Page_Initialized" Unloaded="Page_Unloaded"
>

<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -40,7 +41,7 @@
<StackPanel Orientation="Vertical">
<ComboBox x:Name="comboFont" MinWidth="100"/>
<TextBox x:Name="textFontSize" Text="12" Width="40" HorizontalAlignment="Left" Margin="0,7,0,0"/>
<xctk:ColorPicker x:Name="colorFont" SelectedColor="#111111" Width="50" UsingAlphaChannel="False" Margin="0,5,0,0" HorizontalAlignment="Left"/>
<xctk:ColorPicker x:Name="colorFont" SelectedColor="#111111" Width="50" UsingAlphaChannel="False" Margin="0,5,0,0" HorizontalAlignment="Left" DropDownBackground="{StaticResource ResourceKey=FixBrush}"/>
<TextBox x:Name="textBitrate" Text="4000" Width="50" Margin="0,5,0,0" HorizontalAlignment="Left"/>
<CheckBox x:Name="checkOutline" Margin="0,5,0,0"/>
<CheckBox x:Name="checkTimestamp" Margin="0,5,0,0"/>
Expand All @@ -55,7 +56,7 @@
<TextBlock Text="BTTV Emotes:" HorizontalAlignment="Right" Margin="0,5,0,0"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<xctk:ColorPicker x:Name="colorBackground" SelectedColor="#111111" Width="50" UsingAlphaChannel="False"/>
<xctk:ColorPicker x:Name="colorBackground" SelectedColor="#111111" Width="50" UsingAlphaChannel="False" DropDownBackground="{StaticResource ResourceKey=FixBrush}"/>
<TextBox x:Name="textHeight" Text="600" Margin="0,5,0,0"/>
<TextBox x:Name="textWidth" Text="350" Margin="0,5,0,0"/>
<TextBox x:Name="textUpdateTime" Text="1.0" Margin="0,5,0,0"/>
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageClipDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private string ValidateUrl(string text)
}
else if (text.Contains("clips.twitch.tv/") || clipRegex.IsMatch(text))
{
Uri url = new Uri(text);
Uri url = new UriBuilder(text).Uri;
string path = String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
return path.Split('/').Last();
}
Expand Down
39 changes: 32 additions & 7 deletions TwitchDownloaderWPF/PageVodDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -70,8 +71,16 @@ private async void btnGetInfo_Click(object sender, RoutedEventArgs e)
string thumbUrl = taskInfo.Result["data"][0]["thumbnail_url"].ToString().Replace("%{width}", 512.ToString()).Replace("%{height}", 290.ToString());
Task<BitmapImage> thumbImage = InfoHelper.GetThumb(thumbUrl);
Task<string[]> taskPlaylist = InfoHelper.GetVideoPlaylist(videoId, taskAccessToken.Result["token"].ToString(), taskAccessToken.Result["sig"].ToString());
await Task.WhenAll(thumbImage, taskPlaylist);

await taskPlaylist;
try
{
await thumbImage;
}
catch
{
AppendLog("ERROR: Unable to find thumbnail");
}

comboQuality.Items.Clear();
videoQualties.Clear();
string[] playlist = taskPlaylist.Result;
Expand All @@ -91,15 +100,16 @@ private async void btnGetInfo_Click(object sender, RoutedEventArgs e)
}
comboQuality.SelectedIndex = 0;

imgThumbnail.Source = thumbImage.Result;
TimeSpan vodLength = TimeSpan.Parse(Regex.Replace(taskInfo.Result["data"][0]["duration"].ToString(), @"[^\d]", ":").TrimEnd(':'));
if (!thumbImage.IsFaulted)
imgThumbnail.Source = thumbImage.Result;
TimeSpan vodLength = GenerateTimespan(taskInfo.Result["data"][0]["duration"].ToString());
textStreamer.Text = taskInfo.Result["data"][0]["user_name"].ToString();
textTitle.Text = taskInfo.Result["data"][0]["title"].ToString();
textCreatedAt.Text = taskInfo.Result["data"][0]["created_at"].ToString();
numEndHour.Value = vodLength.Hours;
numEndMinute.Value = vodLength.Minutes;
numEndSecond.Value = vodLength.Seconds;
labelLength.Text = String.Format("{0}:{1}:{2}", vodLength.Hours, vodLength.Minutes, vodLength.Seconds);
labelLength.Text = String.Format("{0:00}:{1:00}:{2:00}", vodLength.TotalHours, vodLength.Minutes, vodLength.Seconds);

SetEnabled(true);
}
Expand All @@ -116,6 +126,21 @@ private async void btnGetInfo_Click(object sender, RoutedEventArgs e)
}
}

private TimeSpan GenerateTimespan(string input)
{
//There might be a better way to do this, gets string 0h0m0s and returns timespan
TimeSpan returnSpan = new TimeSpan(0);
string[] inputArray = input.Remove(input.Length-1).Replace('h', ':').Replace('m', ':').Split(':');

returnSpan = returnSpan.Add(TimeSpan.FromSeconds(Int32.Parse(inputArray[inputArray.Length - 1])));
if (inputArray.Length > 1)
returnSpan = returnSpan.Add(TimeSpan.FromMinutes(Int32.Parse(inputArray[inputArray.Length - 2])));
if (inputArray.Length > 2)
returnSpan = returnSpan.Add(TimeSpan.FromHours(Int32.Parse(inputArray[inputArray.Length - 3])));

return returnSpan;
}

private void btnDownload_Click(object sender, RoutedEventArgs e)
{
bool isValid = ValidateInput();
Expand Down Expand Up @@ -197,7 +222,7 @@ private void BackgroundDownloadManager_DoWork(object sender, DoWorkEventArgs e)
for (int i = 0; i < videoChunks.Length; i++)
{
if (videoChunks[i].Contains("#EXTINF"))
videoList.Add(new KeyValuePair<string, double>(videoChunks[i + 1], Double.Parse(videoChunks[i].Remove(0, 8).TrimEnd(','))));
videoList.Add(new KeyValuePair<string, double>(videoChunks[i + 1], Double.Parse(videoChunks[i].Remove(0, 8).TrimEnd(','), CultureInfo.InvariantCulture)));
}
}
Queue<string> videoParts = new Queue<string>(GenerateCroppedVideoList(videoList, options));
Expand Down Expand Up @@ -426,7 +451,7 @@ private int ValidateUrl(string text)
{
int number;
//Extract just the numbers from the URL, also remove query string
Uri url = new Uri(text);
Uri url = new UriBuilder(text).Uri;
string path = String.Format("{0}{1}{2}{3}", url.Scheme, Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);
bool success = Int32.TryParse(Regex.Match(path, @"\d+").Value, out number);
if (success)
Expand Down

0 comments on commit 2f683ab

Please sign in to comment.