コード例 #1
0
        public static void GetFlickrImageSizes(ImageFeedItem imageFeedItem, string flickrApiKey, GetFlickrImageSizesCallback callback)
        {
            if (imageFeedItem == null || (imageFeedItem.Sizes != null && imageFeedItem.Sizes.Count > 0) || imageFeedItem.SourceType != SourceType.Flickr)
            {
                callback(imageFeedItem);
            }

            string          query   = string.Format(CultureInfo.InvariantCulture, "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key={0}&photo_id={1}", flickrApiKey, imageFeedItem.ServiceId);
            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri(query));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(imageFeedItem);
                }

                try
                {
                    XDocument doc = XDocument.Parse(e.Response);
                    imageFeedItem.Sizes = new Dictionary <Size, Uri>();
                    foreach (XElement size in doc.Descendants("size"))
                    {
                        imageFeedItem.Sizes[new Size(int.Parse(size.Attribute("width").Value, CultureInfo.InvariantCulture), int.Parse(size.Attribute("height").Value, CultureInfo.InvariantCulture))] = new Uri(size.Attribute("source").Value);
                    }

                    callback(imageFeedItem);
                }
                catch
                {
                    callback(imageFeedItem);
                }
            };
        }
コード例 #2
0
        /// <summary>
        /// Extracts image links to the TwitPic service from status messages.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>True if an image link was found, false otherwise.</returns>
        private static bool ExtractTwitPicImageLink(string status, GetImageLinkCallback callback)
        {
            Match match = Regex.Match(status, @"http://(www\.)?twitpic.com/(\w+)\b", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return(false);
            }

            string twitpic   = match.Groups[0].Value;
            string twitpicId = match.Groups[2].Value;

            status = status.Replace(twitpic, string.Empty).Trim();

            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri(string.Format(CultureInfo.InvariantCulture, "http://api.twitpic.com/2/media/show.xml?id={0}", twitpicId)));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null, status);
                }
                else
                {
                    callback(new Uri(@"http://twitpic.com/show/large/" + match.Groups[2].Value), status);
                }
            };

            return(true);
        }
コード例 #3
0
        internal static void GetTwitterUserIdFromUserName(string username, GetTwitterUserIdFromUserNameCallback callback)
        {
            if (_twitterUserNameCache.ContainsValue(username))
            {
                callback((from kvp in _twitterUserNameCache where kvp.Value == username select kvp.Key).FirstOrDefault());
                return;
            }

            string          query   = string.Format(CultureInfo.InvariantCulture, "http://api.twitter.com/1/users/show.xml?screen_name={0}", username);
            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri(query));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null);
                }

                try
                {
                    string userid = XDocument.Parse(e.Response).Element("user").Element("id").Value;
                    _twitterUserNameCache[userid] = username;
                    callback(userid);
                }
                catch
                {
                    callback(null);
                }
            };
        }
コード例 #4
0
        internal static void GetFlickrGroupIdFromGroupName(string groupname, string apiKey, GetFlickrGroupIdFromGroupNameCallback callback)
        {
            if (_flickrGroupNameCache.ContainsValue(groupname))
            {
                callback((from kvp in _flickrGroupNameCache where kvp.Value == groupname select kvp.Key).FirstOrDefault());
                return;
            }

            string          groupurl = string.Format(CultureInfo.InvariantCulture, "http://www.flickr.com/groups/{0}", groupname);
            string          query    = string.Format(CultureInfo.InvariantCulture, "http://api.flickr.com/services/rest/?method=flickr.urls.lookupGroup&api_key={0}&url={1}", apiKey, groupurl);
            AsyncWebRequest request  = new AsyncWebRequest();

            request.Request(new Uri(query));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null);
                }

                try
                {
                    string groupid = XDocument.Parse(e.Response).Element("rsp").Element("group").Attribute("id").Value;
                    _flickrUserNameCache[groupid] = groupname;
                    callback(groupid);
                }
                catch
                {
                    callback(null);
                }
            };
        }
コード例 #5
0
        internal static void GetFlickrUserIdFromUserName(string username, string apiKey, GetFlickrUserIdFromUserNameCallback callback)
        {
            if (_flickrUserNameCache.ContainsValue(username))
            {
                callback((from kvp in _flickrUserNameCache where kvp.Value == username select kvp.Key).FirstOrDefault());
                return;
            }

            string          query   = string.Format(CultureInfo.InvariantCulture, "http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key={0}&username={1}", apiKey, username);
            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri(query));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null);
                }

                try
                {
                    string userid = XDocument.Parse(e.Response).Element("rsp").Element("user").Attribute("nsid").Value;
                    _flickrUserNameCache[userid] = username;
                    callback(userid);
                }
                catch
                {
                    callback(null);
                }
            };
        }
コード例 #6
0
        /// <summary>
        /// Initiates a request to the feed service.
        /// </summary>
        protected virtual void Poll()
        {
            if (_retryDateTime > DateTime.Now)
            {
                return;
            }

            _timer.Stop();

            AsyncWebRequest request = new AsyncWebRequest();

            request.Result += Request_Result;
            request.Request(BuildQuery());
        }
コード例 #7
0
        private static bool ExtractYFrogImageLink(string status, GetImageLinkCallback callback)
        {
            Match match = Regex.Match(status, @"http://(www\.)?yfrog.com/(\w+)\b", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return(false);
            }

            status = status.Replace(match.Groups[0].Value, string.Empty).Trim();
            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri("http://yfrog.com/api/xmlInfo?path=" + match.Groups[2].Value));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null, status);
                }

                try
                {
                    using (StringReader stringReader = new StringReader(e.Response))
                    {
                        XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
                        while (xmlTextReader.Read())
                        {
                            if (xmlTextReader.MoveToContent() == XmlNodeType.Element && xmlTextReader.Name == "image_link")
                            {
                                string uriString = xmlTextReader.ReadString();
                                string extension = Path.GetExtension(uriString).ToUpper(CultureInfo.InvariantCulture);
                                if (extension == ".JPG" || extension == ".PNG" || extension == ".GIF")
                                {
                                    // sometimes there will be video files, and we don't want that.
                                    callback(new Uri(uriString), status);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    callback(null, status);
                }
            };

            return(true);
        }
コード例 #8
0
        private static bool ExtractTweetPhotoImageLink(string status, GetImageLinkCallback callback)
        {
            Match match = Regex.Match(status, @"http://(www\.)?tweetphoto.com/(\d+)\b", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                return(false);
            }

            status = status.Replace(match.Groups[0].Value, string.Empty).Trim();
            AsyncWebRequest request = new AsyncWebRequest();

            request.Request(new Uri("http://tweetphotoapi.com/api/tpapi.svc/photos/" + match.Groups[2].Value));
            request.Result += (sender, e) =>
            {
                if (e.Status != HttpStatusCode.OK)
                {
                    callback(null, status);
                }

                try
                {
                    using (StringReader stringReader = new StringReader(e.Response))
                    {
                        XmlTextReader reader = new XmlTextReader(stringReader);
                        while (reader.Read())
                        {
                            if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "BigImageUrl")
                            {
                                callback(new Uri(reader.ReadString()), status);
                            }
                        }
                    }
                }
                catch
                {
                    callback(null, status);
                }
            };

            return(true);
        }