Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
truebigsand committed Apr 10, 2022
1 parent 3ffec80 commit 70f67e8
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 32 deletions.
1 change: 1 addition & 0 deletions CustomTools/CustomTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.115.5" />
</ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions CustomTools/Kouyu100AutoFinishInnerWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Window x:Class="CustomTools.Kouyu100AutoFinishInnerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomTools"
mc:Ignorable="d"
WindowStartupLocation="CenterOwner"
Title="Kouyu100AutoFinishInnerWindow" Height="450" Width="800">
<Grid>
<TextBlock x:Name="HomeworkTitleTextBlock" Margin="10,10,0,0" TextWrapping="Wrap" Text="标题: " HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBlock x:Name="ExamIdTextBlock" HorizontalAlignment="Left" Margin="10,30,0,0" TextWrapping="Wrap" Text="ExamId: " VerticalAlignment="Top"/>
<TextBlock x:Name="HomeworkIdTextBlock" HorizontalAlignment="Left" Margin="10,50,0,0" TextWrapping="Wrap" Text="HwId: " VerticalAlignment="Top"/>
<Button x:Name="StartButton" Content="一键开摆" HorizontalAlignment="Center" Margin="0,27,0,0" VerticalAlignment="Top" Click="StartButton_Click"/>
<TextBlock HorizontalAlignment="Left" Margin="269,10,0,0" TextWrapping="Wrap" Text="摆烂进度: " VerticalAlignment="Top"/>
<ProgressBar x:Name="UploadProgressBar" HorizontalAlignment="Left" Height="12" Margin="328,10,0,0" VerticalAlignment="Top" Width="200"/>
</Grid>
</Window>
206 changes: 206 additions & 0 deletions CustomTools/Kouyu100AutoFinishInnerWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CustomTools
{
/// <summary>
/// Kouyu100AutoFinishInnerWindow.xaml 的交互逻辑
/// </summary>
public partial class Kouyu100AutoFinishInnerWindow : Window
{
private readonly Homework currentHomework;
string cookieString;
int ScoreId;
public Kouyu100AutoFinishInnerWindow(Homework currentHomework, string cookieString)
{
InitializeComponent();
this.currentHomework = currentHomework;
this.cookieString = cookieString;
HomeworkTitleTextBlock.Text = $"标题: {currentHomework.Name}";
ExamIdTextBlock.Text = $"ExamId: {currentHomework.ExamId}";
HomeworkIdTextBlock.Text = $"HwId: {currentHomework.HomeworkId}";
#pragma warning disable CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
#pragma warning disable CS8602 // 解引用可能出现空引用。
#pragma warning disable CS8604 // 引用类型参数可能为 null。
ScoreId = (int)((
(JObject)JsonConvert.DeserializeObject(
Kouyu100HttpGet($"http://028.kouyu100.com/njjlzxhx/getTimeAndAnswer.action?examId={currentHomework.ExamId}&homeWork.id={currentHomework.HomeworkId}")
)
)["listenExamScore"]["id"]);
#pragma warning restore CS8604 // 引用类型参数可能为 null。
#pragma warning restore CS8602 // 解引用可能出现空引用。
#pragma warning restore CS8600 // 将 null 字面量或可能为 null 的值转换为非 null 类型。
}
public string Kouyu100HttpGet(string Url)
{
//Create
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.AllowAutoRedirect = false;
request.Headers.Add("Cookie", cookieString);
//Receive
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string resultString = streamReader.ReadToEnd();
//Clean up
streamReader.Close();
responseStream.Close();
return resultString;
}
public string Kouyu100HttpPost(string Url, string postDataStr)
{
//Create
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("Cookie", cookieString);
Encoding encoding = Encoding.UTF8;
byte[] postData = encoding.GetBytes(postDataStr);
request.ContentLength = postData.Length;
//Send
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);
//Receive
HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encoding);
string resultString = streamReader.ReadToEnd();
//Clean up
requestStream.Close();
streamReader.Close();
responseStream.Close();
return resultString;
}
// 修改自https://www.cnblogs.com/hofmann/p/11347007.html
static public string DictionaryToQueryString(IDictionary<string, string> parameters)
{
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();

StringBuilder query = new StringBuilder();
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = dem.Current.Value;
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
query.Append(key).Append('=').Append(value).Append('&');
}
}
string content = query.ToString().Substring(0, query.Length - 1);

