This repository was archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
95 lines (78 loc) · 1.97 KB
/
main.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
package main
import (
"bufio"
"compress/gzip"
"encoding/xml"
"flag"
"fmt"
"log"
"os"
)
var (
dir = flag.String("dir", "", "Directory which holds Users.xml file")
output = flag.String("output", "users.rdf.gz", "Output rdf.gz file")
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
type User struct {
Id string `xml:",attr"`
Reputation string `xml:",attr"`
CreationDate string `xml:",attr"`
DisplayName string `xml:",attr"`
Location string `xml:",attr"`
AboutMe string `xml:",attr"`
LastAccessDate string `xml:",attr"`
}
func main() {
flag.Parse()
err := os.RemoveAll(*output)
check(err)
o, err := os.OpenFile(*output, os.O_WRONLY|os.O_CREATE, 0755)
check(err)
f, err := os.Open(*dir + "/Users.xml")
check(err)
w := gzip.NewWriter(o)
log.Println("1/2 Reading users file")
c := bufio.NewReader(f)
decoder := xml.NewDecoder(c)
var str string
for {
t, _ := decoder.Token()
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
if se.Name.Local == "row" {
var u User
decoder.DecodeElement(&u, &se)
str = fmt.Sprintf("<u%s> <Id> %q .\n", u.Id, u.Id)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <Reputation> %q .\n", u.Id, u.Reputation)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <CreationDate> %q .\n", u.Id, u.CreationDate)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <LastAccessDate> %q .\n", u.Id, u.LastAccessDate)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <DisplayName> %q .\n", u.Id, u.DisplayName)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <Location> %q .\n", u.Id, u.Location)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <AboutMe> %q .\n", u.Id, u.AboutMe)
w.Write([]byte(str))
str = fmt.Sprintf("<u%s> <Type> \"User\" .\n", u.Id)
w.Write([]byte(str))
}
}
}
log.Println("Finished generating RDF.")
err = w.Flush()
check(err)
err = w.Close()
check(err)
err = o.Close()
check(err)
}