Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client
- import (
- "bytes"
- "fmt"
- "golang.org/x/net/html"
- "net/http"
- "net/http/cookiejar"
- "net/url"
- )
- const baseUrl = "https://www.okcupid.com"
- type OKCupidClient struct {
- username string
- password string
- client *http.Client
- }
- func New(username, password string) *OKCupidClient {
- cookieJar, _ := cookiejar.New(nil)
- client := &http.Client{
- Jar: cookieJar,
- }
- return &OKCupidClient{username: username, password: password, client: client}
- }
- func (okc *OKCupidClient) DoLogin() {
- loginUrl := baseUrl + "/login"
- data := url.Values{}
- data.Add("username", okc.username)
- data.Add("password", okc.password)
- request, _ := http.NewRequest("POST", loginUrl, bytes.NewBufferString(data.Encode()))
- request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- okc.client.Do(request)
- }
- func (okc *OKCupidClient) FetchProfile(username string) {
- profileUrl := baseUrl + "/profile/" + username
- response, _ := okc.client.Get(profileUrl)
- z := html.NewTokenizer(response.Body)
- depth := 0
- for {
- tt := z.Next()
- switch tt {
- case html.ErrorToken:
- return
- case html.TextToken:
- if depth > 0 {
- fmt.Println(string(z.Text()))
- }
- case html.StartTagToken, html.EndTagToken:
- _, val, _ := z.TagAttr()
- tn, _ := z.TagName()
- if string(val) == "essays2015-essay-content" {
- if tt == html.StartTagToken {
- depth++
- }
- } else {
- if depth > 0 && string(tn) == "div" {
- depth--
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement