-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlottery.go
58 lines (47 loc) · 1.11 KB
/
lottery.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
package lottery
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
const api = "http://api.elpais.com/ws/LoteriaNavidadPremiados"
func CheckNumbers(numbers ...int) (res []SearchResponse, err error) {
for _, n := range numbers {
ns := strconv.Itoa(n)
u := fmt.Sprintf("%s?n=%s", api, ns)
resp, err := http.Get(u)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
elems := strings.Split(string(body), "=")
if len(elems) != 2 || elems[0] != "busqueda" {
return nil, fmt.Errorf("unknown response format: %s", body)
}
sr, err := decodeSearchResponse(elems[1])
if err != nil {
return nil, err
}
res = append(res, sr)
}
return res, nil
}
type SearchResponse struct {
Num int `json:"numero"`
Prize int `json:"premio"`
Timestamp int `json:"timestamp"`
Status int `json:"status"`
Error int `json:"error"`
}
func decodeSearchResponse(j string) (SearchResponse, error) {
var res SearchResponse
err := json.Unmarshal([]byte(j), &res)
return res, err
}