Passed
Push — master ( fcc4d9...d0843d )
by Emre
02:21
created

main.batchProcess   B

Complexity

Conditions 8

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nop 1
dl 0
loc 25
rs 7.3333
c 0
b 0
f 0
1
package main
2
3
import (
4
	"encoding/xml"
5
	"fmt"
6
	"net/http"
7
	"net/url"
8
	"os"
9
	"path"
10
	"time"
11
)
12
13
type SitemapIndex struct {
0 ignored issues
show
introduced by
exported type SitemapIndex should have comment or be unexported
Loading history...
14
	XMLName xml.Name  `xml:"sitemapindex"`
15
	XMLNs   string    `xml:"xmlns,attr"`
16
	Sitemap []Sitemap `xml:"sitemap"`
17
}
18
type Sitemap struct {
0 ignored issues
show
introduced by
exported type Sitemap should have comment or be unexported
Loading history...
19
	Loc     string `xml:"loc"`
20
	LastMod string `xml:"lastmod,omitempty"`
21
}
22
23
func (s Sitemap) findFileName() string {
24
	u, _ := url.Parse(s.Loc)
25
	dir := path.Dir(u.Path)[1:]
26
	filename := u.Path[len(dir)+1+1:]
27
28
	if _, err := os.Stat(dir); os.IsNotExist(err) != false {
29
		os.MkdirAll(dir, 0777)
30
	}
31
	filename = dir + string(os.PathSeparator) + filename
32
	return filename
33
}
34
func (si *SitemapIndex) validate() SitemapIndex {
35
	logChannel := make(chan string)
36
	validSitemapChannel := make(chan Sitemap)
37
38
    go func() {
39
        for _, sitemap := range (*si).Sitemap {
40
            sitemap.validate(logChannel,validSitemapChannel)
41
        }
42
        if Verbose {fmt.Println("Validation done")}
43
        close(logChannel)
44
        close(validSitemapChannel)
45
    }()
46
47
    go func() { 
48
        for {
49
            logMsg,isLogChannelOpen := <-logChannel
50
            if !isLogChannelOpen {
51
                break
52
            }
53
            fmt.Println(logMsg)
54
        }
55
    }()
56
57
    
58
	newSitemapIndex := SitemapIndex{
59
		XMLNs: si.XMLNs,
60
	}
61
62
	for {
63
        if Verbose { fmt.Println("Waits for sitemap data") }
64
        validSitemap, isValidSitemapChannelOpen := <-validSitemapChannel
65
        if !isValidSitemapChannelOpen {
66
            break
67
        }
68
		newSitemapIndex.Sitemap = append(newSitemapIndex.Sitemap, validSitemap)
69
	}
70
71
	return newSitemapIndex
72
}
73
74
func (s *Sitemap) validate(logChannel chan string,sitemapChannel chan Sitemap) {
75
    resp,err := http.Get((*s).Loc)
76
    if err!=nil {
77
        logChannel <- err.Error()
78
        return
79
    }
80
    logChannel <- fmt.Sprintf("Response code is %d for %s", resp.StatusCode, (*s).Loc)
81
    if resp.StatusCode == 200 {
82
       if Verbose { fmt.Println("Sitemap returning to channel") }
83
       sitemapChannel <- (*s)
84
       if Verbose { fmt.Println("Sitemap returned to channel") }
85
    }
86
    return
87
}
88
89
func (si *SitemapIndex) saveToFile(filename string) error {
90
	m, err := xml.Marshal((*si))
91
	if err != nil {
92
		return err
93
	}
94
95
	file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
96
	file.Write([]byte(xml.Header))
97
	file.Write(m)
98
	file.Close()
99
	return err
100
}
101
102
func batchProcess(uri string) {
103
	resp, err := http.Get(uri)
104
	if err != nil {
105
		fmt.Printf("Url cannot fetched: %s\n", uri)
106
		fmt.Println(err)
107
		os.Exit(1)
108
	}
109
110
	rawXMLData := readXMLFromResponse(resp)
111
    if Verbose {fmt.Printf("XML readed from response\n")}
112
113
	sitemapIndex := newSitemapIndexFromXML(rawXMLData)
114
    if Verbose {fmt.Printf("New sitemap created\n")}
115
	newSitemapIndex := sitemapIndex.validate()
116
    if Verbose {fmt.Printf("Sitemap validated\n")}
117
118
	for _, sitemap := range newSitemapIndex.Sitemap {
119
        if Verbose  {fmt.Printf("Wait for 2 sec.\n")}
120
		time.Sleep(time.Second * 2)
121
		filename := sitemap.findFileName()
122
        if Verbose {fmt.Printf("Filename is %s\n",filename)}
123
		singleProcess(sitemap.Loc, filename)
124
	}
125
126
	newSitemapIndex.saveToFile(OutputFileName)
127
}
128
129
func newSitemapIndexFromXML(rawXMLData []byte) SitemapIndex {
130
	sm := SitemapIndex{}
131
	err := xml.Unmarshal(rawXMLData, &sm)
132
133
	if err != nil {
134
		fmt.Printf("Sitemap index cannot parsed. Because: %s", err)
135
		return SitemapIndex{}
136
	}
137
	return sm
138
}
139