From fa044db630dc7b0996a52a18070271f9bd921b7f Mon Sep 17 00:00:00 2001 From: DCjanus <1102035688@qq.com> Date: Thu, 9 Mar 2017 19:25:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BA=86WPF=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WpfApp/App.xaml | 9 +++ WpfApp/App.xaml.cs | 16 +++++ WpfApp/MainWindow.xaml | 26 ++++++++ WpfApp/MainWindow.xaml.cs | 106 ++++++++++++++++++++++++++++++++ WpfApp/README.MD | 5 ++ WpfApp/SubDownloader.cs | 124 ++++++++++++++++++++++++++++++++++++++ WpfApp/app.config | 3 + WpfApp/icon.ico | Bin 0 -> 4286 bytes WpfApp/packages.config | 4 ++ 9 files changed, 293 insertions(+) create mode 100644 WpfApp/App.xaml create mode 100644 WpfApp/App.xaml.cs create mode 100644 WpfApp/MainWindow.xaml create mode 100644 WpfApp/MainWindow.xaml.cs create mode 100644 WpfApp/README.MD create mode 100644 WpfApp/SubDownloader.cs create mode 100644 WpfApp/app.config create mode 100644 WpfApp/icon.ico create mode 100644 WpfApp/packages.config diff --git a/WpfApp/App.xaml b/WpfApp/App.xaml new file mode 100644 index 0000000..e77334c --- /dev/null +++ b/WpfApp/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/WpfApp/App.xaml.cs b/WpfApp/App.xaml.cs new file mode 100644 index 0000000..edf808d --- /dev/null +++ b/WpfApp/App.xaml.cs @@ -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 +{ + /// + /// App.xaml 的交互逻辑 + /// + public partial class App : Application + { + } +} diff --git a/WpfApp/MainWindow.xaml b/WpfApp/MainWindow.xaml new file mode 100644 index 0000000..cd35291 --- /dev/null +++ b/WpfApp/MainWindow.xaml @@ -0,0 +1,26 @@ + + + + + diff --git a/WpfApp/MainWindow.xaml.cs b/WpfApp/MainWindow.xaml.cs new file mode 100644 index 0000000..9430173 --- /dev/null +++ b/WpfApp/MainWindow.xaml.cs @@ -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); + } + } + } + } +} diff --git a/WpfApp/README.MD b/WpfApp/README.MD new file mode 100644 index 0000000..480c446 --- /dev/null +++ b/WpfApp/README.MD @@ -0,0 +1,5 @@ +用C#写的WPF程序,最终的打包成了单文件发布 + +icon.ico是软件图标,不过我想你们也不会在意的233333 + +[项目依赖](packages.config) \ No newline at end of file diff --git a/WpfApp/SubDownloader.cs b/WpfApp/SubDownloader.cs new file mode 100644 index 0000000..ce0de62 --- /dev/null +++ b/WpfApp/SubDownloader.cs @@ -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 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 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 getSubInfoListAsync() + { + var result = JsonConvert.DeserializeObject(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; + } +} diff --git a/WpfApp/app.config b/WpfApp/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/WpfApp/app.config @@ -0,0 +1,3 @@ + + + diff --git a/WpfApp/icon.ico b/WpfApp/icon.ico new file mode 100644 index 0000000000000000000000000000000000000000..be6931357bfa56115fd7d3fb3f46e9be13ec133d GIT binary patch literal 4286 zcmc&&_fu497G-Oz#xd3y#sqXXs7R)p90UZB8Dvn9Q9;o`L_m-zh=52CB`GhKNQ>c1!LRZAbjl;i0ubLu_yQ#b_d1aNK!gt&z?te zUODpXE}`ObH)^|X;>zuN7@U~Gvv=T9t|LtVnOu(5@utsPVrmQb2oz({2w z;eX}jjf$o=q~?|2(6K~fM8oS~7~F~L>~#Q6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{gql|9}_o=J@_o+!>poXJ+x7u?e>1Ga@#D-u8>{{(>)I6JNnFqV^_c7Lxy2 zZ?cEumaRxR%`;z0Cd#Xu(cL$U$)|7eX6{G)f${#spYd+)k9hm;1K!U43GaXW2|xVw zvxF~lc>kB5(R{TRJAC}$=<1H7^h|u~yAP|Wy_PoTpZc$3-Rrf*9BPtpQz$tfW;_#F z(Tkk6xNz^Zaqb3Dc%c?Y==nYeL(toI2k$=oi0_I2*T4M}A3y#JAOHSueEi3M@JevN zoMy;JP8rXKCr=?I_dJf$18dql$l+eBSiR~u?zIH>3hKbCzM(PoS8)b8f8zYlaHEIr z+IIjt^aU+lV;HhG?VMcT?COSHtm|k*3}TMOBldVA;^LFwzQY^ackRLEuic@|Inv^@ zgOW2Gm3R^-($7G?Q3vY8654Ak*n52aYnYmuK}inGS-(o~z~?`a&jww6&Z7m-=Qc2; zN9h`v@SdUMS7tnSS=!kDc2B&gPtVk)e~1wNVaYib_l)T+`bs6eT?<;&UvMu%;ENeF z;qKeW{FMA13kwSv7#P5ud&rFONgH!|6rV|mVZ`Sq;g#a~SjC<)W?sg8Ru%pw!jv8= zd{n#^If}5db%5PwC+-zJ&b{WRx##@LIeQv>2Hv)98%#}2p;D<}Y)q~V4WTqKhKl)! zoJ`4y5znR@>B%}94X{o}pU){u7}76=cMCk5O`F+6e0F9o=H$zY+*peKw{wI8XVA&j aRob!Z*?$|>X-mB)aX>A8{Xf9}>-t}d>iP5l literal 0 HcmV?d00001 diff --git a/WpfApp/packages.config b/WpfApp/packages.config new file mode 100644 index 0000000..e1fae9c --- /dev/null +++ b/WpfApp/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file