Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.49 KB | None | 0 0
  1. package client
  2.  
  3. import (
  4.     "bytes"
  5.     "fmt"
  6.     "golang.org/x/net/html"
  7.     "net/http"
  8.     "net/http/cookiejar"
  9.     "net/url"
  10. )
  11.  
  12. const baseUrl = "https://www.okcupid.com"
  13.  
  14. type OKCupidClient struct {
  15.     username string
  16.     password string
  17.     client   *http.Client
  18. }
  19.  
  20. func New(username, password string) *OKCupidClient {
  21.     cookieJar, _ := cookiejar.New(nil)
  22.     client := &http.Client{
  23.         Jar: cookieJar,
  24.     }
  25.     return &OKCupidClient{username: username, password: password, client: client}
  26. }
  27.  
  28. func (okc *OKCupidClient) DoLogin() {
  29.  
  30.     loginUrl := baseUrl + "/login"
  31.  
  32.     data := url.Values{}
  33.     data.Add("username", okc.username)
  34.     data.Add("password", okc.password)
  35.  
  36.     request, _ := http.NewRequest("POST", loginUrl, bytes.NewBufferString(data.Encode()))
  37.     request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  38.  
  39.     okc.client.Do(request)
  40. }
  41.  
  42. func (okc *OKCupidClient) FetchProfile(username string) {
  43.     profileUrl := baseUrl + "/profile/" + username
  44.     response, _ := okc.client.Get(profileUrl)
  45.     z := html.NewTokenizer(response.Body)
  46.     depth := 0
  47.     for {
  48.         tt := z.Next()
  49.         switch tt {
  50.         case html.ErrorToken:
  51.             return
  52.         case html.TextToken:
  53.             if depth > 0 {
  54.                 fmt.Println(string(z.Text()))
  55.             }
  56.         case html.StartTagToken, html.EndTagToken:
  57.             _, val, _ := z.TagAttr()
  58.             tn, _ := z.TagName()
  59.             if string(val) == "essays2015-essay-content" {
  60.                 if tt == html.StartTagToken {
  61.                     depth++
  62.                 }
  63.             } else {
  64.                 if depth > 0 && string(tn) == "div" {
  65.                     depth--
  66.                 }
  67.             }
  68.         }
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement