forked from WiiLink24/NewsChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (117 loc) · 3.31 KB
/
main.go
File metadata and controls
142 lines (117 loc) · 3.31 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
138
139
140
141
142
package main
import (
"WiiNewsPR/news"
"bytes"
"encoding/binary"
"flag"
"fmt"
"hash/crc32"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/wii-tools/lzx/lz10"
)
type News struct {
Header Header
Headlines []Headlines
HeadlineText []uint16
Topics []Topic
Timestamps []Timestamp
TopicText []uint16
Articles []Article
ArticleText []uint16
Sources []Source
SourcePictures []byte
SourceCopyright []byte
Locations []Location
LocationText []uint16
Images []Image
ImagesData []byte
CaptionData []uint16
newsSource news.Source
currentLanguageCode uint8
currentCountryCode uint8
currentHour int
// Titles of articles from previous hours. Required for making sure we don't have duplicates.
oldArticleTitles []string
// Placeholder for the timestamps for a specific topic.
timestamps [][]Timestamp
articles []news.Article
// Placeholder for the topics.
topics []Topic
}
var currentTime = 0
func main() {
outputDir := flag.String("o", ".", "Output directory for generated files (default: . [current directory])")
cacheDir := flag.String("c", "./cache", "Cache directory for articles generated previously (default: ./cache)")
flag.Parse()
n := News{}
n.currentCountryCode = 49 // USA
n.currentLanguageCode = 1 // English
t := time.Now()
currentTime = int(t.Unix())
n.currentHour = t.Hour()
buffer := new(bytes.Buffer)
n.ReadNewsCache(*cacheDir)
n.GetNewsArticles()
n.MakeHeader()
n.MakeWiiMenuHeadlines()
n.MakeArticleTable()
n.MakeTopicTable()
n.MakeSourceTable()
n.WriteNewsCache(*cacheDir)
n.MakeLocationTable()
n.WriteImages()
n.Header.Filesize = n.GetCurrentSize()
n.WriteAll(buffer)
crcTable := crc32.MakeTable(crc32.IEEE)
checksum := crc32.Checksum(buffer.Bytes()[12:], crcTable)
n.Header.CRC32 = checksum
buffer.Reset()
n.WriteAll(buffer)
compressed, err := lz10.Compress(buffer.Bytes())
checkError(err)
// If the folder exists we can just continue
outputPath := filepath.Join(*outputDir, fmt.Sprintf("v2/%d/%03d", n.currentLanguageCode, n.currentCountryCode))
err = os.MkdirAll(outputPath, os.ModePerm)
if !os.IsExist(err) {
checkError(err)
}
outputFile := filepath.Join(outputPath, fmt.Sprintf("news.bin.%02d", n.currentHour))
err = os.WriteFile(outputFile, SignFile(compressed), 0666)
checkError(err)
log.Printf("Successfully generated news file for %d/%03d at hour %02d\n", n.currentLanguageCode, n.currentCountryCode, n.currentHour)
}
func checkError(err error) {
if err != nil {
log.Fatalf("News Channel file generator has encountered a fatal error! Reason: %v\n", err)
}
}
func Write(writer io.Writer, data any) {
err := binary.Write(writer, binary.BigEndian, data)
checkError(err)
}
func (n *News) WriteAll(writer io.Writer) {
Write(writer, n.Header)
Write(writer, n.Headlines)
Write(writer, n.HeadlineText)
Write(writer, n.Articles)
Write(writer, n.ArticleText)
Write(writer, n.Topics)
Write(writer, n.Timestamps)
Write(writer, n.TopicText)
Write(writer, n.Sources)
Write(writer, n.SourcePictures)
Write(writer, n.Locations)
Write(writer, n.LocationText)
Write(writer, n.Images)
Write(writer, n.ImagesData)
Write(writer, n.CaptionData)
}
func (n *News) GetCurrentSize() uint32 {
buffer := bytes.NewBuffer(nil)
n.WriteAll(buffer)
return uint32(buffer.Len())
}