Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

Commit

Permalink
上传了WPF版本
Browse files Browse the repository at this point in the history
  • Loading branch information
DCjanus committed Mar 9, 2017
1 parent 8aeb76c commit fa044db
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 0 deletions.
9 changes: 9 additions & 0 deletions WpfApp/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="DC_sub_downloader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DC_sub_downloader"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
16 changes: 16 additions & 0 deletions WpfApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace DC_sub_downloader
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
}
}
26 changes: 26 additions & 0 deletions WpfApp/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Window x:Class="DC_sub_downloader.MainWindow"
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:DC_sub_downloader"
mc:Ignorable="d"
Background="Azure"
Title="DC字幕下载器"
Height="600"
Width="800"
DragEnter="Window_DragEnter"
DragOver="Window_DragOver"
Drop="Window_Drop">
<Grid
Background="Azure"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock
ForceCursor="False"
Name="messageBlock"
FontSize="20px"
Foreground="#d3d3d3"
Text="拖入视频开始下载,支持多文件"/>
</Grid>
</Window>
106 changes: 106 additions & 0 deletions WpfApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Linq;

using System.Windows;
using System.IO;

namespace DC_sub_downloader
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.AllowDrop = true;
}

private async void Window_Drop(object sender, DragEventArgs e)
{
var allFilePaths = (String[])e.Data.GetData(DataFormats.FileDrop);
var downloadInfoList = allFilePaths.Select(x => new DownloadInfo() { fileName = System.IO.Path.GetFileName(x), status = DownloadStatus.Pre, subNumber = 0 }).ToArray();

this.messageBlock.Text = String.Join("\n", downloadInfoList.Select(x => x.message));

for (var i = 0; i < allFilePaths.Length; i++)
{
var filePath = allFilePaths[i];
if (File.Exists(filePath))
{
var theDownloader = new SubDownloader(filePath);

downloadInfoList[i].status = DownloadStatus.Ing;
this.messageBlock.Text = String.Join("\n", downloadInfoList.Select(x => x.message));

var subNumber = await theDownloader.downLoadAllAsync();

downloadInfoList[i].subNumber = subNumber;
downloadInfoList[i].status = DownloadStatus.Done;
}
else
{
downloadInfoList[i].status = DownloadStatus.NotFile;
}
this.messageBlock.Text = String.Join("\n", downloadInfoList.Select(x => x.message));
}
}

private void Window_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Link;
}
else
{
e.Effects = DragDropEffects.None;
this.messageBlock.Text = "拖入的不是文件";
}
}

private void Window_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Link;
}
else
{
e.Effects = DragDropEffects.None;
}
}
}

enum DownloadStatus
{
Pre, Ing, Done, NotFile
}

class DownloadInfo
{
public String fileName;
public DownloadStatus status;
public int subNumber;
public String message
{
get
{
if (status == DownloadStatus.Pre)
{
return String.Format("即将开始下载:{0}", fileName);
}
else if (status == DownloadStatus.Ing)
{
return String.Format("正在下载字幕:{0}", fileName);
}
else if (status == DownloadStatus.NotFile)
{
return String.Format("不是文件或者文件无法访问:{0}", fileName);
}
else
{
return String.Format("{1,2}个字幕完成:{0}", fileName, subNumber);
}
}
}
}
}
5 changes: 5 additions & 0 deletions WpfApp/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
用C#写的WPF程序,最终的打包成了单文件发布

icon.ico是软件图标,不过我想你们也不会在意的233333

[项目依赖](packages.config)
124 changes: 124 additions & 0 deletions WpfApp/SubDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Linq;
using System.Text;

namespace DC_sub_downloader
{
public class SubDownloader
{
private String filePath;
public SubDownloader(String filePath)
{
this.filePath = filePath;
}

public async Task<int> downLoadAllAsync()
{
var dirName = Path.GetDirectoryName(this.filePath);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.filePath);
var client = new WebClient();

var subInfoList = await this.getSubInfoListAsync();
subInfoList = subInfoList.Where(s => s.rate > 0).ToArray();

for (var i = 0; i < subInfoList.Length; i++)
{
var theSubInfo = subInfoList[i];
var subFileName = String.Format("{0}_{1}{2}", fileNameWithoutExtension, i, Path.GetExtension(theSubInfo.surl));
var subFilePath = Path.Combine(dirName, subFileName);
while (true)
{
try
{
await client.DownloadFileTaskAsync(theSubInfo.surl, subFilePath);
break;
}
catch (WebException)
{
continue;
}
}
}
return subInfoList.Length;
}

public String Cid
{
get
{
var stream = new FileStream(this.filePath, FileMode.Open, FileAccess.Read);
var reader = new BinaryReader(stream);
var fileSize = (new FileInfo(filePath).Length);
var SHA1 = new SHA1CryptoServiceProvider();
var buffer = new byte[0xf000];
if (fileSize < 0xf000)
{
reader.Read(buffer, 0, (int)fileSize);
buffer = SHA1.ComputeHash(buffer, 0, (int)fileSize);
}
else
{
reader.Read(buffer, 0, 0x5000);
stream.Seek(fileSize / 3, SeekOrigin.Begin);
reader.Read(buffer, 0x5000, 0x5000);
stream.Seek(fileSize - 0x5000, SeekOrigin.Begin);
reader.Read(buffer, 0xa000, 0x5000);

buffer = SHA1.ComputeHash(buffer, 0, 0xf000);
}
var result = "";
foreach (var i in buffer)
{
result += String.Format("{0:X2}", i);
}
return result;
}
}

public async Task<String> getRawSubInfosAsync()
{
var client = new WebClient();
var url = String.Format("http://sub.xmp.sandai.net:8000/subxl/{0}.json", this.Cid);
while (true)
{
try
{
var data = await client.DownloadDataTaskAsync(url);
return Encoding.UTF8.GetString(data);
}
catch (WebException)
{
continue;
}
}
}

public async Task<SubInfo[]> getSubInfoListAsync()
{
var result = JsonConvert.DeserializeObject<SubList>(await this.getRawSubInfosAsync()).sublist;
result = result.Where(s => !string.IsNullOrEmpty(s.surl)).ToArray();
return result;
}


}
public class SubInfo
{
public String scid { get; set; }
public String sname { get; set; }
public String language { get; set; }
public long rate { get; set; }
public String surl { get; set; }
public long svote { get; set; }
public long roffset { get; set; }
}
public class SubList
{
public SubInfo[] sublist;
}
}
3 changes: 3 additions & 0 deletions WpfApp/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
Binary file added WpfApp/icon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions WpfApp/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
</packages>

0 comments on commit fa044db

Please sign in to comment.