/
advertisers.go
68 lines (54 loc) · 1.7 KB
/
advertisers.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
package profitshare
import (
"encoding/json"
"net/url"
"time"
)
// Commissions instance
type Commissions struct {
CommissionType string `json:"type"`
Value string `json:"value"`
}
// Advertiser instance
type Advertiser struct {
ID string `json:"id"`
Name string `json:"name"`
Logo string `json:"logo"`
Category string `json:"category"`
URL string `json:"url"`
LastUpdateProducts string `json:"last_update_products"`
Commissions []Commissions `json:"commissions"`
}
// GetAdvertisers returns a list of advertise from a period of time to another
func (ps *ProfitShare) GetAdvertisers(from time.Time, to time.Time) ([]Advertiser, error) {
dateFormat := "2006-01-02"
url, err := url.Parse("affiliate-advertisers")
if err != nil {
return []Advertiser{}, err
}
q := url.Query()
q.Add("date_from", from.Format(dateFormat))
q.Add("date_to", to.Format(dateFormat))
url.RawQuery = q.Encode()
body, err := ps.Get(url.String())
if err != nil {
return []Advertiser{}, err
}
var rez map[string]map[string]Advertiser
_ = json.Unmarshal(body, &rez)
var ret []Advertiser
for _, item := range rez["result"] {
ret = append(ret, item)
}
return ret, nil
}
// GetAdvertisers1M returns a list of advertisers from a month ago to now
func (ps *ProfitShare) GetAdvertisers1M() ([]Advertiser, error) {
now := time.Now()
return ps.GetAdvertisers(now.AddDate(0, -1, 0), now)
}
// GetAdvertisers1D returns a list of advertisers from a day ago to now
func (ps *ProfitShare) GetAdvertisers1D() ([]Advertiser, error) {
now := time.Now()
return ps.GetAdvertisers(now.AddDate(0, 0, -1), now)
}