/// <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); }
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); }
/// <summary> /// Parses a status message for known URLs for image hosting services, and returns a URL to the image. /// </summary> /// <param name="status">The status text.</param> /// <param name="callback">The callback.</param> /// <returns>Whether or not an image URL was found.</returns> internal static bool GetImageLink(string status, GetImageLinkCallback callback) { if (ExtractYFrogImageLink(status, callback)) { return(true); } if (ExtractTwitPicImageLink(status, callback)) { return(true); } if (ExtractTweetPhotoImageLink(status, callback)) { return(true); } return(false); }
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); }
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; }
/// <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; }
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; }
/// <summary> /// Parses a status message for known URLs for image hosting services, and returns a URL to the image. /// </summary> /// <param name="status">The status text.</param> /// <param name="callback">The callback.</param> /// <returns>Whether or not an image URL was found.</returns> internal static bool GetImageLink(string status, GetImageLinkCallback callback) { if (ExtractYFrogImageLink(status, callback)) { return true; } if (ExtractTwitPicImageLink(status, callback)) { return true; } if (ExtractTweetPhotoImageLink(status, callback)) { return true; } return false; }