Exemplo n.º 1
0
        private async void AsyncGetTemplateKey(string apiURL)
        {
            var request = HttpWebRequest.CreateHttp(apiURL);

            byte[] result;
            using (var response = (await SimpleHttpService.GetResponseAsync(request)))
            {
                result = await Task <byte[]> .Run(() =>
                {
                    byte[] buffer = new byte[11];
                    response.GetResponseStream().Read(buffer, 0, 11);
                    return(buffer);
                });
            }

            GifDecoder decoder = new GifDecoder();

            if (decoder.IsSupportedFileFormat(result))
            {
                this.ContentTemplate = GetDataTemplate("Type:Gif");
            }
            else
            {
                this.ContentTemplate = GetDataTemplate("Type:Else");
            }
            this.ApplyTemplate();
        }
Exemplo n.º 2
0
        internal static async Task <IEnumerable <Tuple <string, string> > > GetImagesFromUri(string title, Uri uri)
        {
            var             href        = uri.OriginalString;
            var             groups      = hashRe.Match(href).Groups;
            GroupCollection albumGroups = null;

            if (groups.Count == 0 || (groups.Count > 0 && string.IsNullOrWhiteSpace(groups[0].Value)))
            {
                albumGroups = albumHashRe.Match(href).Groups;
            }

            if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
            {
                if (Regex.IsMatch(groups[1].Value, "[&,]"))
                {
                    var hashes = Regex.Split(groups[1].Value, "[&,]");
                    //Imgur doesn't really care about the extension and the browsers don't seem to either.
                    return(hashes
                           .Select(hash => Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", hash))));
                }
                else
                {
                    //Imgur doesn't really care about the extension and the browsers don't seem to either.
                    return(new Tuple <string, string>[] { Tuple.Create(title, string.Format("http://i.imgur.com/{0}.gif", groups[1].Value)) });
                }
            }
            else if (albumGroups.Count > 2 && string.IsNullOrWhiteSpace(albumGroups[2].Value))
            {
                var    apiURL  = string.Format("{0}album/{1}.json", apiPrefix, albumGroups[1].Value);
                var    request = HttpWebRequest.CreateHttp(apiURL);
                string jsonResult;
                using (var response = (await SimpleHttpService.GetResponseAsync(request)))
                {
                    jsonResult = await Task <string> .Run(() =>
                    {
                        using (var sr = new StreamReader(response.GetResponseStream()))
                        {
                            return(sr.ReadToEnd());
                        }
                    });
                }
                var result = JsonConvert.DeserializeObject(jsonResult) as JObject;
                return(((IEnumerable)((JObject)result.GetValue("album")).GetValue("images"))
                       .Cast <JObject>()
                       .Select(e => Tuple.Create((string)((JObject)e.GetValue("image")).GetValue("caption"), (string)((JObject)e.GetValue("links")).GetValue("original"))));
            }
            else
            {
                return(Enumerable.Empty <Tuple <string, string> >());
            }
        }
Exemplo n.º 3
0
        internal static async Task <IEnumerable <Tuple <string, string> > > GetImagesFromUri(string title, Uri uri)
        {
            var href   = uri.OriginalString.Split('?')[0];
            var groups = hashRe.Match(href).Groups;

            if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
            {
                var photoID = groups[1].Value;
                var apiURL  = string.Format("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=81afa34d85f53254ff12a8cb73cba64d&photo_id={0}&format=json&nojsoncallback=1", photoID);

                var    request = HttpWebRequest.CreateHttp(apiURL);
                string jsonResult;
                using (var response = (await SimpleHttpService.GetResponseAsync(request)))
                {
                    jsonResult = await Task <string> .Run(() =>
                    {
                        using (var sr = new StreamReader(response.GetResponseStream()))
                        {
                            return(sr.ReadToEnd());
                        }
                    });
                }

                dynamic result = JsonConvert.DeserializeObject(jsonResult);

                var biggest = 0;
                var source  = "";
                foreach (var sz in result.sizes.size)
                {
                    if ((int)sz.width > biggest)
                    {
                        biggest = (int)sz.width;
                        source  = sz.source;
                    }
                }
                return(new Tuple <string, string>[] { Tuple.Create(title, source) });
            }
            else
            {
                return(Enumerable.Empty <Tuple <string, string> >());
            }
        }
Exemplo n.º 4
0
        internal static async Task <IEnumerable <Tuple <string, string> > > GetImagesFromUri(string title, Uri uri)
        {
            var href   = uri.OriginalString.Split('?')[0];
            var groups = hashRe.Match(href).Groups;

            if (groups.Count > 2 && string.IsNullOrWhiteSpace(groups[2].Value))
            {
                var hash = groups[1].Value;
                if (hash.StartsWith("m"))
                {
                    var    apiURL  = "http://min.us/api/GetItems/" + hash;
                    var    request = HttpWebRequest.CreateHttp(apiURL);
                    string jsonResult;
                    using (var response = (await SimpleHttpService.GetResponseAsync(request)))
                    {
                        jsonResult = await Task <string> .Run(() =>
                        {
                            using (var sr = new StreamReader(response.GetResponseStream()))
                            {
                                return(sr.ReadToEnd());
                            }
                        });
                    }
                    dynamic result = JsonConvert.DeserializeObject(jsonResult);
                    return(new Tuple <string, string>[] { Tuple.Create(title, (string)result.src) });
                }
                else
                {
                    return(Enumerable.Empty <Tuple <string, string> >());
                }
            }
            else
            {
                return(Enumerable.Empty <Tuple <string, string> >());
            }
        }