forked from WiiLink24/NewsChannel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources.go
More file actions
156 lines (134 loc) · 3.61 KB
/
sources.go
File metadata and controls
156 lines (134 loc) · 3.61 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
150
151
152
153
154
155
156
package main
import (
"WiiNewsPR/news/endi"
_ "embed"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"time"
)
type Source struct {
Logo uint8
Position uint8
_ uint16
PictureSize uint32
PictureOffset uint32
NameSize uint32
NameOffset uint32
CopyrightSize uint32
CopyrightOffset uint32
}
func (n *News) GetNewsArticles() {
n.newsSource = endi.NewEndi(n.oldArticleTitles)
var err error
n.articles, err = n.newsSource.GetArticles()
if err != nil {
panic(err)
}
// Save articles to file for inspection (Debug)
// n.debugSaveArticles()
}
func (n *News) MakeSourceTable() {
n.Header.SourceTableOffset = n.GetCurrentSize()
logo := n.newsSource.GetLogo()
n.Sources = append(n.Sources, Source{
Logo: 0,
Position: 1,
PictureSize: uint32(len(logo)),
PictureOffset: 0,
NameSize: 0,
NameOffset: 0,
CopyrightSize: 0,
CopyrightOffset: 0,
})
n.Sources[0].PictureOffset = n.GetCurrentSize()
n.SourcePictures = logo
for n.GetCurrentSize()%4 != 0 {
n.SourcePictures = append(n.SourcePictures, 0)
}
n.Header.NumberOfSources = 1
}
// debugSaveArticles saves the fetched articles to a readable JSON file so you can see what was fetched.
func (n *News) debugSaveArticles() {
if len(n.articles) == 0 {
fmt.Printf("No articles found\n")
return
}
// Create directory
err := os.MkdirAll("debug", 0755)
if err != nil {
fmt.Printf("Error creating debug directory: %v\n", err)
return
}
// Structure
type DebugArticle struct {
Title string `json:"title"`
Content string `json:"content"`
Topic string `json:"topic"`
Location string `json:"location"`
HasImage bool `json:"hasImage"`
ImageSize int `json:"imageSize"`
ImageCaption string `json:"imageCaption"`
ImageBase64 string `json:"imageBase64,omitempty"`
}
var debugArticles []DebugArticle
topicNames := []string{"National", "International", "Sports", "Entertainment", "Business", "Science", "Technology"}
for _, article := range n.articles {
var content string
if article.Content != nil {
content = *article.Content
} else {
content = "No content"
}
var location string
if article.Location != nil {
location = article.Location.Name
} else {
location = "No location"
}
var topicName string
if int(article.Topic) < len(topicNames) {
topicName = topicNames[article.Topic]
} else {
topicName = fmt.Sprintf("Topic_%d", article.Topic)
}
var hasImage bool
var imageSize int
var imageCaption string
var imageBase64 string
if article.Thumbnail != nil {
hasImage = true
imageSize = len(article.Thumbnail.Image)
imageCaption = article.Thumbnail.Caption
if len(article.Thumbnail.Image) > 0 {
imageBase64 = base64.StdEncoding.EncodeToString(article.Thumbnail.Image)
}
}
debugArticles = append(debugArticles, DebugArticle{
Title: article.Title,
Content: content,
Topic: topicName,
Location: location,
HasImage: hasImage,
ImageSize: imageSize,
ImageCaption: imageCaption,
ImageBase64: imageBase64,
})
}
// Create filename with timestamp
timestamp := time.Now().Format("2006-01-02_15-04-05")
filename := fmt.Sprintf("debug/articles_%s.json", timestamp)
// Save to JSON file
jsonData, err := json.MarshalIndent(debugArticles, "", " ")
if err != nil {
fmt.Printf("Error marshaling articles: %v\n", err)
return
}
err = os.WriteFile(filename, jsonData, 0644)
if err != nil {
fmt.Printf("Error writing debug file: %v\n", err)
return
}
fmt.Printf("Debug: Saved %d articles to %s\n", len(debugArticles), filename)
}