-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
113 lines (97 loc) · 3.27 KB
/
utils.go
File metadata and controls
113 lines (97 loc) · 3.27 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
package feedfetcher
import (
"encoding/json"
"fmt"
"strconv"
)
// Redis key prefixes - used for building and parsing keys
const (
RedisFeedPrefix = "feed:"
RedisArticlesPrefix = "articles:"
RedisArticlePrefix = "article:"
)
// BuildRequestHeaders builds request headers for conditional GET requests
// Reference: api/src/lib/feedUtils.js buildRequestHeaders() function
func BuildRequestHeaders(lastModified, etag string) map[string]string {
headers := map[string]string{
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",
}
if lastModified != "" {
headers["If-Modified-Since"] = lastModified
}
if etag != "" {
headers["If-None-Match"] = etag
}
return headers
}
// RedisKeys represents Redis key names for a feed
type RedisKeys struct {
FeedKey string `json:"feedKey" yaml:"feedKey"`
ArticlesKey string `json:"articlesKey" yaml:"articlesKey"`
}
// BuildRedisKeys builds Redis key names for a feed
// Reference: api/src/lib/feedUtils.js buildRedisKeys() function
func BuildRedisKeys(feedURI string) RedisKeys {
return RedisKeys{
FeedKey: RedisFeedPrefix + feedURI,
ArticlesKey: RedisArticlesPrefix + feedURI,
}
}
// BuildArticleKey builds the article key for Redis sorted set
// Reference: api/src/lib/feedUtils.js buildArticleKey() function
func BuildArticleKey(hash string) string {
return RedisArticlePrefix + hash
}
// ProcessArticle processes an article by adding computed fields
// Reference: api/src/lib/feedUtils.js processArticle() function
func ProcessArticle(article Article, feedURI string) Article {
processed := article
processed.Hash = Hash(article)
processed.Score = Score(article)
processed.FeedURL = feedURI
return processed
}
// ShouldStoreArticle determines if an article should be stored in S3
// Reference: api/src/lib/feedUtils.js shouldStoreArticle() function
func ShouldStoreArticle(oldScore *string, newScore int64) bool {
// Store if article is new (oldScore is nil)
if oldScore == nil {
return true
}
// Parse oldScore as int64
oldScoreInt, err := strconv.ParseInt(*oldScore, 10, 64)
if err != nil {
// If parsing fails, treat as different score
return true
}
// Store if score has changed
return newScore != oldScoreInt
}
// IsValidArticle validates that an article has required fields
// Reference: api/src/lib/feedUtils.js isValidArticle() function
func IsValidArticle(article *Article) bool {
return article != nil && article.GUID != "" && article.Description != ""
}
// ExtractArticleIds extracts article IDs (hashes) from Redis keys by removing the "article:" prefix
// Reference: api/src/lib/feedUtils.js extractArticleIds() function
func ExtractArticleIds(articleKeys []string) []string {
prefixLength := len(RedisArticlePrefix)
ids := make([]string, len(articleKeys))
for i, key := range articleKeys {
if len(key) > prefixLength {
ids[i] = key[prefixLength:]
} else {
ids[i] = key
}
}
return ids
}
// GenerateArticleBody generates JSON body for S3 storage
// Reference: api/src/lib/feedUtils.js generateArticleBody() function
func GenerateArticleBody(article Article) (string, error) {
body, err := json.Marshal(article)
if err != nil {
return "", fmt.Errorf("failed to marshal article: %w", err)
}
return string(body), nil
}