Initial commit.
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package rss
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var processedItemsDir string
|
||||
|
||||
//nolint:gochecknoinits
|
||||
func init() {
|
||||
userHomeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic("Failed to get user's home directory: " + err.Error())
|
||||
}
|
||||
|
||||
processedItemsDir = strings.Replace("~/.rsser", "~", userHomeDir, 1)
|
||||
|
||||
if err := os.MkdirAll(processedItemsDir, 0o755); err != nil {
|
||||
panic("Failed to create ~/.rsser: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RSS) addProcessedItem(domain, guid string) error {
|
||||
file, err := os.OpenFile(
|
||||
filepath.Join(processedItemsDir, domain),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY,
|
||||
0o644,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("add processed item: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if _, err := file.WriteString(guid + "\n"); err != nil {
|
||||
return fmt.Errorf("add processed item: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RSS) checkItemWasProcessed(domain, guid string) (bool, error) {
|
||||
fileDataBytes, err := os.ReadFile(filepath.Join(processedItemsDir, domain))
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, fmt.Errorf("check item processing: %w", err)
|
||||
}
|
||||
|
||||
fileData := string(fileDataBytes)
|
||||
|
||||
for line := range strings.Lines(fileData) {
|
||||
if line == guid+"\n" {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user