您的位置:首页 > 编程语言 > Go语言

博客园刷星golang v0.1

2015-09-16 10:29 393 查看
最近爽了把golang,写个刷星代码练练手。

程序入口:

func main() {    for i := 1; i < 20; i++ {        ing := getLastIng()        if strings.Contains(ing, "幸运闪") {            insertIng(i)        } else {            deleteIng(ing)        }        time.Sleep(15 * time.Minute)    }}

检查最新的一条闪存状态:

func getLastIng() (text string) {    client := &http.Client{}    req, _ := http.NewRequest("GET", "http://ing.cnblogs.com/ajax/ing/GetIngList?IngListType=my&PageIndex=1&PageSize=1&Tag=&_=1441948524646", nil)    req.Header.Set("Content-Type", "application/json; charset=UTF-8")    req.Header.Set("Cookie", cookie)    resp, err := client.Do(req)    defer resp.Body.Close()    body, err := ioutil.ReadAll(resp.Body)    if err != nil {        // handle error    }    return string(body)}

插入一条新的闪存:

ing := Ing{}    ing.PublicFlag = 1    rand.Seed(time.Now().Unix())    ing.Content = "mm" + strconv.Itoa(rand.Intn(100))    text, _ := json.Marshal(ing)    client := &http.Client{}    req, err := http.NewRequest("POST", "http://ing.cnblogs.com/ajax/ing/Publish", bytes.NewReader(text))    if err != nil {    }    req.Header.Set("Content-Type", "application/json; charset=UTF-8")    req.Header.Set("Cookie", cookie)    resp, err := client.Do(req)    defer resp.Body.Close()    body, _ := ioutil.ReadAll(resp.Body)    fmt.Println(string(body))

删除没有star的闪存:

func deleteIng(ing string) {    begin := strings.Index(ing, "DelIng(")    l := len("DelIng(")    ingId := ing[begin+l : begin+l+6]    fmt.Println("{ingId:" + ingId + "}")    client := &http.Client{}    req, _ := http.NewRequest("POST", "http://ing.cnblogs.com/ajax/ing/del", strings.NewReader("{ingId:"+ingId+"}"))    req.Header.Set("Content-Type", "application/json; charset=UTF-8")    req.Header.Set("Cookie", cookie)    resp, err := client.Do(req)    defer resp.Body.Close()    body, err := ioutil.ReadAll(resp.Body)    if err != nil {        fmt.Println(err)    }    fmt.Println(string(body))}

就酱紫!以后心情好再改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: