コード例 #1
0
        private static FavoriteImage favoriteImageArray(XElement root)
        {
            string        link  = "";
            bool          found = false;
            FavoriteImage img   = new FavoriteImage();

            try
            {
                IEnumerable <XElement> items = root.XPathSelectElement("//images").Elements();
                foreach (XElement item in items)
                {
                    foreach (XElement attr in items.Elements())
                    {
                        if ((attr.Name == "link" || attr.Value.StartsWith("https://i.imgur.com/")) && !attr.Value.Contains("."))
                        {
                            break;
                        }
                        if (attr.Name == "link" || attr.Value.StartsWith("https://i.imgur.com/"))
                        {
                            img.Source = attr.Value;
                            found      = true;
                        }
                        if (attr.Name == "id")
                        {
                            img.Id = attr.Value;
                        }
                        if (attr.Name == "datetime")
                        {
                            img.Time = new DateTime(Convert.ToInt32(attr.Value));
                        }
                        if (attr.Name == "type")
                        {
                            img.Type = attr.Value;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }
            }
            catch
            {
                found = false;
            }
            return(img);
        }
コード例 #2
0
 public static void HandleAccountFavoritesCallBack(ClientData data, string value)
 {
     try
     {
         if (!value.Contains("success"))
         {
             Console.WriteLine("Can't handle favorites images.");
             return;
         }
         value = value.Remove(0, 37);
         value = value.Remove(value.Length - 4, 4);
         string[] entries = value.Split(new string[] { ",\"size\":0}," }, StringSplitOptions.RemoveEmptyEntries);
         if (entries.Length == 0)
         {
             Console.WriteLine("Can't handle favorites images.");
             return;
         }
         for (int i = 0; i < entries.Length; i++)
         {
             XmlDictionaryReader jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(entries[i] + "}"),
                                                                                       new System.Xml.XmlDictionaryReaderQuotas());
             XElement      root = XElement.Load(jsonReader);
             FavoriteImage img  = favoriteImageArray(root);
             if ((img == null || img.Source == null || img.Source == string.Empty) && entries[i].Contains("cover"))
             {
                 string url = "https://i.imgur.com/" + root.XPathSelectElement("//cover").Value + ".png";
                 img = new FavoriteImage(root.XPathSelectElement("//id").Value,
                                         root.XPathSelectElement("//type").Value,
                                         root.XPathSelectElement("//datetime").Value,
                                         url,
                                         root.XPathSelectElement("//width").Value,
                                         root.XPathSelectElement("//height").Value);
             }
             if (img.Source == null)
             {
                 continue;
             }
             data.Favorites.Add(img);
         }
     } catch { }
 }