Last active
December 2, 2019 20:49
-
-
Save yinheli/8475218 to your computer and use it in GitHub Desktop.
领取 v2ex 每日奖励 golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"regexp" | |
"strings" | |
) | |
var loginURL = "https://www.v2ex.com/signin" | |
var missionURL = "https://www.v2ex.com/mission/daily" | |
var username = "your_username" | |
var password = "your_password" | |
type myCookieJar struct { | |
cookies []*http.Cookie | |
} | |
func (c *myCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) { | |
if c.cookies == nil { | |
c.cookies = make([]*http.Cookie, 0) | |
} | |
for _, it := range cookies { | |
c.cookies = append(c.cookies, it) | |
} | |
} | |
func (c *myCookieJar) Cookies(u *url.URL) []*http.Cookie { | |
return c.cookies | |
} | |
func main() { | |
cookieJar := &myCookieJar{} | |
client := http.Client{Jar: cookieJar} | |
// login | |
loginreq, _ := http.NewRequest("GET", loginURL, nil) | |
loginresp, _ := client.Do(loginreq) | |
loginHTML, _ := ioutil.ReadAll(loginresp.Body) | |
re := regexp.MustCompile(`<input type="hidden" value="(\d+)" name="once" />`) | |
matched := re.FindStringSubmatch(string(loginHTML)) | |
once := matched[1] | |
re = regexp.MustCompile(`<input type="text" class="sl" name="(.*)?" value="" autofocus="autofocus" autocorrect="off" spellcheck="false" autocapitalize="off" placeholder="用户名或电子邮箱地址" />`) | |
matched = re.FindStringSubmatch(string(loginHTML)) | |
loginUserNameKey := matched[1] | |
re = regexp.MustCompile(`<input type="password" class="sl" name="(.*)?" value="" autocorrect="off" spellcheck="false" autocapitalize="off" />`) | |
matched = re.FindStringSubmatch(string(loginHTML)) | |
loginPasswordKey := matched[1] | |
params := url.Values{"next": {"/", "/"}, loginUserNameKey: {username}, loginPasswordKey: {password}, "once": {once}} | |
req, _ := http.NewRequest("POST", loginURL, strings.NewReader(params.Encode())) | |
req.Header.Add("Host", "v2ex.com") | |
req.Header.Add("Origin", "https://v2ex.com") | |
req.Header.Add("Referer", loginURL) | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
for _, c := range cookieJar.cookies { | |
req.AddCookie(c) | |
} | |
client.Do(req) | |
// loginPostRsp, _ := client.Do(req) | |
// loginPostHTML, _ := ioutil.ReadAll(loginPostRsp.Body) | |
// try finish mission | |
missionReq, _ := http.NewRequest("GET", missionURL, nil) | |
missionResp, _ := client.Do(missionReq) | |
missionHTML, _ := ioutil.ReadAll(missionResp.Body) | |
missionHTMLStr := string(missionHTML) | |
if strings.Index(missionHTMLStr, "每日登录奖励已领取") > 0 { | |
fmt.Println("每日登录奖励已领取") | |
} else { | |
re = regexp.MustCompile(`location.href = '(.*)'`) | |
matched = re.FindStringSubmatch(missionHTMLStr) | |
req, _ = http.NewRequest("GET", "https://www.v2ex.com"+matched[1], nil) | |
client.Do(req) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment