-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (135 loc) · 2.83 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
)
type Record struct {
Advertiser string
AccountID string
Relationship string
AuthorityID string
Original string
Keep bool
}
func (r *Record) UniqueID() string {
return r.Advertiser + r.AccountID + r.Relationship
}
func (r *Record) Row() string {
if r.Keep {
return r.Original
}
c := []string{
r.Advertiser,
r.AccountID,
r.Relationship,
}
if r.AuthorityID != "" {
c = append(c, r.AuthorityID)
}
return strings.Join(c, ",")
}
func ParseRow(row string) (*Record, error) {
row = strings.TrimSpace(row)
re := regexp.MustCompile("\\s*#.+$")
cols := strings.Split(re.ReplaceAllString(row, ""), ",")
if len(cols) < 3 || len(cols) > 4 {
return nil, fmt.Errorf("Failed parsing line")
}
r := &Record{
Advertiser: strings.TrimSpace(strings.ToLower(cols[0])),
AccountID: strings.TrimSpace(cols[1]),
Relationship: strings.TrimSpace(cols[2]),
Original: row,
}
if len(cols) == 4 {
r.AuthorityID = strings.TrimSpace(cols[3])
}
return r, nil
}
func main() {
// load authority ids
authorities := make(map[string]string)
authf, err := os.Open("authorities.csv")
if err != nil {
log.Fatal(err)
}
defer authf.Close()
auth := csv.NewReader(authf)
for {
record, err := auth.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
authorities[record[0]] = record[1]
}
// prepare file header
header := []string{
fmt.Sprintf("# ads.txt:%s", time.Now().Format(time.RFC3339)),
"contact=sales@wasdmedia.de",
"contact=https://wasdmedia.de/",
"# ---",
}
// load and parse ads.txt parts
dir := "./parts"
rows := make(map[string]*Record)
log.SetOutput(os.Stderr)
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
f, err := os.Open(filepath.Join(dir, file.Name()))
// dailymotion's validator expects identical lines *rolleyes*
keep := file.Name() == "10_dailymotion.txt"
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
row := strings.TrimSpace(scanner.Text())
if row[0] == '#' {
if strings.HasPrefix(row, "#dailymotion") {
header = append(header, "# dailymotion ads.txt version:")
header = append(header, row)
}
continue
}
r, err := ParseRow(row)
if err != nil {
log.Printf("%s: %s\n", err, scanner.Text())
continue
}
if id, ok := authorities[r.Advertiser]; ok {
r.AuthorityID = id
}
r.Keep = keep
rows[r.UniqueID()] = r
}
f.Close()
}
out := []string{}
for _, r := range rows {
out = append(out, r.Row())
}
sort.Strings(out)
for _, h := range header {
fmt.Println(h)
}
fmt.Println("# ---")
for _, o := range out {
fmt.Println(o)
}
}