-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (122 loc) · 3.59 KB
/
Copy pathmain.go
File metadata and controls
149 lines (122 loc) · 3.59 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
143
144
145
146
147
148
149
package main
import (
"NewsChannel/news"
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"log"
"os"
"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
source 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
// Placeholder for locations. Used in order to collect all the used locations without duplicates.
locations []*news.Location
articles []news.Article
// Placeholder for the topics.
topics []Topic
}
var currentTime = 0
func main() {
// Load configuration from JSON file instead of having random numbers (added Spain btw)
config, err := LoadConfig("countries.json")
checkError(err)
// Process each country/language combination
for _, countryConfig := range config.Countries {
n := News{}
n.currentCountryCode = countryConfig.CountryCode
n.currentLanguageCode = countryConfig.LanguageCode
log.Printf("Processing %s (%s) - Country: %d, Language: %d",
countryConfig.Name, countryConfig.Language,
countryConfig.CountryCode, countryConfig.LanguageCode)
t := time.Now()
currentTime = int(t.Unix())
n.currentHour = t.Hour()
buffer := new(bytes.Buffer)
n.ReadNewsCache()
n.setSource(countryConfig.Source)
n.GetNewsArticles()
n.MakeHeader()
n.MakeWiiMenuHeadlines()
n.MakeArticleTable()
n.MakeTopicTable()
n.MakeSourceTable()
n.WriteNewsCache()
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
err = os.MkdirAll(fmt.Sprintf("./v2/%d/%03d", n.currentLanguageCode, n.currentCountryCode), os.ModePerm)
if !os.IsExist(err) {
checkError(err)
}
err = os.WriteFile(fmt.Sprintf("./v2/%d/%03d/news.bin.%02d", n.currentLanguageCode, n.currentCountryCode, n.currentHour), SignFile(compressed), 0666)
checkError(err)
log.Printf("Successfully generated news file for %s (%s)", countryConfig.Name, countryConfig.Language)
}
}
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())
}