美文网首页
批量获取手机号归属地

批量获取手机号归属地

作者: pingwazi | 来源:发表于2021-08-13 13:35 被阅读0次
package main

import (
    "bufio"
    "crypto/tls"
    "encoding/json"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "path/filepath"
    "strings"
    "time"
)

type RespData struct {
    Mobile string
    Msg    string
    Meta   struct {
        Result string `json:result`
    } `json:meta`
    Data struct {
        Area     string `json:area`
        Operator string `json:operator`
    } `json:data`
}

var (
    client *http.Client
)

func init() {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} //忽略https的证书检查
    client = &http.Client{Transport: tr}
}

func main() {
    log.Println("请输入含有手机号的文本文件路径(最终结果也将放在此目录中)!当手机号较多时将会消耗较多系统资源")
    stdInput := bufio.NewReader(os.Stdin)
    filePath, _ := stdInput.ReadString('\n')
    filePath = strings.ReplaceAll(filePath, "\n", "")
    filePath = strings.ReplaceAll(filePath, "\r", "")
    filePath = strings.Trim(filePath, " ")
    if filePath == "" {
        log.Fatal("您未输入文件路径!告辞")
    }
    mobiles := ReadMobilesFromFile(filePath)
    mobileAreaChan := make(chan *RespData, len(mobiles))
    for _, mobile := range mobiles {
        go MobileAreaInfo(mobileAreaChan, mobile)
    }
    fileDir := filepath.Dir(filePath)
    currentTime := time.Now()
    mobileAreaFile := filepath.Join(fileDir, currentTime.Format("2006-01-02"))
    os.Remove(mobileAreaFile)
    fileObj, err := os.Create(mobileAreaFile)
    if err != nil {
        log.Fatal(err)
    }
    defer fileObj.Close()
    writer := bufio.NewWriter(fileObj)
    for i := 0; i < len(mobiles); i++ {
        data := <-mobileAreaChan
        line := fmt.Sprintf("手机号=%s 归属地=%s 运营商=%s 错误信息=%s\n", data.Mobile, data.Data.Area, data.Data.Operator, data.Msg)
        fmt.Println(line)
        writer.WriteString(line)
    }
    writer.Flush()
}

//从文件中读取手机号
func ReadMobilesFromFile(fileName string) []string {
    file, openErr := os.Open(fileName)
    if openErr != nil {
        log.Fatal(openErr)
    }
    defer file.Close()
    ret := []string{}
    rd := bufio.NewReader(file)
    for {
        line, readErr := rd.ReadString('\n')
        if readErr != nil && readErr == io.EOF {
            break
        } else if readErr != nil {
            log.Fatal(readErr)
        }
        line = strings.Replace(line, "\n", "", -1)
        line = strings.Replace(line, "\r", "", -1)
        ret = append(ret, line)
    }
    return ret
}

//手机号归属地信息
func MobileAreaInfo(areaChan chan *RespData, mobile string) {
    var respData *RespData = new(RespData)
    respData.Mobile = mobile
    req, err := http.NewRequest("GET", fmt.Sprintf("https://www.baifubao.com/callback?cmd=1059&phone=%s", mobile), nil)
    if err != nil {
        respData.Msg = err.Error()
        areaChan <- respData
        return
    }
    resp, err := client.Do(req)

    if err != nil {
        respData.Msg = err.Error()
        areaChan <- respData
        return
    }
    defer resp.Body.Close()
    if resp.StatusCode != http.StatusOK {
        respData.Msg = fmt.Sprintf("响应状态码%d错误", resp.StatusCode)
        areaChan <- respData
        return
    }

    bodyData, _ := ioutil.ReadAll(resp.Body)
    bodyStr := string(bodyData)
    bodyStr = strings.Replace(bodyStr, "/*fgg_again*/(", "", -1)
    bodyStr = strings.TrimSuffix(bodyStr, ")")
    json.Unmarshal([]byte(bodyStr), respData)
    areaChan <- respData
}

相关文章

网友评论

      本文标题:批量获取手机号归属地

      本文链接:https://www.haomeiwen.com/subject/bpidbltx.html