微博图片URL获取上传用户golang版

CODE

package main

import (
    "flag"
    "fmt"
    "net/url"
    "regexp"
)

var (
    partern = regexp.MustCompile("[[:alnum:]]{32}")
)

// * example url: https://wx1.sinaimg.cn/orj360/002S1Vdwgy1gudlaxizffj60j60j60tf02.jpg
func main() {
    flag.Parse()
    imgUrl := flag.Arg(0)

    if len(imgUrl) < 24 {
        fmt.Println("invalid weibo image url.", imgUrl)
        return
    }

    uid, err := getUid(imgUrl)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    fmt.Printf("https://weibo.com/u/%d\n", uid)
}

func getUid(imgUrl string) (uid int, err error) {
    var carry = 16
    URL, err := url.Parse(imgUrl)
    if err != nil {
        return
    }

    hash := partern.FindString(URL.EscapedPath())[:8]

    if hash[0] == '0' && hash[1] == '0' {
        carry = 62
    }

    for _, b := range hash {
        uid = uid*carry + idx(b)
    }

    return
}

func idx(c rune) int {
    i := int(c)
    if i >= 48 && i <= 57 {
        return i - 48
    }

    if c >= 97 && c <= 122 {
        return i - 97 + 10
    }
    return i - 65 + 36
}

仅有一条评论

添加新评论