-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (103 loc) · 2.82 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"encoding/csv"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"net/http"
"os"
)
type Configuration struct {
ApiKey string `yaml:"apiKey"`
OutputFileName string `yaml:"outputFilename"`
NoDuplicates bool `yaml:"noDuplicates"`
Websites []string `yaml:"websites"`
//DateFrom string `yaml:"date_from"`
//DateTo string `yaml:"date_to"`
}
type ApiData struct {
Responses []Response
}
type Response struct {
SimilarSites []SimilarSite `yaml:"similar_sites"`
}
type SimilarSite struct {
Url string `yaml:"url"`
Score string `yaml:"score"`
}
const ConfigFileName = "config.yml"
const SimilarWebApiUrl = "https://api.similarweb.com/v1/website/%s/similar-sites/similarsites?api_key=%s"
var configuration Configuration
func main() {
responses := request()
if responses == nil {
log.Fatal("API response is empty!")
}
generateFile(responses)
log.Printf("Done! Results are in %s file. Just press Enter to close the window", configuration.OutputFileName)
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func request() (responses []*http.Response) {
yamlFile, err := ioutil.ReadFile(ConfigFileName)
if err != nil {
log.Printf("No configuration file with name %s provided!", ConfigFileName)
}
err = yaml.Unmarshal(yamlFile, &configuration)
if err != nil {
log.Fatalf("Cannot read configuration file: %v", err)
}
key := configuration.ApiKey
for _, website := range configuration.Websites {
log.Printf("Getting similar websites for %s...", website)
url := fmt.Sprintf(SimilarWebApiUrl, website, key)
response, err := http.Get(url)
if err != nil {
log.Printf("API request error: %s", err)
continue
}
responses = append(responses, response)
log.Printf("Done getting similar websites for %s", website)
}
return
}
func generateFile(responses []*http.Response) {
file, err := os.Create(configuration.OutputFileName)
if err != nil {
log.Fatalf("Cannot create %s file", configuration.OutputFileName)
}
defer file.Close()
var writeData [][]string
for _, response := range responses {
var data = Response{}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal("API request error: ", err)
}
err = yaml.Unmarshal(body, &data)
if err != nil {
log.Fatalf("Cannot parse API data: %v", err)
}
for _, similarSite := range data.SimilarSites {
writeData = append(writeData, []string{similarSite.Url, similarSite.Score})
}
}
if len(writeData) > 0 {
file, err := os.Create(configuration.OutputFileName)
if err != nil {
log.Fatal("Cannot create file", err)
}
defer file.Close()
w := csv.NewWriter(file)
w.Comma = ';'
defer w.Flush()
w.WriteAll(writeData)
if err := w.Error(); err != nil {
log.Fatalln("Error writing csv:", err)
}
} else {
log.Fatal("No data for writing!")
}
}