-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathparse_html_to_json.go
117 lines (115 loc) · 2.88 KB
/
parse_html_to_json.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
package main
import (
"country"
"encoding/json"
"io/ioutil"
"logger"
"os"
"path"
"runtime"
"sync"
)
// Converts html files into json files.
// Expects html files in dirs pages/YYYY-MM-DD/*.html
func main() {
// prepare for concurrent parsing
numCpus := runtime.NumCPU()-1
runtime.GOMAXPROCS(numCpus)
var wg sync.WaitGroup
// read config
configBytes, err := ioutil.ReadFile("config.json")
if err != nil {
logger.Stderr("Error reading config.json")
logger.Stderr(err)
return
}
// parse config
config := map[string]string{}
err = json.Unmarshal(configBytes, &config)
countryHtmlRoot, exists := config["country_html_root"]
if !exists {
logger.Stderr("Missing config value: country_html_root")
}
countryJsonRoot, exists := config["country_json_root"]
if !exists {
logger.Stderr("Missing config value: country_json_root")
}
// Get directories
dirs, err := ioutil.ReadDir(countryHtmlRoot)
if err != nil {
logger.Stderr("Error reading country_html_root directory")
logger.Stderr(countryHtmlRoot)
logger.Stderr(err)
return
}
// iterate over date directories
for _, dir := range dirs {
// ignore files
if !dir.IsDir() {
continue
}
// get the path and files for this directory
filesRoot := path.Join(countryHtmlRoot, dir.Name())
files, err := ioutil.ReadDir(filesRoot)
if err != nil {
logger.Stderr("Error reading directory")
logger.Stderr(filesRoot)
logger.Stderr(err)
return
}
// iterate over page files
for _, file := range files {
// ignore directories
if file.IsDir() {
logger.Stderr("Unexpected directory")
logger.Stderr(file.Name())
return
}
filelocation := path.Join(filesRoot, file.Name())
// check if already parsed
dstDir := path.Join(countryJsonRoot, dir.Name())
dst := path.Join(dstDir, file.Name()+".json")
wg.Add(1)
go func(src, dst string, wg *sync.WaitGroup) {
defer wg.Done()
_, err = os.Stat(dst)
if !os.IsNotExist(err) {
//logger.Stdout("Already parsed", filelocation)
return
}
// parse the file
logger.Stdout("Parsing", filelocation)
p, err := country.NewPage(filelocation)
if err != nil {
logger.Stderr("Error parsing file")
logger.Stderr(filelocation)
logger.Stderr(err)
return
}
// save the parsed json
content, err := json.MarshalIndent(p.ParsedData, "", " ")
if err != nil {
logger.Stderr("Error marshalling parsed page to json")
logger.Stderr(filelocation)
logger.Stderr(err)
return
}
err = os.MkdirAll(dstDir, 0775)
if err != nil {
logger.Stderr("Error creating json directory")
logger.Stderr(dstDir)
logger.Stderr(err)
return
}
err = ioutil.WriteFile(dst, content, 0664)
if err != nil {
logger.Stderr("Error saving content for json file")
logger.Stderr(dstDir)
logger.Stderr(err)
return
}
}(filelocation, dst, &wg)
}
}
wg.Wait()
}