private async Task UpdateUserInformation(string Username, BlogRollType Type)
        {
            try
            {
                var needInsert = true;
                var br         = DB.BlogRolls.SingleOrDefault(x => x.GitHubId == Username);
                if (br == null)
                {
                    br = new BlogRoll();
                }
                else
                {
                    needInsert = false;
                    if (br.Type == BlogRollType.Follower && Type == BlogRollType.Following)
                    {
                        return;
                    }
                }
                br.Type     = Type;
                br.GitHubId = Username;
                var url = $"https://github.com/{ Config["BlogRoll:GitHub"] }";
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(url);
                    var result = await client.GetAsync("/" + Username);

                    var fullnameRegex = new Regex(@"(?<=<div class=""vcard-fullname"" itemprop=""name"">).*(?=</div>)");
                    var html          = await result.Content.ReadAsStringAsync();

                    br.NickName = fullnameRegex.Match(html).Value;
                    if (string.IsNullOrEmpty(br.NickName))
                    {
                        br.NickName = Username;
                    }
                    var websiteRegex = new Regex(@"(?<=</svg><a href=""http).*(?="" class=""url"" rel="")");
                    var website      = websiteRegex.Match(html).Value;
                    br.URL = string.IsNullOrEmpty(website) ? null : "http" + website;
                    var avatarRegex = new Regex(@"(?<=<meta content="").*(?="" name=""twitter:image:src"" /><meta content="")");
                    var avatarURL   = avatarRegex.Match(html).Value.Replace("&amp;", "&").Replace("s=400", "s=150");
                    if (br.AvatarId == null)
                    {
                        br.AvatarId = await UpdateAvatar(avatarURL);
                    }
                    else
                    {
                        await UpdateAvatar(avatarURL, br.AvatarId.Value);
                    }
                }

                if (needInsert)
                {
                    DB.BlogRolls.Add(br);
                }
                DB.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemple #2
0
        public void CanValidateBlogRoll()
        {
            var results = new ValidationResults();
            var widget  = new BlogRoll()
            {
                Header = "users", Zone = "left"
            };

            // Confirm errors.
            widget.NumberOfEntries = 0;
            widget.BloggerName     = "";
            widget.Url             = "alkjsdflk";
            widget.Format          = "";
            bool success = widget.Validate(results);

            Assert.IsFalse(success);
            Assert.AreEqual(results.Count, 4);

            results.Clear();
            widget.NumberOfEntries = 20;
            widget.BloggerName     = "kishore";
            widget.Url             = "http://kishore.com/rss";
            widget.Format          = "rss";
            success = widget.Validate(results);
            Assert.IsTrue(success);
            Assert.AreEqual(results.Count, 0);
        }
 public void SetBlogRoll(IEnumerable <BlogPost> blogRoll)
 {
     foreach (var blog in blogRoll)
     {
         BlogRoll.Add(blog);
     }
 }
Exemple #4
0
 public void SetBlogRoll(IEnumerable <BlogPost> blogPosts)
 {
     foreach (var blog in blogPosts)
     {
         if (blog.IsApproved)
         {
             BlogRoll.Add(blog);
         }
     }
 }