Go请求网络并POST json结果到接口

cooolr 于 2020-12-18 发布
package main

import "os"
import "fmt"
import "time"
import "bytes"
import "encoding/json"
import "strconv"
import "net/http"
import "io/ioutil"
import "github.com/tidwall/gjson"

var print = fmt.Println
type Body struct {
    Body string
}

func get() string {
    //timestamp := strconv.FormatInt(time.Now().UnixNano()-1000, 10)
    //ltime,err := strconv.Atoi(timestamp[:10])
    //if err != nil {print(err)}
    //ltime1 := strconv.Itoa(ltime-77)
    //ltime2 := strconv.Itoa(ltime-49)
    //ltime3 := timestamp[:10]
    //rticket := timestamp[:13]
    //url := "http://ib-lq.snssdk.com/api/news/feed/v88/?list_count=7&support_rn=0&category=news_world&concern_id=6215497896255556098&refer=1&refresh_reason=1&session_refresh_idx=6&count=20&min_behot_time="+ltime1+"&last_refresh_sub_entrance_interval="+ltime2+"&loc_mode=7&tt_from=pull&plugin_enable=3&sati_extra_params=%7B%22last_click_item_list%22%3A%5B%5D%7D&iid=2972723731498317&device_id=49139426659&ac=wifi&channel=goapk3&aid=13&app_name=news_article&version_code=716&version_name=7.1.6&device_platform=android&ab_version=1859937%2C662099%2C668774%2C2033166%2C2001178%2C668775%2C2063718%2C2247462%2C1965373%2C668779%2C660830%2C662176%2C1877264&ab_group=100167&ab_feature=94563%2C102749&ssmix=a&device_type=MI+5s+Plus&device_brand=Xiaomi&language=zh&os_api=24&os_version=7.0&uuid=861414035938894&openudid=52d4d74cff948fbe&manifest_version_code=716&resolution=1080*1920&dpi=440&update_version_code=71611&_rticket="+rticket+"&plugin=26958&rom_version=miui_v9_v9.6.1.0.nbgcnfd&ts="+ltime3+"&as=a2856e4d74bd7f52b24755&mas=00c89fdc094798ac7a0b5903c9499929dd8e8a20e0266a6eb3&cp=5ef2de2fe62d4q1"
    // url := "http://ic.snssdk.com/api/news/feed/v47/?category=news_world&refer=1&refresh_reason=1&count=20&min_behot_time="+ltime1+"&%20last_refresh_sub_entrance_interval="+ltime2+"&loc_mode=6&loc_time=1607508135&latitude=23.03649&longitude=113.312399&city=%E5%B9%BF%E5%B7%9E%E5%B8%82&tt_from=pull&cp=56f795a2893d9q1&plugin%20_enable=4&client_extra_params=%7B%22ad_download%22%3A%7B%22space_unoccupied%22%3A104704320%2C%22space_cleanable%22%3A0%7D%2C%22last_ad_position%22%3A-1%2C%22playparam%22%3A%22codec_type%3%20A0%22%7D&last_ad_show_interval=-1&cached_item_num=0&iid=2040322739672910&device_id=49139426659&ac=wifi&mac_address=78%3A02%3AF8%3A35%3AD2%3A50&channel=juyouliang_lite_and5&aid=35&app_name%20=news_article_lite&version_code=755&version_name=7.5.5&device_platform=android&ab_version=668906%2C668908%2C668903%2C668904%2C668907%2C668905%2C1859936&ab_client=a1%2Ce1%2Cf2%2Cg2%2Cf7&ab%20_feature=z1&abflag=3&ssmix=a&device_type=MI+5s+Plus&device_brand=Xiaomi&language=zh&os_api=23&os_version=6.0.1&uuid=861414035938894&openudid=fe5d529685a9efb6&manifest_version_code=7550&re%20solution=1080*1920&dpi=440&update_version_code=75503&_rticket="+rticket+"&plugin_state=8715907089&sa_enable=0&rom_version=miui_v8_v8.5.6.0.mbgcned&cdid=d4a8ac78-6d9a-4413-aa7b-6e2df90bd%20691"
    url := "http://ib-lq.snssdk.com/api/news/feed/v88/?list_count=7&support_rn=0&category=news_world&concern_id=6215497896255556098&refer=1&refresh_reason=1&session_refresh_idx=6&count=20&min_behot_time=1607655788&last_refresh_sub_entrance_interval=1607656148&loc_mode=7&tt_from=pull&plugin_enable=3&sati_extra_params=%7B%22last_click_item_list%22%3A%5B%5D%7D&iid=2972723731498317&device_id=49139426659&ac=wifi&channel=goapk3&aid=13&app_name=news_article&version_code=716&version_name=7.1.6&device_platform=android&ab_version=1859937%2C662099%2C668774%2C2033166%2C2001178%2C668775%2C2063718%2C2247462%2C1965373%2C668779%2C660830%2C662176%2C1877264&ab_group=100167&ab_feature=94563%2C102749&ssmix=a&device_type=MI+5s+Plus&device_brand=Xiaomi&language=zh&os_api=24&os_version=7.0&uuid=861414035938894&openudid=52d4d74cff948fbe&manifest_version_code=716&resolution=1080*1920&dpi=440&update_version_code=71611&_rticket=1607656148591&plugin=26958&rom_version=miui_v9_v9.6.1.0.nbgcnfd&ts=1607656148&as=a2856e4d74bd7f52b24755&mas=00c89fdc094798ac7a0b5903c9499929dd8e8a20e0266a6eb3&cp=5ef2de2fe62d4q1"

    client := &http.Client{}
    req,_ := http.NewRequest("GET", url, nil)
    // req.Header.Add("Accept-Encoding", "gzip")
    req.Header.Add("X-SS-QUERIES", "dGMCAb6ot3awALq2K%2F3edydVVQre4IoZCowOX8iipaluXQSusMJstGtA9PquYM4AB2XVVQQewkC9INznWRbed8Z9PiQKeBJ22shGDvT3WkgtllWOPuKInQpnElWurNDPqWvVP96ml8zlNHDXK%2BiuQlyhPFU%3D")
    req.Header.Add("X-SS-REQ-TICKET", "1607656148597")
    req.Header.Add("sdk-version", "1")
    req.Header.Add("Cookie", "install_id=2972723731498317; ttreq=1$00d3394e53a723fb5b9e7b4e6d7ac261a79acb4d; odin_tt=9ae379dcc99d43bfd367e2d72d805cb73c27a5608b9537a89fdcff4dc0935a4d96fd6b4638a2c00a17a196529280937a; qh[360]=1")
    req.Header.Add("X-Gorgon", "0339fe56000066480ae03043958cef4c166ee68f1fea86f93c8c")
    req.Header.Add("X-Khronos", "1607656148")
    req.Header.Add("X-Pods", "")
    req.Header.Add("Host", "ib-lq.snssdk.com")
    req.Header.Add("Connection", "Keep-Alive")
    req.Header.Add("User-Agent", "okhttp/3.10.0.1")
    req.Host = "ib-lq.snssdk.com"

    resp,err := client.Do(req)
    // resp,err := http.Get(url)
    if err != nil {
        print(err)
        return ""
    }
    defer resp.Body.Close()
    body,err := ioutil.ReadAll(resp.Body)
    return string(body)

}

func post(body string) string {
    url := "http://111.230.24.37:10000/redis"
    var jsonstr = []byte(body)
    buffer:= bytes.NewBuffer(jsonstr)
    client := &http.Client{}
    req,_ := http.NewRequest("POST", url, buffer)
    req.Header.Add("Content-Type", "application/json")

    resp,err := client.Do(req)
    if err != nil {
        print(err)
        return ""
    }
    defer resp.Body.Close()
    res,_ := ioutil.ReadAll(resp.Body)
    return string(res)
}

func run() {
    data := get()
    local,_ := time.LoadLocation("Asia/Shanghai")
    now := time.Now().In(local).Format("2006-01-02 15:04:05")
    var body Body
    body.Body = data
    jsonBytes,_ := json.Marshal(body)
    body_string := string(jsonBytes)
    res := post(body_string)
    print(now,len(gjson.Get(data,"data").Array()),res)
}

func main() {
    duration := 1
    if c:=len(os.Args);c>1 {
        duration,_ = strconv.Atoi(os.Args[1])
    }
    for {
	run()
        time.Sleep(time.Duration(duration*60) * time.Second)
    }
}