Esempio n. 1
0
        private async void Open_Click(object sender, RoutedEventArgs e)
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();

            picker.FileTypeFilter.Add("*");
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                string hex;
                double length;
                this.Collection.Clear();

                Stream stream = await file.OpenStreamForReadAsync();

                byte[] result = ComputeMovieHash(stream);
                hex = ToHexadecimal(result);
                var properties = await file.GetBasicPropertiesAsync();

                length = properties.Size;

                await Task.Run(() => this.messenger.OSLogIn());

                SearchSubtitlesResponse ssre = null;
                SubInfo subtitle             = null;
                await Task.Run(() => this.messenger.SearchOS(hex, length, "all", ref ssre));

                foreach (SubInfo info in ssre.data)
                {
                    if (info.LanguageName == "English")
                    {
                        subtitle = info;
                    }
                }

                byte[] subtitleStream = null;
                if (subtitle != null)
                {
                    await Task.Run(() => this.messenger.DownloadSubtitle(int.Parse(subtitle.IDSubtitleFile), ref subtitleStream));

                    StorageFolder sf = await file.GetParentAsync();

                    string      subtitlename = file.Name.Substring(0, file.Name.LastIndexOf('.'));
                    StorageFile sfile        = await sf.CreateFileAsync(subtitlename + ".srt");

                    await FileIO.WriteBytesAsync(sfile, subtitleStream);
                }
                else
                {
                    Debug.WriteLine("Subtitle not found");
                }
            }
        }
Esempio n. 2
0
        private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
        {
            string path = ((MenuFlyoutItem)sender).Tag.ToString();

            Debug.WriteLine(((MenuFlyoutItem)sender).Tag);
            StorageFile file = await StorageFile.GetFileFromPathAsync(path);

            if (file != null)
            {
                string hex;
                double length;

                Stream stream = await file.OpenStreamForReadAsync();

                byte[] result = ComputeMovieHash(stream);
                hex = ToHexadecimal(result);
                var properties = await file.GetBasicPropertiesAsync();

                length = properties.Size;

                await Task.Run(() => this.messenger.OSLogIn());

                SearchSubtitlesResponse ssre = null;
                await Task.Run(() => this.messenger.SearchOS(hex, length, "all", ref ssre));

                //List<string> subtitles = new List<string>();
                List <SubInfo> subtitles = new List <SubInfo>();
                List <string>  subInfo   = new List <string>();
                foreach (SubInfo info in ssre.data)
                {
                    subtitles.Add(info);
                }
                SubtitleComparator c = new SubtitleComparator();
                subtitles.Sort(c);
                SubtitleList.Items.Clear();
                foreach (var x in subtitles)
                {
                    SubtitleList.Items.Add(x);
                }
                SubtitleDialog.IsPrimaryButtonEnabled = false;
                ContentDialogResult DialogResult = await SubtitleDialog.ShowAsync();

                if (DialogResult == ContentDialogResult.Primary)
                {
                    SubInfo selectedSubtitle = (SubInfo)SubtitleList.SelectedItem;
                    byte[]  subtitleStream   = null;
                    if (selectedSubtitle != null)
                    {
                        await Task.Run(() => this.messenger.DownloadSubtitle(int.Parse(selectedSubtitle.IDSubtitleFile), ref subtitleStream));

                        StorageFolder sf = await file.GetParentAsync();

                        string      subtitlename = file.Name.Substring(0, file.Name.LastIndexOf('.'));
                        StorageFile sfile        = await sf.CreateFileAsync(subtitlename + ".srt", CreationCollisionOption.ReplaceExisting);

                        await FileIO.WriteBytesAsync(sfile, subtitleStream);
                    }
                    else
                    {
                        Debug.WriteLine("Subtitle not found");
                    }
                }
            }
        }