-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1topsites.go
180 lines (150 loc) · 4.56 KB
/
1topsites.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
175
176
177
178
179
180
package main
import (
"encoding/xml"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/kataras/iris"
"github.com/smartystreets/go-aws-auth"
"github.com/zew/awis/mdl"
"github.com/zew/gorpx"
"github.com/zew/irisx"
"github.com/zew/logx"
"github.com/zew/util"
)
var awsSess *session.Session // not needed
func init() {
awsSess = session.New(&aws.Config{
Region: aws.String("us-west-2"),
Credentials: credentials.NewSharedCredentials("", "default"),
})
}
// Builds current ISO8601 timestamp.
// eg 2007-08-31T16:47:05.0000Z
func iso8601Timestamp() string {
t := time.Now()
ts := t.Format("2006-01-02T15:04:05")
// ts += ".0000Z"
ts += "Z"
// gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time())
// logx.Printf("ts is %v", ts)
return ts
}
func unixDayStamp() int {
ts := int(int32(time.Now().Unix()))
ts = int(ts/(24*3600))*24*3600 + 9*3600 // norm it towards a single day; 8 in the morning
return ts
}
func ParseIntoDomains(dat []byte) ([]mdl.Domain, error) {
type Result struct {
// Sites []Site `xml:"TopSitesResponse>Response>TopSitesResult>Alexa>TopSites>Country>Sites>Site"`
Sites []mdl.Domain `xml:"Response>TopSitesResult>Alexa>TopSites>Country>Sites>Site"` // omit the outmost tag name TopSitesResponse
}
result := Result{}
err := xml.Unmarshal(dat, &result)
if err != nil {
return nil, err
}
return result.Sites, nil
}
func topSites(c *iris.Context) {
var err error
reqSigned, _ := http.NewRequest("GET", Pref(), nil)
display := ""
errors := ""
respBytes := []byte{}
ts := unixDayStamp()
if irisx.EffectiveParam(c, "submit", "none") != "none" {
var ServiceHost1 = "ats.amazonaws.com"
myUrl := url.URL{}
myUrl.Host = ServiceHost1
myUrl.Scheme = "http"
// logx.Printf("host is %v", myUrl.String())
vals := map[string]string{
"Action": "TopSites",
"AWSAccessKeyId": util.EnvVar("AWS_ACCESS_KEY_ID"),
"SignatureMethod": "HmacSHA256",
"SignatureVersion": "2",
"Timestamp": iso8601Timestamp(),
// "Signature" : "will be added by awsauth.Sign2(req)"
"ResponseGroup": "Country",
"Url": irisx.EffectiveParam(c, "Url", "wwww.zew.de"),
"CountryCode": irisx.EffectiveParam(c, "CountryCode", "DE"),
"Start": irisx.EffectiveParam(c, "Start", "0"),
"Count": irisx.EffectiveParam(c, "Count", "5"),
}
queryStr := ""
for k, v := range vals {
queryStr += fmt.Sprintf("%v=%v&", k, v)
}
logx.Printf("queryStr is %v", queryStr)
strUrl := myUrl.String() + "/?" + queryStr
req, err := http.NewRequest("GET", strUrl, nil)
util.CheckErr(err)
// logx.Printf("req is %v", req)
// Explicit or implicit -
// At every rate - we need to call Sign2(),
// because awsauth does not know about awis
if false {
awsauth.Sign2(req, awsauth.Credentials{
AccessKeyID: util.EnvVar("AWS_ACCESS_KEY_ID"),
SecretAccessKey: util.EnvVar("AWS_SECRET_ACCESS_KEY"),
// SecurityToken: "Security Token", // STS (optional)
})
} else {
awsauth.Sign2(req)
}
reqSigned = req
resp, err := util.HttpClient().Do(reqSigned)
util.CheckErr(err)
defer resp.Body.Close()
respBytes, err = ioutil.ReadAll(resp.Body)
util.CheckErr(err)
// target := html.EscapeString(string(respBytes))
domains, err := ParseIntoDomains(respBytes)
if err != nil {
errors += fmt.Sprintf("xml parsing failded: %v\n", err)
}
for _, domain := range domains {
domain.LastUpdated = ts
err := gorpx.Db1Map().Insert(&domain)
if err != nil {
errors += fmt.Sprintf("domain: %v\n", err)
}
}
display += util.IndentedDump(domains)
display = errors + "\n\n" + display
}
s := struct {
HTMLTitle string
Title string
FlashMsg template.HTML
FormAction string
ParamUrl string
ParamStart string
ParamCount string
ParamCountryCode string
URL string
StructDump1 template.HTML
StructDump2 template.HTML
}{
HTMLTitle: AppName() + " top sites",
Title: AppName() + " top sites",
FlashMsg: template.HTML("Alexa Web Information Service"),
StructDump2: template.HTML(display),
URL: reqSigned.URL.String(),
FormAction: PathTopSites,
ParamUrl: irisx.EffectiveParam(c, "Url", "www.zew.de"),
ParamStart: irisx.EffectiveParam(c, "Start", "0"),
ParamCount: irisx.EffectiveParam(c, "Count", "5"),
ParamCountryCode: irisx.EffectiveParam(c, "CountryCode", "DE"),
}
err = c.Render("form.html", s)
util.CheckErr(err)
}