return content;
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
Task.Run(() =>
{
/* Python Code:
* def upload_answer(group_id, choose_id, answer, exam_id, homework_id, score_id):
data = {
'chooseAnswer': f'{group_id}@@@{choose_id}@@@{answer}@@@1',
'listenExamAnswer.quesType': 1,
'listenExamAnswer.orgId': 10206,
'listenExamAnswer.orgType': 1,
'listenExamAnswer.managerId': 255505,
'listenExamAnswer.examId': exam_id,
'listenExamAnswer.homeWorkId': homework_id,
'listenExamScore.id': score_id
}
return req.post('https://028.kouyu100.com/njjlzxhx/saveSingleExamAnswer.action',data=data,cookies=cookies)
response = req.get(f'http://028.kouyu100.com/njjlzxhx/getListenGroupsByExamId.action?examId={exam_id}',cookies=cookies)
group_list = response.json()['groupList']
answer_list = []
for i in group_list:
for j in i['chooseList']:
answer_list.append({
'group_id': i['id'],
'choose_id': j['id'],
'answer': j['answers']
})
for i in answer_list:
group_id, choose_id, answer = i['group_id'], i['choose_id'], i['answer']
print(upload_answer(group_id, choose_id, answer, exam_id, homework_id, score_id).text)
# print(answer, end='')
req.get(f'https://028.kouyu100.com/njjlzxhx/endExamAnswer.action?examId={exam_id}&homeWork.id={homework_id}&listenExamScore.id={score_id}')
if req.get(f'http://028.kouyu100.com/njjlzxhx/findStudentExamInfo.action?examId={exam_id}&hwId={homework_id}').text != '{"scoreList":[]}':
print('ok!')
else:
print('failed!')
*/
// 构造答案列表
string groupListResultString = Kouyu100HttpGet($"http://028.kouyu100.com/njjlzxhx/getListenGroupsByExamId.action?examId={currentHomework.ExamId}");
JArray groupList = (JArray)((JObject)JsonConvert.DeserializeObject(groupListResultString))["groupList"];
List<Tuple<int, int, string>> answerList = new List<Tuple<int, int, string>>();
foreach(JObject group in groupList)
{
foreach (JObject choose in (JArray)group["chooseList"])
{
answerList.Add(new Tuple<int, int, string>((int)group["id"], (int)choose["id"], (string)choose["answers"]));
}
}
int count = 0;
double total = answerList.ToArray().Length;
// 遍历答案列表上传答案
foreach (Tuple<int, int, string> tuple in answerList)
{
int groupId = tuple.Item1;
int chooseId = tuple.Item2;
string answer = tuple.Item3;
Dictionary<string, string> data = new Dictionary<string, string>()
{
{ "chooseAnswer", $"{groupId}@@@{chooseId}@@@{answer}@@@1" },
{ "listenExamAnswer.quesType", "1" },
{ "listenExamAnswer.orgId", "10206" },
{ "listenExamAnswer.orgType", "1" },
{ "listenExamAnswer.managerId", "255505" },
{ "listenExamAnswer.examId", currentHomework.ExamId.ToString() },
{ "listenExamAnswer.homeWorkId", currentHomework.HomeworkId.ToString() },
{ "listenExamScore.id", ScoreId.ToString() }
};
// 上传单个答案并检查
if (Kouyu100HttpPost("https://028.kouyu100.com/njjlzxhx/saveSingleExamAnswer.action", DictionaryToQueryString(data)) != "1.0")
{
throw new WebException("上传单个答案API调用错误! ");
}
count++;
UploadProgressBar.Dispatcher.BeginInvoke(new Action(delegate
{
UploadProgressBar.Value = count / total * 100;
}));
}
// 结束上传
_ = Kouyu100HttpGet($"https://028.kouyu100.com/njjlzxhx/endExamAnswer.action?examId={currentHomework.ExamId}&homeWork.id={currentHomework.HomeworkId}&listenExamScore.id={ScoreId}");
// 检查结果
string result = Kouyu100HttpGet($"http://028.kouyu100.com/njjlzxhx/findStudentExamInfo.action?examId={currentHomework.ExamId}&hwId={currentHomework.HomeworkId}");
if (result!= "{\"scoreList\":[]}")
{
UploadProgressBar.Dispatcher.Invoke(delegate
{
UploadProgressBar.Value = 100;
});
MessageBox.Show("成功! ");
}
else
{
MessageBox.Show("失败! ");
UploadProgressBar.Dispatcher.Invoke(delegate
{
UploadProgressBar.Value = 0;
});
}
});
}
}
}
45 changes: 45 additions & 0 deletions CustomTools/Kouyu100AutoFinishWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Window x:Class="CustomTools.Kouyu100AutoFinishWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomTools"
mc:Ignorable="d"
WindowStartupLocation="CenterOwner"
Title="Kouyu100" Height="450" Width="800">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="10,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="authToken(从网页Cookies中取得): "/>
<TextBox x:Name="AuthTokenTextBox" Text="njjlzxhx.56928bf4-3837-4a07-8502-02e5dc2cd565_0920" HorizontalAlignment="Left" Margin="208,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="440"/>
<Button x:Name="GetHomeworkListButton" Content="获取作业列表" Margin="653,10,10,0" VerticalAlignment="Top" Click="GetHomeworkListButton_Click" Background="#FF2196F3"/>
<ListBox x:Name="HomeworkListBox" Margin="10,47,10,10" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Status}" Foreground="{DynamicResource MaterialDesignLightForeground}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="#FFA7FFA7"></Setter>
<Style.Triggers>
<Trigger Property="Text" Value="未完成">
<Setter Property="Background" Value="#FFFF2649"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="HomeworkListBoxItem_MouseDoubleClick"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</Window>
Loading

0 comments on commit 70f67e8

Please sign in to comment.