Skip to content

Commit

Permalink
update: v0.2.5.0
Browse files Browse the repository at this point in the history
changes:
	1. fixed login issue caused by bilibili oauth rule change
	2. bumped ui framework to 2.1.1
	3. emojis pics will be cached to reduce network usage now
  • Loading branch information
AlaricGilbert committed Jul 10, 2020
1 parent d6de9b3 commit 944933f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 71 deletions.
10 changes: 8 additions & 2 deletions BiliCommenter/BiliCommenter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<Version>0.2.4.0</Version>
<Version>0.2.5.0</Version>
<ApplicationIcon>logo.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<None Remove="avatar\ic_22.png" />
<None Remove="avatar\ic_22_hide.png" />
<None Remove="avatar\ic_33.png" />
<None Remove="avatar\ic_33_hide.png" />
<None Remove="logo.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.0.0-alpha0490" />
<Resource Include="logo.ico" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>

Expand Down
22 changes: 11 additions & 11 deletions BiliCommenter/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BiliCommenter"
mc:Ignorable="d"
Title="Bilibili Commenter" Height="600" Width="1000">
Title="Bilibili Commenter" Height="600" Width="1000" ResizeMode="NoResize">
<mahCtrls:MetroWindow.Resources>
</mahCtrls:MetroWindow.Resources>
<mahCtrls:MetroWindow.RightWindowCommands>
Expand Down Expand Up @@ -81,8 +81,8 @@
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Button Content="Add/Update" Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}" Grid.Row="1" Click="AddOrUpdate"/>
<Button Content="Remove" Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}" Grid.Row="3" Click="Remove"/>
<Button Content="Add/Update" Style="{DynamicResource MahApps.Styles.Button}" Grid.Row="1" Click="AddOrUpdate"/>
<Button Content="Remove" Style="{DynamicResource MahApps.Styles.Button}" Grid.Row="3" Click="Remove"/>
</Grid>
</Grid>
<Grid Grid.Row="2">
Expand All @@ -98,7 +98,7 @@
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="Emoji" Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}" Name="EmojiButton" Click="EmojiButton_Click"/>
<Button Content="Emoji" Style="{DynamicResource MahApps.Styles.Button}" Name="EmojiButton" Click="EmojiButton_Click"/>
<Label Grid.Column="1" Name="MessageTBStatus" HorizontalAlignment="Right"/>
</Grid>
</Grid>
Expand Down Expand Up @@ -151,19 +151,19 @@
mahCtrls:TextBoxHelper.IsWaitingForData="True"
mahCtrls:TextBoxHelper.UseFloatingWatermark="True"
mahCtrls:TextBoxHelper.Watermark="Password"
Style="{StaticResource MahApps.Styles.PasswordBox.ButtonRevealed}"
Style="{StaticResource MahApps.Styles.PasswordBox.Button.Revealed}"
KeyDown="PasswordBox_KeyDown"/>

