-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkudago_client.go
88 lines (77 loc) · 2.13 KB
/
kudago_client.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"time"
)
type Event struct {
Title string
Place string
Location string
Site string
}
type resultResponse struct {
Results []eventResponse `json:"results"`
}
type eventResponse struct {
Title string `json:"title"`
Place placeResponse `json:"place"`
Location locationResponse `json:"location"`
Site string `json:"site_url"`
}
type locationResponse struct {
Slug string `json:"slug"`
}
type placeResponse struct {
ID int `json:"id"`
}
func NewKudaGoClient(url string) KudaGoClient {
return &kudago{
Url: url,
}
}
type KudaGoClient interface {
GetRandomEvent(city string, user string) (*Event, error)
GetEventForToday(city string, user string) (*Event, error)
GetEventForTomorrow(city string, user string) (*Event, error)
}
type kudago struct {
Url string
}
func (k *kudago) GetRandomEvent(city string, user string) (*Event, error) {
return k.getEvent(city, user, time.Now().Unix(), time.Now().AddDate(0, 6, 0).Unix())
}
func (k *kudago) GetEventForToday(city string, user string) (*Event, error) {
return k.getEvent(city, user, time.Now().Unix(), time.Now().AddDate(0, 0, 1).Unix())
}
func (k *kudago) GetEventForTomorrow(city string, user string) (*Event, error) {
return k.getEvent(city, user, time.Now().AddDate(0, 0, 1).Unix(), time.Now().AddDate(0, 0, 2).Unix())
}
func (k *kudago) getEvent(city string, user string, from int64, until int64) (*Event, error) {
url := fmt.Sprintf("%s/events?fields=location,title,place,site_url&location=%s&actual_since=%d&actual_until=%d&page_size=100", k.Url, city, from, until)
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
responseData, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var resultResp resultResponse
err = json.Unmarshal(responseData, &resultResp)
if err != nil {
return nil, err
}
event := resultResp.Results[rand.Intn(len(resultResp.Results))]
e := Event{
Title: event.Title,
Location: event.Location.Slug,
Place: string(event.Place.ID),
Site: event.Site,
}
return &e, nil
}