forked from go-shiori/shiori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
184 lines (155 loc) · 5.08 KB
/
update.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cmd
import (
"fmt"
"github.com/RadhiFadlillah/go-readability"
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
"html/template"
"strconv"
"strings"
"sync"
"time"
)
var (
updateCmd = &cobra.Command{
Use: "update [indices]",
Short: "Update the saved bookmarks.",
Long: "Update fields of an existing bookmark. " +
"Accepts space-separated list of indices (e.g. 5 6 23 4 110 45), hyphenated range (e.g. 100-200) or both (e.g. 1-3 7 9). " +
"If no arguments, ALL bookmarks will be updated. Update works differently depending on the flags:\n" +
"- If indices are passed without any flags (--url, --title, --tag and --excerpt), read the URLs from DB and update titles from web.\n" +
"- If --url is passed (and --title is omitted), update the title from web using the URL. While using this flag, update only accept EXACTLY one index.\n" +
"While updating bookmark's tags, you can use - to remove tag (e.g. -nature to remove nature tag from this bookmark).",
Run: func(cmd *cobra.Command, args []string) {
// Read flags
url, _ := cmd.Flags().GetString("url")
title, _ := cmd.Flags().GetString("title")
excerpt, _ := cmd.Flags().GetString("excerpt")
tags, _ := cmd.Flags().GetStringSlice("tags")
offline, _ := cmd.Flags().GetBool("offline")
skipConfirmation, _ := cmd.Flags().GetBool("yes")
// Check if --url flag is used
if url != "" {
if len(args) != 1 {
cError.Println("Update only accepts one index while using --url flag")
return
}
idx, err := strconv.Atoi(args[0])
if err != nil || idx < -1 {
cError.Println("Index is not valid")
return
}
}
// If no arguments, confirm to user
if len(args) == 0 && !skipConfirmation {
confirmUpdate := ""
fmt.Print("Update ALL bookmarks? (y/n): ")
fmt.Scanln(&confirmUpdate)
if confirmUpdate != "y" {
fmt.Println("No bookmarks updated")
return
}
}
// Update bookmarks
bookmarks, err := updateBookmarks(args, url, title, excerpt, tags, offline)
if err != nil {
cError.Println(err)
return
}
printBookmark(bookmarks...)
},
}
)
func init() {
updateCmd.Flags().StringP("url", "u", "", "New URL for this bookmark.")
updateCmd.Flags().StringP("title", "i", "", "New title for this bookmark.")
updateCmd.Flags().StringP("excerpt", "e", "", "New excerpt for this bookmark.")
updateCmd.Flags().StringSliceP("tags", "t", []string{}, "Comma-separated tags for this bookmark.")
updateCmd.Flags().BoolP("offline", "o", false, "Update bookmark without fetching data from internet.")
updateCmd.Flags().BoolP("yes", "y", false, "Skip confirmation prompt and update ALL bookmarks")
rootCmd.AddCommand(updateCmd)
}
func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) {
mutex := sync.Mutex{}
// Read bookmarks from database
bookmarks, err := DB.GetBookmarks(true, indices...)
if err != nil {
return []model.Bookmark{}, err
}
if len(bookmarks) == 0 {
return []model.Bookmark{}, fmt.Errorf("No matching index found")
}
if url != "" && len(bookmarks) == 1 {
bookmarks[0].URL = url
}
// If not offline, fetch articles from internet
if !offline {
waitGroup := sync.WaitGroup{}
for i, book := range bookmarks {
waitGroup.Add(1)
go func(pos int, book model.Bookmark) {
defer waitGroup.Done()
article, err := readability.Parse(book.URL, 10*time.Second)
if err == nil {
book.Title = article.Meta.Title
book.ImageURL = article.Meta.Image
book.Excerpt = article.Meta.Excerpt
book.Author = article.Meta.Author
book.MinReadTime = article.Meta.MinReadTime
book.MaxReadTime = article.Meta.MaxReadTime
book.Content = article.Content
book.HTML = template.HTML(article.RawContent)
mutex.Lock()
bookmarks[pos] = book
mutex.Unlock()
}
}(i, book)
}
waitGroup.Wait()
}
// Map the tags to be deleted
addedTags := make(map[string]struct{})
deletedTags := make(map[string]struct{})
for _, tag := range tags {
tag = strings.ToLower(tag)
tag = strings.TrimSpace(tag)
if strings.HasPrefix(tag, "-") {
tag = strings.TrimPrefix(tag, "-")
deletedTags[tag] = struct{}{}
} else {
addedTags[tag] = struct{}{}
}
}
// Set default title, excerpt and tags
for i := range bookmarks {
if title != "" {
bookmarks[i].Title = title
}
if excerpt != "" {
bookmarks[i].Excerpt = excerpt
}
tempAddedTags := make(map[string]struct{})
for key, value := range addedTags {
tempAddedTags[key] = value
}
newTags := []model.Tag{}
for _, tag := range bookmarks[i].Tags {
if _, isDeleted := deletedTags[tag.Name]; isDeleted {
tag.Deleted = true
}
if _, alreadyExist := addedTags[tag.Name]; alreadyExist {
delete(tempAddedTags, tag.Name)
}
newTags = append(newTags, tag)
}
for tag := range tempAddedTags {
newTags = append(newTags, model.Tag{Name: tag})
}
bookmarks[i].Tags = newTags
}
result, err := DB.UpdateBookmarks(bookmarks)
if err != nil {
return []model.Bookmark{}, fmt.Errorf("Failed to update bookmarks: %v", err)
}
return result, nil
}