Created
          September 10, 2013 18:24 
        
      - 
      
 - 
        
Save uncas/6513434 to your computer and use it in GitHub Desktop.  
    Determine whether email has gravatar or not by checking the length of the downloaded image (6951 equals the default image).
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private static string GetGravatarUrl( | |
| string email, | |
| int size = 200, | |
| string defaultPicture = null) | |
| { | |
| if (string.IsNullOrWhiteSpace(email)) | |
| return null; | |
| byte[] b1 = Encoding.UTF8.GetBytes(email.Trim().ToLower()); | |
| var provider = new MD5CryptoServiceProvider(); | |
| byte[] hash = provider.ComputeHash(b1); | |
| string str = string.Join("", hash.Select(x => x.ToString("x2"))); | |
| string gravatarUrl = | |
| string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=g", str, size); | |
| if (!string.IsNullOrWhiteSpace(defaultPicture)) | |
| return gravatarUrl + "&d=" + HttpUtility.UrlEncode(defaultPicture); | |
| return gravatarUrl; | |
| } | |
| const string sql = | |
| @"SELECT Email FROM Emails WHERE Size IS NULL"; | |
| using ( | |
| var connection = | |
| new SqlConnection( | |
| "Server=.\SqlExpress;Database=test;Integrated Security=true;") | |
| ) | |
| { | |
| IEnumerable<string> emails = connection.Query<string>(sql); | |
| foreach (string email in emails) | |
| { | |
| string url = GetGravatarUrl(email); | |
| var client = new WebClient(); | |
| byte[] picture = client.DownloadData(url); | |
| connection.Execute( | |
| "UPDATE Emails SET Size = @size WHERE Email = @email", | |
| new {email, size = picture.Length}); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment