-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (100 loc) · 2.48 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
//"github.com/aws/aws-lambda-go/lambda"
"os"
"time"
"net/url"
"bitlyequisens/db"
)
type link struct {
UnitReference string `json:"unit_reference"`
LinkClicks []struct {
Date string `json:"date"`
Clicks int `json:"clicks"`
} `json:"link_clicks"`
Units int `json:"units"`
Unit string `json:"unit"`
}
type linkids struct {
Links []struct {
Id string `json:"id"`
} `json:"links"`
}
var (
Token = os.Getenv("BITLYTOKEN")
)
func HandleRequest() {
for _, linkid := range GetLinkIds(){
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api-ssl.bitly.com/v4/bitlinks/bit.ly/" + linkid + "/clicks?unit=month&units=1", nil) // Gets ALL links of any date
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer " + Token)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
links := link{}
err = json.Unmarshal(bodyText, &links)
if err != nil {
log.Panic("error:", err)
}
db.InsertDb(linkid, links.LinkClicks)
}
}
func GetOldestDate () string {
// Returns the date of two years ago from now
theTime := time.Now()
toAdd := -17280 * time.Hour
newTime := theTime.Add(toAdd)
log.Println("Oldest date to get links indexes is", newTime)
log.Println("Formated date is", newTime.Format("2006-01-02T15:04:05-0700"))
return url.QueryEscape(newTime.Format("2006-01-02T15:04:05-0700"))
}
func GetLinkIds() []string {
// Outputs the list of bitly link ID's
var listIds []string
ids := linkids{}
client := &http.Client{}
reqLink := "https://api-ssl.bitly.com/v4/groups/Bj156NPShb0/bitlinks/clicks?" + GetOldestDate()
log.Printf("link to get list of links id is %s", reqLink)
req, err := http.NewRequest("GET", reqLink , nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", "Bearer " + Token)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(bodyText, &ids)
if err != nil {
log.Panic("error:", err)
}
for _, id := range ids.Links{
item := strings.Split(id.Id, "/")
listIds = append(listIds, item[1])
}
log.Println("list of link id's", listIds)
return listIds
}
func main() {
//lambda.Start(HandleRequest)
HandleRequest()
}