public TweetMediaViewModel(IMediaEntity model)
        {
            this.id   = model.Id.Value;
            this.type = model.VideoDetails == null ? TweetMediaType.Image : TweetMediaType.Video;

            if (this.type == TweetMediaType.Video)
            {
                // Video support has been elided in the view layer for these reasons:
                //
                // 1. Amazingly, WPF's MediaElement does not support HTTPS.
                //    See https://connect.microsoft.com/VisualStudio/feedback/details/934355/in-a-wpf-standalone-application-exe-when-the-source-of-a-mediaelement-is-set-to-a-https-uri-it-throws-a-nullreferenceexception.
                // 2. Changing the URI to HTTP does not help. The MediaElement fails to load the video, giving an obscure "Exception from HRESULT: 0xC00D11B1".
                //    Presumably this is because the HTTPS redirects to HTTP, since the videos play fine in Windows Media Player.
                // 3. I considered dropping in https://github.com/unosquare/ffmediaelement as an alternative to MediaElement.
                //    However, the licensing situation is unclear and there were too many bugs related to disposal/memory management.
                //
                // That said, I have included all the data you need here and left stub code in TweetView.xaml.cs. Feel free to drop in the above control to get videos working.

                this.uri         = new Uri(model.VideoDetails.Variants[0].URL);
                this.aspectRatio = model.VideoDetails.AspectRatio[0] / model.VideoDetails.AspectRatio[1];
            }
            else
            {
                this.uri = new Uri(model.MediaURLHttps);
                var size = model.Sizes["large"];
                this.aspectRatio = size.Width ?? 1d / size.Height ?? 1d;
            }
        }
示例#2
0
        private static Option <IMedia> GetVideo(IMediaEntity media, string thumbnailUrl)
        {
            IVideoInformationEntity videoInfo = media.VideoDetails;

            IVideoEntityVariant[] variants = videoInfo.Variants;

            IVideoEntityVariant bestVideo = variants.OrderByDescending(variant => variant.Bitrate)
                                            .FirstOrDefault();

            Dictionary <string, IMediaEntitySize> sizes = media.Sizes;
            IMediaEntitySize size = sizes.GetValueOrDefault("large")
                                    ?? sizes.GetValueOrDefault("medium")
                                    ?? sizes.GetValueOrDefault("small");

            if (bestVideo != null)
            {
                return(Option.Some <IMedia>(
                           new Video(
                               bestVideo.URL,
                               thumbnailUrl,
                               TimeSpan.FromMilliseconds(videoInfo.DurationInMilliseconds),
                               size?.Width,
                               size?.Height)));
            }

            return(Option.None <IMedia>());
        }
示例#3
0
        private string GetMediaUrl(IMediaEntity media)
        {
            switch (media.MediaType)
            {
            case "photo": return(media.MediaURLHttps);

            case "animated_gif": return(media.VideoDetails.Variants[0].URL);

            case "video": return(media.VideoDetails.Variants.OrderByDescending(x => x.Bitrate).First().URL);

            default: return(null);
            }
        }
示例#4
0
        public bool Equals(IMediaEntity other)
        {
            if (Id == null || Id != other.Id)
            {
                return false;
            }

            if (Indices == null || other.Indices == null)
            {
                return Indices == other.Indices;
            }

            return Indices.ContainsSameObjectsAs(other.Indices, true);
        }
示例#5
0
        public bool Equals(IMediaEntity other)
        {
            if (Id == null || other == null || Id != other.Id)
            {
                return(false);
            }

            if (Indices == null || other.Indices == null)
            {
                return(Indices == other.Indices);
            }

            return(Indices.ContainsSameObjectsAs(other.Indices, true));
        }