<StackPanel Orientation="Horizontal" Margin="10 10 10 10">
<Button
Content="Login"
Width="180"
Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}"
Style="{DynamicResource MahApps.Styles.Button}"
Click="LoginButton_Click"/>
<Button
Content="Cancel"
Width="180"
Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}" Margin="20,0,0,0"
Style="{DynamicResource MahApps.Styles.Button}" Margin="20,0,0,0"
Click="ChangeLoginFlyout"/>
</StackPanel>
<Label Margin="10 10 10 10" Name="ResultLabel" Foreground="Red" ></Label>
Expand All @@ -181,9 +181,9 @@
</mahCtrls:Flyout>
<mahCtrls:Flyout Name="SettingsFlyout" Header="Settings" Position="Right" Width="200" Theme="Accent">
<StackPanel Orientation="Vertical">
<mahCtrls:ToggleSwitch Margin="30 10 10 10" Name="IsSaveAccessKey" OnLabel="保存登陆状态" OffLabel="不保存登陆状态" Style="{StaticResource MahApps.Styles.ToggleSwitch.Win10}"/>
<mahCtrls:ToggleSwitch Margin="30 10 10 10" Name="IsInheritTasks" OnLabel="继承任务状态" OffLabel="不继承任务状态" Style="{StaticResource MahApps.Styles.ToggleSwitch.Win10}"/>
<Button Margin="10 10 10 10" Style="{DynamicResource MahApps.Styles.Button.AccentedSquare}" Content="SAVE" Click="UpdateSettings"/>
<mahCtrls:ToggleSwitch Margin="30 10 10 10" Name="IsSaveAccessKey" OnContent="保存登陆状态" OffContent="不保存登陆状态" Style="{StaticResource MahApps.Styles.ToggleSwitch}"/>
<mahCtrls:ToggleSwitch Margin="30 10 10 10" Name="IsInheritTasks" OnContent="继承任务状态" OffContent="不继承任务状态" Style="{StaticResource MahApps.Styles.ToggleSwitch}"/>
<Button Margin="10 10 10 10" Style="{DynamicResource MahApps.Styles.Button}" Content="SAVE" Click="UpdateSettings"/>
</StackPanel>
</mahCtrls:Flyout>
<mahCtrls:Flyout Name="EmojiFlyout"
Expand All @@ -195,7 +195,7 @@
CloseButtonVisibility="Collapsed"
Header="Top"
TitleVisibility="Collapsed">
<mahCtrls:MetroTabControl Name="EmojiTabControl" TabStripPlacement="Bottom" mahCtrls:ControlsHelper.HeaderFontSize="20">
<mahCtrls:MetroTabControl Name="EmojiTabControl" TabStripPlacement="Bottom" >
</mahCtrls:MetroTabControl>
</mahCtrls:Flyout>
</mahCtrls:FlyoutsControl>
Expand Down
71 changes: 48 additions & 23 deletions BiliCommenter/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace BiliCommenter
Expand Down Expand Up @@ -87,6 +88,10 @@ public void Initialize()
if (Settings.Default.IsInheritTasks)
ReadTasks();

// Init caches
if (!Directory.Exists("cache"))
Directory.CreateDirectory("cache");

