shiori/cmd/add.go

96 lines
2.4 KiB
Go
Raw Normal View History

package cmd
import (
"github.com/RadhiFadlillah/go-readability"
2018-01-30 17:16:25 +08:00
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
2018-02-17 22:49:16 +08:00
"html/template"
2018-02-25 17:04:12 +08:00
"strings"
"time"
)
var (
addCmd = &cobra.Command{
Use: "add url",
2018-01-30 14:31:31 +08:00
Short: "Bookmark the specified URL.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Read flag and arguments
url := args[0]
2018-01-30 15:57:36 +08:00
title, _ := cmd.Flags().GetString("title")
excerpt, _ := cmd.Flags().GetString("excerpt")
tags, _ := cmd.Flags().GetStringSlice("tags")
2018-01-30 15:57:36 +08:00
offline, _ := cmd.Flags().GetBool("offline")
2018-02-25 17:04:12 +08:00
// Create bookmark item
bookmark := model.Bookmark{
URL: url,
Title: normalizeSpace(title),
Excerpt: normalizeSpace(excerpt),
}
bookmark.Tags = make([]model.Tag, len(tags))
for i, tag := range tags {
2018-02-26 21:01:39 +08:00
bookmark.Tags[i].Name = strings.TrimSpace(tag)
2018-02-25 17:04:12 +08:00
}
// Save new bookmark
2018-02-25 17:04:12 +08:00
result, err := addBookmark(bookmark, offline)
if err != nil {
cError.Println(err)
2018-02-25 17:04:12 +08:00
return
}
2018-02-12 22:06:53 +08:00
2018-02-25 17:04:12 +08:00
printBookmark(result)
},
}
)
func init() {
2018-01-30 15:57:36 +08:00
addCmd.Flags().StringP("title", "i", "", "Custom title for this bookmark.")
addCmd.Flags().StringP("excerpt", "e", "", "Custom excerpt for this bookmark.")
addCmd.Flags().StringSliceP("tags", "t", []string{}, "Comma-separated tags for this bookmark.")
2018-01-30 15:57:36 +08:00
addCmd.Flags().BoolP("offline", "o", false, "Save bookmark without fetching data from internet.")
rootCmd.AddCommand(addCmd)
}
2018-02-25 17:04:12 +08:00
func addBookmark(base model.Bookmark, offline bool) (book model.Bookmark, err error) {
// Prepare initial result
book = base
2018-01-30 15:57:36 +08:00
// Fetch data from internet
if !offline {
2018-02-25 17:04:12 +08:00
article, err := readability.Parse(book.URL, 10*time.Second)
2018-01-30 15:57:36 +08:00
if err != nil {
2018-01-30 17:16:25 +08:00
cError.Println("Failed to fetch article from internet:", err)
2018-02-25 17:04:12 +08:00
if book.Title == "" {
book.Title = "Untitled"
}
} else {
book.URL = article.URL
book.ImageURL = article.Meta.Image
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)
2018-01-30 17:16:25 +08:00
2018-02-25 17:04:12 +08:00
if book.Title == "" {
book.Title = article.Meta.Title
}
2018-01-30 15:57:36 +08:00
2018-02-25 17:04:12 +08:00
if book.Excerpt == "" {
book.Excerpt = article.Meta.Excerpt
}
}
}
2018-01-30 17:16:25 +08:00
// Save to database
2018-02-25 17:04:12 +08:00
book.ID, err = DB.CreateBookmark(book)
return book, err
}
2018-02-25 17:04:12 +08:00
func normalizeSpace(str string) string {
return strings.Join(strings.Fields(str), " ")
}