-
Notifications
You must be signed in to change notification settings - Fork 2
/
lotto-scraper-ph.go
92 lines (86 loc) · 2.45 KB
/
lotto-scraper-ph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
)
type LottoResults struct {
Results []LottoItem
}
type LottoItem struct {
Game string
Combination string
Date string
Jackpot string
Winners string
}
const CLR_0 = "\x1b[39;1m"
const CLR_R = "\x1b[31;1m"
const CLR_G = "\x1b[32;1m"
const CLR_Y = "\x1b[33;1m"
const CLR_B = "\x1b[34;1m"
const CLR_M = "\x1b[35;1m"
const CLR_C = "\x1b[36;1m"
const CLR_W = "\x1b[37;1m"
const CLR_X = "\x1b[38;1m"
const CLR_N = "\x1b[0m"
func main() {
wg := &sync.WaitGroup{}
if _, err := os.Stat("results"); os.IsNotExist(err) {
os.Mkdir("."+string(filepath.Separator)+"results", 0777)
}
runtime.GOMAXPROCS(4)
wg.Add(8)
go StartScrape("6-55results.asp", CLR_0, "results/6-55.json", wg)
go StartScrape("6-49results.asp", CLR_R, "results/6-49.json", wg)
go StartScrape("6-45results.asp", CLR_G, "results/6-45.json", wg)
go StartScrape("6-42results.asp", CLR_Y, "results/6-42.json", wg)
go StartScrape("6-dresults.asp", CLR_B, "results/6-d.json", wg)
go StartScrape("4-dresults.asp", CLR_M, "results/4-d.json", wg)
go StartScrape("3-dresults.asp", CLR_C, "results/3-d.json", wg)
go StartScrape("2-dresults.asp", CLR_W, "results/2-d.json", wg)
wg.Wait()
}
func StartScrape(path string, color string, filename string, wg *sync.WaitGroup) {
fmt.Println(color + "Creating " + filename + CLR_N)
var item = LottoItem{Game: "", Combination: "", Date: "", Jackpot: "", Winners: ""}
var results = LottoResults{}
doc, queryError := goquery.NewDocument("http://pcso-lotto-results-and-statistics.webnatin.com/" + path)
if queryError != nil {
fmt.Println("\nError fetching data..\n" + queryError.Error())
return
}
insertedCounter := 0
doc.Find("table tr").Each(func(i int, s *goquery.Selection) {
s.Find("td").Each(func(j int, t *goquery.Selection) {
if j == 0 {
item.Game = t.Text()
}
if j == 2 {
item.Date = t.Text()
}
if j == 3 {
item.Combination = t.Text()
}
if j == 4 {
item.Jackpot = t.Text()
}
if j == 5 {
item.Winners = t.Text()
}
})
results.Results = append(results.Results, item)
fmt.Print(color + "●" + CLR_N)
insertedCounter++
})
jsonResults, _ := json.Marshal(results)
ioutil.WriteFile(filename, []byte(jsonResults), 0644)
fmt.Println(color + "\nCreated json file :" + filename + " -> " + strconv.Itoa(insertedCounter) + " records " + CLR_N)
wg.Done()
}