Ejemplo n.º 1
0
        public static List<RedditImage> ParseFromJson(string jsonText, out string nextPath)
        {
            var ret = new List<RedditImage>();

            var jsonObject = JObject.Parse(jsonText);
            var jArr = jsonObject["data"]["children"] as JArray;

            try
            {
                nextPath = (string)jsonObject["data"]["after"];
            }
            catch (InvalidOperationException)
            {
                nextPath = "null";
            }

            var allRedditImages = Task.WhenAll(jArr.Select(async itemJTok =>
            {
                var dataJTok = itemJTok["data"];
                var tentativeThumbnail = (string)dataJTok["thumbnail"];

                if (!Uri.IsWellFormedUriString(tentativeThumbnail, UriKind.Absolute))
                {
                    return null;
                }

                var item = new RedditImage()
                {
                    Title = System.Net.WebUtility.HtmlDecode((string)dataJTok["title"]),
                    Permalink = "http://reddit.com" + (string)dataJTok["permalink"],
                    OriginalUrl = (string)dataJTok["url"],
                    NSFW = (bool)dataJTok["over_18"],
                };

                // Parser task here to determine the correct image and album
                Task.Run(async () =>
                {
                    var extractRet = await ImgUrlExtractor.Extract((string)dataJTok["url"]);
                    var linkedImg = extractRet.Item1;
                    var galleryUrls = extractRet.Item2;

                    if (linkedImg != null && linkedImg.ThumbnailLink == null)
                    {
                        linkedImg.ThumbnailLink = (string)dataJTok["thumbnail"];
                    }

                    if (linkedImg == null && galleryUrls != null && galleryUrls.Count > 0)
                    {
                        linkedImg = galleryUrls[0];
                    }

                    // Modify UI, so need to run this on UI thread.
                    if (RunOnUiThread != null)
                    {
                        RunOnUiThread(() =>
                        {
                            item.DisplayingImage = linkedImg;
                        });
                    }
                    else
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            item.DisplayingImage = linkedImg;
                        });
                    }

                    item.GalleryImages = galleryUrls;
                });

                return item;
            })).Result;
            ret = allRedditImages.Where(item => item != null).ToList();


            return ret;
        }
Ejemplo n.º 2
0
        public static List <RedditImage> ParseFromJson(string jsonText, out string nextPath)
        {
            var ret = new List <RedditImage>();

            var jsonObject = JObject.Parse(jsonText);
            var jArr       = jsonObject["data"]["children"] as JArray;

            try
            {
                nextPath = (string)jsonObject["data"]["after"];
            }
            catch (InvalidOperationException)
            {
                nextPath = "null";
            }

            var allRedditImages = Task.WhenAll(jArr.Select(async itemJTok =>
            {
                var dataJTok           = itemJTok["data"];
                var tentativeThumbnail = (string)dataJTok["thumbnail"];

                if (!Uri.IsWellFormedUriString(tentativeThumbnail, UriKind.Absolute))
                {
                    return(null);
                }

                var item = new RedditImage()
                {
                    Title       = System.Net.WebUtility.HtmlDecode((string)dataJTok["title"]),
                    Permalink   = "http://reddit.com" + (string)dataJTok["permalink"],
                    OriginalUrl = (string)dataJTok["url"],
                    NSFW        = (bool)dataJTok["over_18"],
                };

                // Parser task here to determine the correct image and album
                Task.Run(async() =>
                {
                    var extractRet  = await ImgUrlExtractor.Extract((string)dataJTok["url"]);
                    var linkedImg   = extractRet.Item1;
                    var galleryUrls = extractRet.Item2;

                    if (linkedImg != null && linkedImg.ThumbnailLink == null)
                    {
                        linkedImg.ThumbnailLink = (string)dataJTok["thumbnail"];
                    }

                    if (linkedImg == null && galleryUrls != null && galleryUrls.Count > 0)
                    {
                        linkedImg = galleryUrls[0];
                    }

                    // Modify UI, so need to run this on UI thread.
                    if (RunOnUiThread != null)
                    {
                        RunOnUiThread(() =>
                        {
                            item.DisplayingImage = linkedImg;
                        });
                    }
                    else
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            item.DisplayingImage = linkedImg;
                        });
                    }

                    item.GalleryImages = galleryUrls;
                });

                return(item);
            })).Result;

            ret = allRedditImages.Where(item => item != null).ToList();


            return(ret);
        }