-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikipedia.go
More file actions
137 lines (122 loc) · 2.54 KB
/
Copy pathwikipedia.go
File metadata and controls
137 lines (122 loc) · 2.54 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
)
var (
wikiToHtmlUrl = "http://localhost"
)
type page struct {
Title string `xml:"title"`
Text string `xml:"revision>text"`
}
func usage() {
fmt.Println("wikipedia <wikipedia dump file name>")
}
func main() {
url := flag.String("url", "http://localhost", "wikitext to HTML service URL")
port := flag.Int("port", 3000, "wikitext to HTML service port")
ignoreErrors := flag.Bool("ignore-errors", true,
"ignore and continue on errors")
flag.Parse()
// parse rest of the flags
tail := flag.Args()
var filename string
if len(tail) != 0 {
filename = tail[0]
}
if len(filename) == 0 {
usage()
return
}
wikiToHtmlUrl := *url + ":" + strconv.Itoa(*port) + "/parse"
f, err := os.Open(filename)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
r, err := Decompressor(f)
if err != nil {
log.Fatalln(err)
}
decoder := xml.NewDecoder(r)
done := false
for !done {
t, _ := decoder.Token()
if t == nil {
break
}
switch se := t.(type) {
case xml.StartElement:
if se.Name.Local == "page" {
var p page
decoder.DecodeElement(&p, &se)
// parse the page and store in DB
fmt.Println("Parsing:", p.Title)
text, err := parse(p.Text, wikiToHtmlUrl)
if err != nil {
log.Println(err)
dumpPage(p)
if !*ignoreErrors {
done = true
}
} else {
// store in db?
fmt.Println(string(text))
}
}
}
}
}
// parse document in wikitext format to HTML
//
// @doc: docunent in wikitext format
// @url: URL of wikitext to HTML conversion service
//
func parse(doc string, url string) ([]byte, error) {
buf := bytes.NewBufferString(doc)
resp, err := http.Post(url, "text/plain", buf)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func dumpPage(p page) {
log.Println("Error parsing page:", p.Title)
/*log.Println("Dumping page:")
log.Println("TITLE:\n", p.Title)
log.Println("TEXT:\n", p.Text)*/
}
// Source: decompressor() from Cayley project
const (
gzipMagic = "\x1f\x8b"
b2zipMagic = "BZh"
)
func Decompressor(r io.Reader) (io.Reader, error) {
br := bufio.NewReader(r)
buf, err := br.Peek(3)
if err != nil {
return nil, err
}
switch {
case bytes.Compare(buf[:2], []byte(gzipMagic)) == 0:
return gzip.NewReader(br)
case bytes.Compare(buf[:3], []byte(b2zipMagic)) == 0:
return bzip2.NewReader(br), nil
default:
return br, nil
}
}