|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/xml" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"time" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"os" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// URLSet is root for site mite |
|
12
|
|
|
type URLSet struct { |
|
13
|
|
|
XMLName xml.Name `xml:"urlset"` |
|
14
|
|
|
XMLNs string `xml:"xmlns,attr"` |
|
15
|
|
|
URL []URL `xml:"url"` |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
// URL is for every single location url |
|
19
|
|
|
type URL struct { |
|
20
|
|
|
Loc string `xml:"loc"` |
|
21
|
|
|
LastMod string `xml:"lastmod,omitempty"` |
|
22
|
|
|
ChangeFreq string `xml:"changefreq,omitempty"` |
|
23
|
|
|
Priority float32 `xml:"priority,omitempty"` |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
func (us *URLSet) saveToFile(filename string) error { |
|
27
|
|
|
m, err := xml.Marshal((*us)) |
|
28
|
|
|
if err != nil { |
|
29
|
|
|
return err |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777) |
|
33
|
|
|
file.Write([]byte(xml.Header)) |
|
34
|
|
|
file.Write(m) |
|
35
|
|
|
file.Close() |
|
36
|
|
|
return err |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
func (us *URLSet) validate() URLSet { |
|
40
|
|
|
client := &http.Client{ |
|
41
|
|
|
Timeout: 10*time.Second, |
|
42
|
|
|
} |
|
43
|
|
|
c := make(chan string, 20) |
|
44
|
|
|
|
|
45
|
|
|
validURLs := []URL{} |
|
46
|
|
|
for _, url := range (*us).URL { |
|
47
|
|
|
go func(url URL, c chan string) { |
|
48
|
|
|
resp, err := client.Get(url.Loc) |
|
49
|
|
|
defer func() { <-c }() |
|
50
|
|
|
if err != nil { |
|
51
|
|
|
c <- err.Error() |
|
52
|
|
|
return |
|
53
|
|
|
} |
|
54
|
|
|
c <- fmt.Sprintf("Response code is %d for %s", resp.StatusCode, url.Loc) |
|
55
|
|
|
if resp.StatusCode == 200 { |
|
56
|
|
|
validURLs = append(validURLs, url) |
|
57
|
|
|
} |
|
58
|
|
|
}(url, c) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
for range us.URL { |
|
62
|
|
|
fmt.Println(<-c) |
|
63
|
|
|
} |
|
64
|
|
|
newURLSet := URLSet{ |
|
65
|
|
|
XMLNs: us.XMLNs, |
|
66
|
|
|
} |
|
67
|
|
|
for _, url := range validURLs { |
|
68
|
|
|
newURLSet.URL = append(newURLSet.URL, url) |
|
69
|
|
|
} |
|
70
|
|
|
return newURLSet |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
func newURLSetFromXML(rawXMLData []byte) URLSet { |
|
74
|
|
|
us := URLSet{} |
|
75
|
|
|
|
|
76
|
|
|
err := xml.Unmarshal(rawXMLData, &us) |
|
77
|
|
|
|
|
78
|
|
|
if err != nil { |
|
79
|
|
|
fmt.Printf("Sitemap cannot parsed. Because: %s", err) |
|
80
|
|
|
return URLSet{} |
|
81
|
|
|
} |
|
82
|
|
|
return us |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
func singleProcess(uri string, filename string) { |
|
86
|
|
|
client := &http.Client{ |
|
87
|
|
|
Timeout: 10*time.Second, |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if Verbose {fmt.Printf("Single process started for %s\n",filename)} |
|
91
|
|
|
resp, err := client.Get(uri) |
|
92
|
|
|
if err != nil { |
|
93
|
|
|
fmt.Printf("Url cannot fetched: %s\n", uri) |
|
94
|
|
|
fmt.Println(err) |
|
95
|
|
|
os.Exit(1) |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
rawXMLData := readXMLFromResponse(resp) |
|
99
|
|
|
|
|
100
|
|
|
urlSet := newURLSetFromXML(rawXMLData) |
|
101
|
|
|
if Verbose {fmt.Printf("URLSet Generated.\n")} |
|
102
|
|
|
|
|
103
|
|
|
newURLSet := urlSet.validate() |
|
104
|
|
|
if Verbose {fmt.Printf("URLSet Validated.\n")} |
|
105
|
|
|
|
|
106
|
|
|
err = newURLSet.saveToFile(filename) |
|
107
|
|
|
|
|
108
|
|
|
if err != nil { |
|
109
|
|
|
fmt.Println(err) |
|
110
|
|
|
os.Exit(1) |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|