简体   繁体   English

我们如何知道用户通过 api 更改了 github 中默认的个人资料图片

[英]how do we know the user changed profile picture from default in github via api

I'm working on a script to audit Github.我正在编写一个脚本来审核 Github。 so in that process im trying to find a way to know if the GitHub user in my organization has changed their profile picture from the default profile picture.所以在这个过程中,我试图找到一种方法来了解我组织中的 GitHub 用户是否已从默认的个人资料图片更改了他们的个人资料图片。

Im working with the go-github package to access Github.我正在使用go-github包来访问 Github。 https://github.com/google/go-github/blob/ababee01b03f69965d0ec370e65b61ec7967be34/github/users.go https://github.com/google/go-github/blob/ababee01b03f69965d0ec370e65b61ec7967be34/github/users.go

Currently, I can get the list of users from ListMembers method and GetAvatarURL gets organization users profile image.目前,我可以从ListMembers方法获取用户列表,而GetAvatarURL可以获取组织用户的个人资料图片。 But I can't get any differentiation from default github image and the user changed image.但是我无法从默认的 github 图像和用户更改的图像中获得任何区别。

Following is the default image ima talking about.以下是 ima 谈论的默认图像。

在此处输入图片说明

I get the AvatarURL like https://avatars0.githubusercontent.com/u/XXXXXX?v=4 This is the same for default and for images uploaded by the users.我得到像https://avatars0.githubusercontent.com/u/XXXXXX?v=4这样的 AvatarURL 这对于默认值和用户上传的图像是相同的。 only change is the host server of the images like avatars1 , avatars2 , avatars3 & avatars4 .唯一的变化是图像的主机服务器,如avatars1avatars2avatars3avatars4

Is there any other way to find the difference?有没有其他方法可以找到差异?

One way to do this is to get some information about the images using tools like ImageMagick一种方法是使用ImageMagick 等工具获取有关图像的一些信息

After checking the Github auto-generated identicons , there are 2 check that would be interesting :检查 Github 自动生成的identicons 后,有 2 个检查会很有趣:

  • check that the image has 2 unique colors :检查图像是否有 2 种独特的颜色:

     identify -format %k github_avatar.png

output:输出:

2
  • one of this color is grey-ish F0F0F0这种颜色之一是灰色的 F0F0F0

     convert github_avatar.png -colors 2 \\ -format "%c" histogram:info: | \\ cut -d'#' -f2 | cut -d' ' -f1

output:输出:

D65193
F0F0F0

identify and convert tools are part of ImageMagick识别转换工具是 ImageMagick 的一部分

The following code use to fetch 50 members of an organization and check each avatarURL with the 2 checks above :以下代码使用获取组织的 50 名成员,并使用上面的 2 个检查检查每个avatarURL

package main
import (
    "github.com/google/go-github/github"
    "fmt"
    "context"
    "os/exec"
    "strconv"
    "strings"
)

func main() {
    client := github.NewClient(nil)
    opt := &github.ListMembersOptions{
        ListOptions: github.ListOptions{PerPage: 50},
    }
    members, _, err := client.Organizations.ListMembers(context.Background(), "IBM", opt)
    if err != nil {
        println(err.Error())
        return
    }
    max := 50
    for i := 0; i < max; i++ {
        identify_cmd := fmt.Sprintf("identify -format %%k %s",*members[i].AvatarURL)
        cmd := exec.Command("bash", "-c",identify_cmd)
        stdout, err := cmd.Output()
        if err != nil {
            println(err.Error())
        } else {
            color_unique, err := strconv.Atoi(string(stdout))
            if err != nil {
                println(err.Error())
            }
            //check that the thumbnail has exactly 2 unique colors
            if (color_unique == 2) {
                //check that the thumbnail has F0F0F0 among those 2 colors
                convert_cmd := fmt.Sprintf("convert %s -colors 2 -format \"%%c\" histogram:info: | cut -d'#' -f2 | cut -d' ' -f1",*members[i].AvatarURL)
                cmd := exec.Command("bash","-c",convert_cmd)
                stdout, err := cmd.Output()
                if err != nil {
                    println(err.Error())
                } else {
                    colors:=strings.Split(string(stdout),"\n")
                    has_color:=false
                    for i := 0; i < len(colors); i++ {
                        if (colors[i] == "F0F0F0") {
                            has_color = true
                            break
                        }
                    }
                    if (has_color) {
                        fmt.Printf("[%v/%v] user %v has not set her/his thumbnail image (%v)\n",i+1,max,*members[i].HTMLURL,*members[i].AvatarURL)
                    } else {
                        fmt.Printf("[%v/%v] the thumbnail for user %v has 2 unique color but none of them is F0F0F0 (%v)\n",i+1,max,*members[i].HTMLURL,*members[i].AvatarURL)
                    }
                }
            } else {
                fmt.Printf("[%v/%v] %v is not a default thumbnail\n",i+1,max,*members[i].AvatarURL)
            }
        }
    }
}

Note: In order to pass URL to the tools instead of regular files, I had to edit the file under /etc/ImageMagick-6/policy.xml commenting https policy but you can modify the code to download the avatar image yourself if needed注意:为了将 URL 传递给工具而不是常规文件,我必须编辑/etc/ImageMagick-6/policy.xml下的文件注释 https 策略,但如果需要,您可以修改代码以自己下载头像图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM