Skip to content

Commit 3091d84

Browse files
bakitofmartingr
andauthored
fix: parse pocket new CSV format (#1112)
* fix pocket parsing error Signed-off-by: bakito <[email protected]> * add tests forpocket csv Signed-off-by: bakito <[email protected]> * Use file name from test case * fix lint ant test issues Signed-off-by: bakito <[email protected]> --------- Signed-off-by: bakito <[email protected]> Co-authored-by: Felipe Martin <[email protected]>
1 parent d86c9cc commit 3091d84

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

internal/cmd/pocket.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"regexp"
1111
"slices"
1212
"strconv"
13-
"strings"
1413
"time"
1514

1615
"github.com/PuerkitoBio/goquery"
@@ -123,19 +122,23 @@ func parseCsvExport(ctx context.Context, db model.DB, srcFile *os.File) []model.
123122
os.Exit(1)
124123
}
125124

125+
var titleIdx, urlIdx, timeAddedIdx, tagsIdx int
126126
for i, cols := range records {
127127
// Check and skip header
128128
if i == 0 {
129-
expected := []string{"title", "url", "time_added", "cursor", "tags", "status"}
130-
if slices.Compare(cols, expected) != 0 {
131-
cError.Printf("Invalid CSV format. Header must be: %s\n", strings.Join(expected, ","))
129+
titleIdx = slices.Index(cols, "title")
130+
urlIdx = slices.Index(cols, "url")
131+
timeAddedIdx = slices.Index(cols, "time_added")
132+
tagsIdx = slices.Index(cols, "tags")
133+
if titleIdx == -1 || urlIdx == -1 || timeAddedIdx == -1 || tagsIdx == -1 {
134+
cError.Printf("Invalid CSV format. Header must contain: title, url, time_added, tags\n")
132135
os.Exit(1)
133136
}
134137
continue
135138
}
136139

137140
// Get metadata
138-
title, url, timeAdded, tags, err := verifyMetadata(cols[0], cols[1], cols[2], cols[4])
141+
title, url, timeAdded, tags, err := verifyMetadata(cols[titleIdx], cols[urlIdx], cols[timeAddedIdx], cols[tagsIdx])
139142
if err != nil {
140143
cError.Printf("Skip %s: %v\n", url, err)
141144
continue

internal/cmd/pocket_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/go-shiori/shiori/internal/database"
10+
)
11+
12+
func Test_parseCsvExport_old_format(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
fileName string
16+
}{
17+
{
18+
name: "Test old file format",
19+
fileName: "pocket-old.csv",
20+
},
21+
{
22+
name: "Test new file format",
23+
fileName: "pocket-new.csv",
24+
},
25+
}
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
file, err := os.Open("../../testdata/" + tt.fileName)
29+
if err != nil {
30+
t.Error(err.Error())
31+
}
32+
defer file.Close()
33+
ctx := context.TODO()
34+
35+
tmpDir, err := os.MkdirTemp("", "shiori-test-*")
36+
if err != nil {
37+
t.Fatalf("failed to create temp dir: %v", err)
38+
}
39+
defer os.RemoveAll(tmpDir)
40+
41+
dbPath := filepath.Join(tmpDir, "shiori.db")
42+
db, err := database.OpenSQLiteDatabase(ctx, dbPath)
43+
if err != nil {
44+
t.Fatalf("failed to open sqlite database: %v", err)
45+
}
46+
47+
if err := db.Migrate(ctx); err != nil {
48+
t.Fatalf("failed to migrate sqlite database: %v", err)
49+
}
50+
51+
bookmarks := parseCsvExport(ctx, db, file)
52+
if len(bookmarks) != 1 {
53+
t.Errorf("Expected 1 bookmarks, got %d", len(bookmarks))
54+
}
55+
bm := bookmarks[0]
56+
if bm.Title != "Shiori" {
57+
t.Errorf("Expected Title Shiori got %s", bm.URL)
58+
}
59+
if bm.URL != "https://github.com/go-shiori/shiori" {
60+
t.Errorf("Expected URL https://github.com/go-shiori/shiori, got %s", bm.URL)
61+
}
62+
if len(bm.Tags) != 1 {
63+
t.Errorf("Expected 1 tags, got %d", len(bm.Tags))
64+
}
65+
if bm.Tags[0].Name != "shiori" {
66+
t.Errorf("Expected tag shiori, got %s", bm.Tags[0].Name)
67+
}
68+
if bm.CreatedAt == "" {
69+
t.Error("Expected CreatedAt to be not empty")
70+
}
71+
if bm.ModifiedAt == "" {
72+
t.Error("Expected CreatedAt to be not empty")
73+
}
74+
})
75+
}
76+
}

testdata/pocket-new.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title,url,time_added,tags,status
2+
Shiori,https://github.com/go-shiori/shiori,1541343937,shiori,unread

testdata/pocket-old.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title,url,time_added,cursor,tags,status
2+
Shiori,https://github.com/go-shiori/shiori,1541343937,,shiori,unread

0 commit comments

Comments
 (0)