示例#6
0
        private static MessageAttachment ConvertToMessageAttachment(IMediaEntity media)
        {
            var messageAttachment      = new MessageAttachment {
            };
            MessageAttachmentType type = MessageAttachmentType.File;

            if (media.MediaType == "animated_gif")
            {
                type = MessageAttachmentType.AnimatedImage;
            }
            if (media.MediaType == "photo")
            {
                type = MessageAttachmentType.Image;
            }
            if (media.MediaType == "vedio" || media.MediaType == "video")
            {
                type = MessageAttachmentType.Video;
            }
            messageAttachment = new MessageAttachment
            {
                Type         = type,
                Url          = media.MediaURL,
                PreviewUrl   = media.MediaURL,
                MimeType     = new Uri(media.MediaURL).GetMimeType(),
                OriginalId   = media.IdStr,
                OriginalLink = media.URL
            };
            if (media.VideoDetails != null && media.VideoDetails.Variants.Any())
            {
                var video = media.VideoDetails.Variants.FirstOrDefault(t => t.Bitrate > 0);
                if (video == null)
                {
                    video = media.VideoDetails.Variants.FirstOrDefault();
                }

                messageAttachment = new MessageAttachment
                {
                    Type         = type,
                    Url          = video.URL,
                    PreviewUrl   = media.MediaURL,
                    MimeType     = video.ContentType,
                    OriginalId   = media.IdStr,
                    OriginalLink = media.URL
                };
            }

            NormarlizeAttachmentType(messageAttachment);

            return(messageAttachment);
        }
示例#7
0
 public static MediaDetails From(ITweet tweet, IMediaEntity media, int index)
 {
     return(new MediaDetails
     {
         Created = tweet.CreatedAt.DateTime,
         Username = tweet.CreatedBy.ScreenName,
         UserId = tweet.CreatedBy.Id,
         TweetId = tweet.Id,
         MediaId = media.Id.GetValueOrDefault(),
         MediaIndex = index,
         MediaUrl = media.MediaURLHttps,
         TweetUrl = tweet.Url,
         TweetText = tweet.FullText
     });
 }
示例#8
0
 private static string DownloadMedia(IMediaEntity media, string name)
 {
     using (var client = new WebClient())
     {
         var ext  = Path.GetExtension(media.MediaURLHttps);
         var date = DateTime.Now;
         var path = Path.Combine(AppContext.BaseDirectory, "images", name, $"IMG_{name}_{date:dd_MM_yyyy_HH-mm-ss}--{RandomString(5)}{ext}");
         if (ext == ".jpg" ||
             ext == ".png" ||
             ext == ".bmp")
         {
             if (!Directory.Exists(Path.Combine(AppContext.BaseDirectory, "images", name)))
             {
                 Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "images", name));
             }
             client.DownloadFile(new Uri(media.MediaURLHttps), path);
             return(path);
         }
         return(null);
     }
 }
        private void ProcessTweet(ITweet tw)
        {
            Console.WriteLine("A tweet has been found");

            if (tw.RetweetedTweet != null)
            {
                tw = tw.RetweetedTweet;
                Console.WriteLine("Extracting main Tweet rather than RT");
            }

            Console.WriteLine("The tweet is " + tw.FullText);

            List <IMediaEntity> Medias = tw.Media;

            if (Medias != null && Medias.Count > 0)
            {
                Application.Current.Dispatcher.Invoke(delegate
                {
                    IMediaEntity m = Medias[random.Next(Medias.Count)];
                    if (m.MediaURL.Contains(".jpg"))
                    {
                        TextURL         = tw.Url;
                        CurrentImageUrl = m.MediaURL;
                        if (IsShowImage)
                        {
                            ImageFromTweet = new BitmapImage(new Uri(CurrentImageUrl));
                        }
                    }
                });

                if (!Mongo_isTweetSaved(tw.IdStr))
                {
                    Mongo_SaveNewTweet(tw);
                    RecordQueue.Enqueue(tw);
                    //GCS_SaveNewTweetImage(tw);
                }
            }
        }
示例#10
0
 public bool Equals(IMediaEntity other)
 {
     return Id != null && Id == other.Id;
 }
示例#11
0
 public bool Equals(IMediaEntity other)
 {
     return(Id != null && Id == other.Id);
 }