#region Read log-in informations
Thread freshThread = new Thread(async () =>
{
Expand Down Expand Up @@ -121,20 +126,21 @@ public void Initialize()
{
// Get the list of the emojis.
var allEmojis = await Common.GetEmojisAsync();
var checkedEmojis = await Common.CheckCacheAsync(allEmojis);

for (int i = 0; i < allEmojis.Data.Count; i++)
for (int i = 0; i < checkedEmojis.Count; i++)
{
var emojiPack = allEmojis.Data[i];
var emojiPack = checkedEmojis[i];
// These part of code is a piece of s**t, but works :).
this.Invoke(() =>
{
var item = new TabItem();
item.Header = emojiPack.Pname;
item.Header = emojiPack.PackName;
StackPanel panel = new StackPanel { Orientation = Orientation.Vertical, HorizontalAlignment = HorizontalAlignment.Center };
StackPanel stack = null;
for (int j = 0; j < emojiPack.Emojis.Count; j++)
for (int j = 0; j < emojiPack.CheckedEmojis.Count; j++)
{
var emoji = emojiPack.Emojis[j];
var emoji = emojiPack.CheckedEmojis[j];
if (j % 9 == 0)
{
stack = new StackPanel { Orientation = Orientation.Horizontal };
Expand All @@ -146,9 +152,9 @@ public void Initialize()
Height = 75,
Content = new Image
{
Width = 70,
Height = 70,
Source = CreateBI(emoji.Url),
Width = 64,
Height = 64,
Source = CreateBI(emoji.ImagePath),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
},
Expand Down Expand Up @@ -193,8 +199,8 @@ private void ChangeLogStatusFlyouts(object sender, RoutedEventArgs e)
}
private void ChangeSettingFlyout(object sender, RoutedEventArgs e)
{
IsSaveAccessKey.IsChecked = Settings.Default.IsSaveAccessKey;
IsInheritTasks.IsChecked = Settings.Default.IsInheritTasks;
IsSaveAccessKey.IsOn = Settings.Default.IsSaveAccessKey;
IsInheritTasks.IsOn = Settings.Default.IsInheritTasks;
SettingsFlyout.IsOpen = !SettingsFlyout.IsOpen;
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
Expand All @@ -220,7 +226,7 @@ private void LoginButton_Click(object sender, RoutedEventArgs e)
Thread loginThread = new Thread(async () =>
{
// we should handle the exceptions that made it fail to login in the future version.
await Auth.LoginV3(username, password);
await Auth.Login(username, password);
if (Settings.Default.IsSaveAccessKey)
{
Settings.Default.AccessKey = Account.AccessKey;
Expand Down Expand Up @@ -306,24 +312,43 @@ private void MessageTextBox_TextChanged(object sender, TextChangedEventArgs e)
}
private void EmojiButton_Click(object sender, RoutedEventArgs e)
{
EmojiFlyout.IsOpen = !EmojiFlyout.IsOpen;
if (EmojiFlyout.IsOpen)
if (!EmojiFlyout.IsOpen)
{
int delta = 40;
for (int i = 0; i < 400 / delta; i++)
Height += delta;
DoubleAnimation da = new DoubleAnimation(600, 1000, TimeSpan.FromSeconds(0.1));
BeginAnimation(HeightProperty, da);
Thread th = new Thread(() =>
{
Thread.Sleep(100);

this.Invoke(() =>
{
EmojiFlyout.IsOpen = !EmojiFlyout.IsOpen;
});
});
th.Start();
}
else
{
Thread th = new Thread(() => {
Thread.Sleep(500);
int delta = 40;
for (int i = 0; i < 400 / delta; i++)
this.Invoke(() => Height -= delta);
EmojiFlyout.IsOpen = !EmojiFlyout.IsOpen;
Thread th = new Thread(() =>
{
Thread.Sleep(100);

this.Invoke(() =>
{
DoubleAnimation da = new DoubleAnimation(1000, 600, new Duration(TimeSpan.FromSeconds(0.2)));
CircleEase ease = new CircleEase();
ease.EasingMode = EasingMode.EaseIn;
da.EasingFunction = ease;

BeginAnimation(HeightProperty, da);
});
});
th.Start();
}
}


private void AddOrUpdate(object sender, RoutedEventArgs e)
{
if(TaskPair.ContainsKey(BangumiListBox.SelectedItem as string))
Expand Down Expand Up @@ -410,8 +435,8 @@ private void ReadTasks()
}
private void UpdateSettings(object sender, RoutedEventArgs e)
{
Settings.Default.IsSaveAccessKey = IsSaveAccessKey.IsChecked == true;
Settings.Default.IsInheritTasks = IsInheritTasks.IsChecked == true;
Settings.Default.IsSaveAccessKey = IsSaveAccessKey.IsOn == true;
Settings.Default.IsInheritTasks = IsInheritTasks.IsOn == true;
Settings.Default.Save();
}
}
Expand Down
2 changes: 1 addition & 1 deletion BiliCommenter/WelcomeWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
mc:Ignorable="d"
Title="WelcomeWindow" Height="512" Width="512" WindowStyle="None" AllowsTransparency="True" Background="#00FFFFFF">
<Grid>
<Image Source="logo.ico"/>
<Image Source="pack://application:,,,/logo.ico"/>
</Grid>
</Window>
39 changes: 11 additions & 28 deletions BiliCore/API/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -52,46 +54,27 @@ public static async Task LoginFromAccessKey(string accesskey)
await Account.FreshStatusAsync();
if (Account.AccessKey != "") await FreshSSO();
}
public static async Task LoginV3(string username, string password, string captcha = "")
public static async Task Login(string username, string password)
{
string url = "https://passport.bilibili.com/api/v3/oauth2/login";
string data = $"appkey={Common.AppKey}&build={Common.Build}&mobi_app=android&password={Uri.EscapeDataString(await EncryptPassword(password))}&platform=android&ts={Common.TimeSpan}&username={Uri.EscapeDataString(username)}";
if (data != "")
{
data += "&captcha=" + captcha;
}
var pwd = Uri.EscapeDataString(await EncryptPassword(password));

string data = $"username={Uri.EscapeDataString(username)}&password={pwd}&gee_type=10&appkey={Common.AppKey}&mobi_app=android&platform=android&ts={Common.TimeSpan}";
data += "&sign=" + Common.GetSign(data);
using (HttpClient hc = new HttpClient())
{
hc.DefaultRequestHeaders.Referrer = new Uri("https://www.bilibili.com");
var response = await hc.PostAsync(url, new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"));
hc.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 BiliDroid/5.44.2 ([email protected])");
hc.DefaultRequestHeaders.Add("referer", "https://www.bilibili.com/");
var response = await hc.PostAsync(url, new StringContent(data,Encoding.UTF8, "application/x-www-form-urlencoded"));

var result = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<AuthModelV3>(result);
Account.AccessKey = model.data.token_info.access_token;
Account.AuthResultCode = model.code;
await FreshSSO();
}
}
public static async Task LoginV2(string username, string password, string captcha = "")
{
string url = "https://passport.bilibili.com/api/oauth2/login";
string data = $"appkey={Common.AppKey}&build={Common.Build}&mobi_app=android&password={Uri.EscapeDataString(await EncryptPassword(password))}&platform=android&ts={Common.TimeSpan}&username={Uri.EscapeDataString(username)}";
if (data != "")
{
data += "&captcha=" + captcha;
}
data += "&sign=" + Common.GetSign(data);
using (HttpClient hc = new HttpClient())
{
hc.DefaultRequestHeaders.Referrer = new Uri("https://www.bilibili.com");
var response = await hc.PostAsync(url, new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"));
var result = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<AuthModelV2>(result);
Account.AccessKey = model.data.access_token;
Account.AuthResultCode = model.code;
await FreshSSO();
}
}

public static async Task FreshSSO()
{
var url = $"https://api.kaaass.net/biliapi/user/sso?access_key={Account.AccessKey}";
Expand Down
43 changes: 40 additions & 3 deletions BiliCore/API/Common.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using BiliCommenter.Models;
using BiliCore.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -16,9 +19,11 @@ public static long TimeSpan
get { return Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0)).TotalSeconds); }
}

public const string AppKey = "1d8b6e7d45233436";
public const string AppSecret = "560c52ccd288fed045859ed18bffd973";
public const string Build = "5290000";
public const string AppKey = "4409e2ce8ffd12b8";
public const string AppSecret = "59b43e04ad6965f34319062b478f83dd";
public const string Build = "5370000";



public static string GetSign(string url)
{
Expand All @@ -36,6 +41,7 @@ public static string GetSign(string url)
result = MD5.GetMd5String(stringBuilder.ToString()).ToLower();
return result;
}

public static async Task<EmojisModel> GetEmojisAsync()
{
using (var client = new HttpClient())
Expand All @@ -45,5 +51,36 @@ public static async Task<EmojisModel> GetEmojisAsync()
return JsonConvert.DeserializeObject<EmojisModel>(context);
}
}

public static async Task<List<CheckedEmojiPack>> CheckCacheAsync(EmojisModel emojis)
{
List<CheckedEmojiPack> list = new List<CheckedEmojiPack>();
for (int i = 0; i < emojis.Data.Count; i++)
{
var emojiPack = emojis.Data[i];
var checkedEmojiPack = new CheckedEmojiPack();

checkedEmojiPack.PackName = emojiPack.Pname;

for (int j = 0; j < emojiPack.Emojis.Count; j++)
{
var emoji = emojiPack.Emojis[j];
var checkedEmoji = new CheckedEmojiPack.CheckedEmoji();
var url = emoji.Url;
var fileName = url.Split('/').Last();
var filePath = Path.Combine("cache", fileName);
if (!File.Exists(filePath))
{
WebClient wc = new WebClient();
await wc.DownloadFileTaskAsync(new Uri(url), filePath);
}
checkedEmoji.Name = emoji.Name;
checkedEmoji.ImagePath = "pack://siteoforigin:,,,/cache/" + fileName;
checkedEmojiPack.CheckedEmojis.Add(checkedEmoji);
}
list.Add(checkedEmojiPack);
}
return list;
}
}
}
Loading

0 comments on commit 944933f

Please sign in to comment.