forked from WiiLink24/NewsChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticles.go
More file actions
148 lines (121 loc) · 3.96 KB
/
articles.go
File metadata and controls
148 lines (121 loc) · 3.96 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
package main
import (
"math"
"unicode/utf16"
)
type Article struct {
ID uint32
SourceIndex uint32
LocationIndex uint32
PictureTimestamp uint32
PictureIndex uint32
PublishedTime uint32
UpdatedTime uint32
HeadlineSize uint32
HeadlineOffset uint32
ArticleTextSize uint32
ArticleTextOffset uint32
}
type Image struct {
CreditSize uint32
CreditOffset uint32
CaptionSize uint32
CaptionOffset uint32
PictureSize uint32
PictureOffset uint32
}
func (n *News) MakeArticleTable() {
n.Header.ArticleTableOffset = n.GetCurrentSize()
// First write all metadata
for i, article := range n.articles {
publishedTime := currentTime
// FORK UPDATE: Always use San Juan location (index 0)
locationIndex := uint32(0)
n.Articles = append(n.Articles, Article{
ID: uint32(i + 1),
SourceIndex: 0,
LocationIndex: locationIndex,
PictureTimestamp: 0,
PictureIndex: math.MaxUint32,
PublishedTime: fixTime(publishedTime),
UpdatedTime: fixTime(currentTime),
HeadlineSize: 0,
HeadlineOffset: 0,
ArticleTextSize: 0,
ArticleTextOffset: 0,
})
n.timestamps[article.Topic+1] = append(n.timestamps[article.Topic+1], Timestamp{
Time: fixTime(currentTime),
ArticleNumber: uint32(i + 1),
})
}
// Next write the text
for i, article := range n.articles {
encodedTitle := utf16.Encode([]rune(article.Title))
encodedArticle := utf16.Encode([]rune(*article.Content))
n.Articles[i].HeadlineSize = uint32(len(encodedTitle) * 2)
n.Articles[i].ArticleTextSize = uint32(len(encodedArticle) * 2)
n.Articles[i].HeadlineOffset = n.GetCurrentSize()
n.ArticleText = append(n.ArticleText, encodedTitle...)
// Null terminator
n.ArticleText = append(n.ArticleText, 0)
for n.GetCurrentSize()%4 != 0 {
n.ArticleText = append(n.ArticleText, uint16(0))
}
n.Articles[i].ArticleTextOffset = n.GetCurrentSize()
n.ArticleText = append(n.ArticleText, encodedArticle...)
// Null terminator
n.ArticleText = append(n.ArticleText, 0)
for n.GetCurrentSize()%4 != 0 {
n.ArticleText = append(n.ArticleText, uint16(0))
}
}
n.Header.NumberOfArticles = uint32(len(n.Articles))
}
func (n *News) WriteImages() {
n.Header.ImagesTableOffset = n.GetCurrentSize()
// First, create a consistent list of articles with valid images
var articlesWithImages []int // Store indices of articles that have valid images
for i, article := range n.articles {
if article.Thumbnail != nil && len(article.Thumbnail.Image) > 0 {
articlesWithImages = append(articlesWithImages, i)
}
}
// Create Image structs for articles with valid images
for _, articleIndex := range articlesWithImages {
article := n.articles[articleIndex]
n.Images = append(n.Images, Image{
CreditSize: 0,
CreditOffset: 0,
CaptionSize: 0,
CaptionOffset: 0,
PictureSize: uint32(len(article.Thumbnail.Image)),
PictureOffset: 0,
})
}
for imageIndex, articleIndex := range articlesWithImages {
article := n.articles[articleIndex]
n.Images[imageIndex].PictureOffset = n.GetCurrentSize()
n.ImagesData = append(n.ImagesData, article.Thumbnail.Image...)
for n.GetCurrentSize()%4 != 0 {
n.ImagesData = append(n.ImagesData, 0)
}
n.Articles[articleIndex].PictureIndex = uint32(imageIndex)
n.Articles[articleIndex].PictureTimestamp = fixTime(currentTime)
}
for imageIndex, articleIndex := range articlesWithImages {
article := n.articles[articleIndex]
// Only process caption if it exists
if article.Thumbnail.Caption != "" {
caption := utf16.Encode([]rune(article.Thumbnail.Caption))
n.Images[imageIndex].CaptionOffset = n.GetCurrentSize()
n.Images[imageIndex].CaptionSize = uint32(len(caption) / 2)
n.CaptionData = append(n.CaptionData, caption...)
n.CaptionData = append(n.CaptionData, 0)
for n.GetCurrentSize()%4 != 0 {
n.CaptionData = append(n.CaptionData, uint16(0))
}
}
}
n.Header.NumberOfImages = uint32(len(n.Images))
}