コード例 #1
0
        private async void btnGetInfo_Click(object sender, RoutedEventArgs e)
        {
            string id = ValidateUrl(textUrl.Text);

            if (id != "")
            {
                btnGetInfo.IsEnabled = false;
                downloadId           = id;
                if (id.All(Char.IsDigit))
                {
                    downloadType = DownloadType.Video;
                }
                else
                {
                    downloadType = DownloadType.Clip;
                }

                try
                {
                    if (downloadType == DownloadType.Video)
                    {
                        Task <JObject> taskInfo = TwitchHelper.GetVideoInfo(Int32.Parse(downloadId));
                        await Task.WhenAll(taskInfo);

                        videoData = taskInfo.Result;
                        string             thumbUrl  = taskInfo.Result["preview"]["medium"].ToString();
                        Task <BitmapImage> taskThumb = InfoHelper.GetThumb(thumbUrl);

                        try
                        {
                            await taskThumb;
                        }
                        catch
                        {
                            AppendLog("ERROR: Unable to find thumbnail");
                        }
                        if (!taskThumb.IsFaulted)
                        {
                            imgThumbnail.Source = taskThumb.Result;
                        }
                        textTitle.Text     = taskInfo.Result["title"].ToString();
                        textStreamer.Text  = taskInfo.Result["channel"]["display_name"].ToString();
                        textCreatedAt.Text = taskInfo.Result["created_at"].ToString();
                        currentVideoTime   = taskInfo.Result["created_at"].ToObject <DateTime>().ToLocalTime();
                        streamerId         = taskInfo.Result["channel"]["_id"].ToObject <int>();
                        SetEnabled(true, false);
                    }
                    else if (downloadType == DownloadType.Clip)
                    {
                        string         clipId   = downloadId;
                        Task <JObject> taskInfo = TwitchHelper.GetClipInfo(clipId);
                        await Task.WhenAll(taskInfo);

                        JToken clipData = taskInfo.Result;
                        videoData = taskInfo.Result;
                        string             thumbUrl  = clipData["thumbnails"]["medium"].ToString();
                        Task <BitmapImage> taskThumb = InfoHelper.GetThumb(thumbUrl);
                        await Task.WhenAll(taskThumb);

                        imgThumbnail.Source = taskThumb.Result;
                        textStreamer.Text   = clipData["broadcaster"]["display_name"].ToString();
                        textCreatedAt.Text  = clipData["created_at"].ToString();
                        currentVideoTime    = clipData["created_at"].ToObject <DateTime>().ToLocalTime();
                        textTitle.Text      = clipData["title"].ToString();
                        streamerId          = clipData["broadcaster"]["id"].ToObject <int>();
                        SetEnabled(true, false);
                        SetEnabled(false, true);
                    }

                    btnGetInfo.IsEnabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to get Clip/Video information. Please double check your link and try again", "Unable to get info", MessageBoxButton.OK, MessageBoxImage.Error);
                    AppendLog("ERROR: " + ex.Message);
                    btnGetInfo.IsEnabled = true;
                }
            }
            else
            {
                MessageBox.Show("Please double check the VOD/Clip link", "Unable to parse input", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }