/// <summary> /// Writes the media entity. /// </summary> /// <param name="w">The w.</param> /// <param name="e">The e.</param> private static void WriteMediaEntity(JsonWriter w, MediaEntity e) { w.WritePropertyName("type"); switch (e.MediaType) { case MediaEntity.MediaTypes.Unknown: w.WriteNull(); break; case MediaEntity.MediaTypes.Photo: w.WriteValue("photo"); break; default: break; } w.WritePropertyName("sizes"); w.WriteStartObject(); { foreach (var item in e.Sizes) { w.WritePropertyName(item.Size.ToString().ToLower()); w.WriteStartObject(); { w.WritePropertyName("h"); w.WriteValue(item.Height); w.WritePropertyName("w"); w.WriteValue(item.Width); w.WritePropertyName("resize"); w.WriteValue(item.Resize == MediaEntity.MediaSize.MediaSizeResizes.Fit ? "fit" : "crop"); w.WriteEndObject(); } } w.WriteEndObject(); } w.WritePropertyName("id"); w.WriteValue(e.Id); w.WritePropertyName("id_str"); w.WriteValue(e.IdString); w.WritePropertyName("media_url"); w.WriteValue(e.MediaUrl); w.WritePropertyName("media_url_https"); w.WriteValue(e.MediaUrlSecure); w.WritePropertyName("url"); w.WriteValue(e.Url); w.WritePropertyName("display_url"); w.WriteValue(e.DisplayUrl); w.WritePropertyName("expanded_url"); w.WriteValue(e.ExpandedUrl); }
/// <summary> /// Parses the media entity. /// </summary> /// <param name="reader">The reader.</param> /// <returns></returns> public MediaEntity parseMediaEntity(JsonReader reader) { try { if (reader.TokenType != JsonToken.StartObject) { return(null); } MediaEntity entity = new MediaEntity(); int startDepth = reader.Depth; // Start looping through all of the child nodes while (reader.Read() && reader.Depth >= startDepth) { // If the current node isn't a property, skip it if (reader.TokenType != JsonToken.PropertyName) { continue; } string fieldName = reader.Value as string; if (string.IsNullOrEmpty(fieldName)) { continue; } switch (fieldName) { case "type": entity.MediaType = string.IsNullOrEmpty((string)reader.Value) ? MediaEntity.MediaTypes.Unknown : MediaEntity.MediaTypes.Photo; break; case "sizes": entity.Sizes = new List <MediaEntity.MediaSize>(); break; case "large": case "medium": case "small": case "thumb": if (reader.TokenType != JsonToken.PropertyName) { break; } MediaEntity.MediaSize newSize = new MediaEntity.MediaSize(); switch ((string)reader.Value) { case "large": newSize.Size = MediaEntity.MediaSize.MediaSizes.Large; break; case "medium": newSize.Size = MediaEntity.MediaSize.MediaSizes.Medium; break; case "small": newSize.Size = MediaEntity.MediaSize.MediaSizes.Small; break; case "thumb": newSize.Size = MediaEntity.MediaSize.MediaSizes.Thumb; break; default: break; } int sizeDepth = reader.Depth; // Loop through all of the properties of the size and read their values while (reader.Read() && sizeDepth < reader.Depth) { if (reader.TokenType != JsonToken.PropertyName) { continue; } ReadFieldValue(reader, "h", newSize, () => newSize.Height); ReadFieldValue(reader, "w", newSize, () => newSize.Width); if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "resize") { reader.Read(); newSize.Resize = string.IsNullOrEmpty((string)reader.Value) ? MediaEntity.MediaSize.MediaSizeResizes.Unknown : ((string)reader.Value == "fit" ? MediaEntity.MediaSize.MediaSizeResizes.Fit : MediaEntity.MediaSize.MediaSizeResizes.Crop); } } entity.Sizes.Add(newSize); break; case "indices": reader.Read(); reader.Read(); entity.StartIndex = Convert.ToInt32((long)reader.Value); reader.Read(); entity.EndIndex = Convert.ToInt32((long)reader.Value); break; default: break; } ReadFieldValue(reader, "id", entity, () => entity.Id); ReadFieldValue(reader, "id_str", entity, () => entity.IdString); ReadFieldValue(reader, "media_url", entity, () => entity.MediaUrl); ReadFieldValue(reader, "media_url_https", entity, () => entity.MediaUrlSecure); ReadFieldValue(reader, "url", entity, () => entity.Url); ReadFieldValue(reader, "display_url", entity, () => entity.DisplayUrl); ReadFieldValue(reader, "expanded_url", entity, () => entity.ExpandedUrl); } return(entity); } catch { return(null); } }
internal static MediaEntity CheckEntityandCreateMedia(UrlEntity u) { string absolutePath; if (((((!u.ExpandedUrl.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase) && !u.ExpandedUrl.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase)) && (!u.ExpandedUrl.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) && !u.ExpandedUrl.Contains("instagr.am", StringComparison.InvariantCultureIgnoreCase))) && ((!u.ExpandedUrl.Contains("instagram.com", StringComparison.InvariantCultureIgnoreCase) && !u.ExpandedUrl.Contains("yfrog.com", StringComparison.InvariantCultureIgnoreCase)) && (!u.ExpandedUrl.Contains("twitpic.com", StringComparison.InvariantCultureIgnoreCase) && !u.ExpandedUrl.Contains("youtube.com", StringComparison.InvariantCultureIgnoreCase)))) && ((!u.ExpandedUrl.Contains("youtu.be", StringComparison.InvariantCultureIgnoreCase) && !u.ExpandedUrl.Contains("sdrv.ms", StringComparison.InvariantCultureIgnoreCase)) && !u.ExpandedUrl.Contains("skydrive.live.com", StringComparison.InvariantCultureIgnoreCase))) && !u.ExpandedUrl.Contains("lockerz", StringComparison.InvariantCultureIgnoreCase)) { return null; } MediaEntity entity = new MediaEntity { DisplayUrl = u.DisplayUrl, ExpandedUrl = u.ExpandedUrl, Url = u.Url, EndIndex = u.EndIndex, StartIndex = u.StartIndex, MediaType = MediaEntity.MediaTypes.Photo }; if (u.ExpandedUrl.Contains("instagr.am", StringComparison.InvariantCultureIgnoreCase) || u.ExpandedUrl.Contains("instagram.com", StringComparison.InvariantCultureIgnoreCase)) { absolutePath = u.ExpandedUrl.EndsWith("/") ? u.ExpandedUrl.Remove(u.ExpandedUrl.Length - 1) : u.ExpandedUrl; entity.MediaUrl = absolutePath + "/media/"; entity.MediaUrlSecure = absolutePath + "/media/"; return entity; } if (u.ExpandedUrl.Contains("yfrog.com", StringComparison.InvariantCultureIgnoreCase)) { entity.MediaUrl = u.ExpandedUrl + ":medium"; entity.MediaUrlSecure = u.ExpandedUrl + ":medium"; return entity; } if (u.ExpandedUrl.Contains("twitpic.com", StringComparison.InvariantCultureIgnoreCase)) { absolutePath = new Uri(u.ExpandedUrl).AbsolutePath; entity.MediaUrl = "https://twitpic.com/show/full" + absolutePath; entity.MediaUrlSecure = u.ExpandedUrl; return entity; } if (u.ExpandedUrl.Contains("youtube.com", StringComparison.InvariantCultureIgnoreCase) || u.ExpandedUrl.Contains("youtu.be", StringComparison.InvariantCultureIgnoreCase)) { entity.MediaType = MediaEntity.MediaTypes.Unknown; Uri uri = new Uri(u.ExpandedUrl); if (u.ExpandedUrl.Contains("v=", StringComparison.InvariantCultureIgnoreCase)) { absolutePath = HttpUtility.ParseQueryString(uri.Query).Get("v"); } else { absolutePath = uri.AbsolutePath.Replace("/", ""); } if (!string.IsNullOrEmpty(absolutePath)) { entity.MediaUrl = "http://i.ytimg.com/vi/" + absolutePath + "/0.jpg"; entity.MediaUrlSecure = u.ExpandedUrl; return entity; } return null; } if (u.ExpandedUrl.Contains("sdrv.ms", StringComparison.InvariantCultureIgnoreCase) || u.ExpandedUrl.Contains("skydrive.live.com", StringComparison.InvariantCultureIgnoreCase)) { entity.MediaUrl = "https://apis.live.net/v5.0/skydrive/get_item_preview?type=normal&url=" + HttpUtility.UrlEncode(u.ExpandedUrl); entity.MediaUrlSecure = "https://apis.live.net/v5.0/skydrive/get_item_preview?type=normal&url=" + HttpUtility.UrlEncode(u.ExpandedUrl); return entity; } if (u.ExpandedUrl.Contains("lockerz", StringComparison.OrdinalIgnoreCase)) { entity.MediaUrl = "http://api.plixi.com/api/tpapi.svc/imagefromurl?url=" + u.ExpandedUrl + "&size=medium"; entity.MediaUrlSecure = "http://api.plixi.com/api/tpapi.svc/imagefromurl?url=" + u.ExpandedUrl + "&size=medium"; return entity; } entity.MediaUrl = u.ExpandedUrl; entity.MediaUrlSecure = u.ExpandedUrl; return entity; }