Beispiel #1
0
        public void BeginGetAvatars(string searchText, AvatarCallback callback)
        {
            Uri            url     = new Uri(Flickr.FLICKR_PHOTOS_SEARCH_URL + "&sort=relevance&api_key=" + Flickr.FLICKR_API_KEY + "&text=" + HttpUtility.UrlEncode(searchText));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.BeginGetResponse(delegate(IAsyncResult result) {
                var response = request.EndGetResponse(result);
                string xml   = null;
                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    xml = reader.ReadToEnd();
                }

                var doc = new XmlDocument();
                doc.LoadXml(xml);

                List <AvatarInfo> infos = new List <AvatarInfo>();

                foreach (XmlElement element in doc.SelectNodes("/rsp/photos/photo"))
                {
                    string photoTitle = element.GetAttribute("title");


                    string photoThumbnailUrl = String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_s.jpg",
                                                             element.GetAttribute("farm"),
                                                             element.GetAttribute("server"),
                                                             element.GetAttribute("id"),
                                                             element.GetAttribute("secret"));

                    string photoUrl = String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_t.jpg",
                                                    element.GetAttribute("farm"),
                                                    element.GetAttribute("server"),
                                                    element.GetAttribute("id"),
                                                    element.GetAttribute("secret"));

                    infos.Add(new AvatarInfo(photoTitle, photoThumbnailUrl, photoUrl));
                }

                callback(this, infos.ToArray());
            }, null);
        }
        public void BeginGetAvatars(string searchText, AvatarCallback callback)
        {
            Uri url = new Uri(Flickr.FLICKR_PHOTOS_SEARCH_URL + "&sort=relevance&api_key=" + Flickr.FLICKR_API_KEY + "&text=" + HttpUtility.UrlEncode(searchText));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.BeginGetResponse(delegate (IAsyncResult result) {
                var response = request.EndGetResponse(result);
                string xml = null;
                using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                    xml = reader.ReadToEnd();
                }

                var doc = new XmlDocument();
                doc.LoadXml(xml);

                List<AvatarInfo> infos = new List<AvatarInfo>();

                foreach (XmlElement element in doc.SelectNodes("/rsp/photos/photo")) {
                    string photoTitle = element.GetAttribute("title");

                    string photoThumbnailUrl = String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_s.jpg",
                                                             element.GetAttribute("farm"),
                                                             element.GetAttribute("server"),
                                                             element.GetAttribute("id"),
                                                             element.GetAttribute("secret"));

                    string photoUrl = String.Format("http://farm{0}.static.flickr.com/{1}/{2}_{3}_t.jpg",
                                                    element.GetAttribute("farm"),
                                                    element.GetAttribute("server"),
                                                    element.GetAttribute("id"),
                                                    element.GetAttribute("secret"));

                    infos.Add(new AvatarInfo(photoTitle, photoThumbnailUrl, photoUrl));
                }

                callback(this, infos.ToArray());
            }, null);
        }
Beispiel #3
0
        public void RetrieveAvatar(int userId, AvatarCallback acb)
        {
            lock (_lock)
            {
                if (images.ContainsKey(userId))
                {
                    acb(images[userId]);
                    return;
                }
            }

            //DL the img
            try
            {
                var request = WebRequest.CreateHttp("http://facepunch.com/image.php?u=" + userId);
                request.Method = "GET";

                request.BeginGetResponse(
                    a =>
                {
                    try
                    {
                        var response = (HttpWebResponse)request.EndGetResponse(a);

                        var responseStream = response.GetResponseStream();

                        //This ungodly code is from stackoverflow...
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            responseStream.CopyTo(memoryStream);
                            memoryStream.Position = 0;
                            byte[] buffer         = null;
                            if (memoryStream != null && memoryStream.Length > 0)
                            {
                                BinaryReader binaryReader = new BinaryReader(memoryStream);
                                buffer        = binaryReader.ReadBytes((int)memoryStream.Length);
                                Stream stream = new MemoryStream();
                                stream.Write(buffer, 0, buffer.Length);
                                stream.Seek(0, SeekOrigin.Begin);
                                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                                {
                                    BitmapImage bitmapImage = new BitmapImage {
                                        CreateOptions = BitmapCreateOptions.None
                                    };
                                    bitmapImage.SetSource(stream);

                                    lock (_lock)
                                    {
                                        if (images.ContainsKey(userId))         //In case there were more than one call at the same time
                                        {
                                            acb(images[userId]);
                                        }
                                        else
                                        {
                                            images.Add(userId, bitmapImage);
                                            acb(bitmapImage);
                                        }
                                    }
                                });
                            }
                        }
                    }
                    catch (WebException ex)
                    {
                        Logger.WriteLine(ex);
                    }
                }, null);
            }
            catch (WebException ex)
            {
                Logger.WriteLine(ex);
            }
        }