forked from AkaAny/hduhelp-tweet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea_code.go
43 lines (38 loc) · 826 Bytes
/
area_code.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
package prisk
import (
"encoding/json"
"fmt"
"github.com/robertkrimen/otto"
"io/ioutil"
)
func (pr PRisk) GetAreaList() []Area {
var request = pr.GetRequest()
var url = "http://bmfw.www.gov.cn/myqfxdjcx/source/WAP/js/area.js"
resp, body, errs := request.Get(url).End()
fmt.Println(resp.StatusCode)
if errs != nil {
panic(errs[0])
}
err := ioutil.WriteFile("area.js", []byte(body), 0644)
if err != nil {
panic(err)
}
rawJson := mustParseArea(body)
var areas []Area
err = json.Unmarshal([]byte(rawJson), &areas)
return areas
}
func mustParseArea(areaJS string) string {
vm := otto.New()
_, err := vm.Run(string(areaJS))
if err != nil {
panic(err)
}
value, err := vm.Eval("JSON.stringify(area)")
if err != nil {
panic(err)
}
var result = value.String()
fmt.Println(result)
return result
}