how to tell if a url is for default blue image gravatar

时间:2015-07-31 19:56:30

标签: gravatar

Is there a way to tell if a url is for default blue gravatar image?

Here are two urls for the default Gravatar image:

http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?s=120

http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120

Here is a url that has an image:

http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120

Is there something in these url's that I can search for that the default blue gravatar image url has that the non default gravatas do not have?


Edit:

What I am trying to do is: I have an excel sheet downloaded from an app that has a column for gravatar urls. I need to delete all the links that go the the default blue gravatar image.

2 个答案:

答案 0 :(得分:1)

Gravatar avatar image URLs that are the default image will return a 404 Not Found error if the following parameter is set d=404. For example, here are the URLs that you used as examples, but with the parameter set:

http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?s=120&d=404

http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404

http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404


Assuming you're detecting if the images default using JavaScript, you can then use AJAX (without displaying the image) or an error catcher (displaying non-default images) to detect if these images successfully loaded.

jQuery (AJAX)

// Image Exists
$.ajax({url:"http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404",type:"GET",crossDomain:true,success:(function(){console.log("Custom Gravatar");}),error:(function(){console.log("Default Gravatar");})});

// Image Does Not Exist
$.ajax({url:"http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404",type:"GET",crossDomain:true,success:(function(){console.log("Custom Gravatar");}),error:(function(){console.log("Default Gravatar");})});

Error Catching

You can use either the jQuery load and error event handlers, or the HTML onload and onerror attributes.

$("img").load(function(e) {
  e.target.parentNode.parentNode.getElementsByClassName("stat")[0].innerHTML = e.type;
}).error(function(e) {
  e.target.parentNode.parentNode.getElementsByClassName("stat")[0].innerHTML = e.type;
});
table,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
table img {
  width: 48px;
  height: 48px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>Image</td>
      <td>Expected Result</td>
      <td>Actual Result</td>
    </tr>
    <tr>
      <td>
        <img id="good" src="http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?s=120&d=404" />
      </td>
      <td>load</td>
      <td class="stat">Loading...</td>
    </tr>
    <tr>
      <td>
        <img id="bad" src="http://www.gravatar.com/avatar/88a771dc1a611b2038c9a0ad0770b595?s=120&d=404" />
      </td>
      <td>error</td>
      <td class="stat">Loading...</td>
    </tr>
  </tbody>
</table>


EDIT: OP clarified what was being asked for

I wrote a small VBA script in this example file to iterate through the first column up until the first empty cell, creating WinHTTP requests with a modified URL then, as OP asked, deleting the contents of cells that contained a link to the default Gravatar avatar.

To run the code in the sample Excel file:

  • Excel 2003 and lower: Tools > Macro > Macros (Alt + F8) > checkGravatar
  • Excel 2007 and newer: Develooper > Macros > checkGravatar

In order to run the VBA, you may also need to enable macros and reference MSXML.

Sub checkGravatar()
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    Dim URL As String
    Dim goodStat As String
    Dim badStat As String
    Dim row As Integer
    Dim pos As Integer
    row = 1
    URL = Cells(row, 1).Value
    Do While Len(URL) > 0
        If InStr(URL, "gravatar.com/avatar/") > 0 Then
            If InStr(URL, "?") = 0 Then
                URL = URL & "?d=404"
            Else
                If Not InStr(URL, "&d=") Then
                    URL = URL & "&d=404"
                End If
            End If
            objHTTP.Open "GET", URL, False
            objHTTP.send ("")
            If objHTTP.Status = 404 Then
                Cells(row, 1).Value = ""
        Else
            MsgBox "GET request failed"
        End If
        row = row + 1
        URL = Cells(row, 1).Value
    Loop
End Sub

答案 1 :(得分:0)

You can try to pass a default image of your choice, and see what is returned.

URLs mapping to existing gravatars will ignore the default image parameter, returning an image.

URLs mapping do no images will use the default you provide (instead of the blue G). If you pass something invalid (empty string), you'll get a 400.

Compare the existing one: http://www.gravatar.com/avatar/d8f98df8a6ed24a727b993ea01cc91f6?d=%22%22

With the non-existing one: http://www.gravatar.com/avatar/19b2990ba88512ab38abdbbca5701d27?d=%22%22

That said... It's quite rude to deliberately cause errors on someone else's API.
You could fire off some background worker that will GET the images and see what's returned.