2018-01-28 14:55:43 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/RadhiFadlillah/go-readability"
|
2018-01-30 17:16:25 +08:00
|
|
|
"github.com/RadhiFadlillah/shiori/model"
|
2018-01-28 14:55:43 +08:00
|
|
|
"github.com/spf13/cobra"
|
2018-02-17 22:49:16 +08:00
|
|
|
"html/template"
|
2018-01-28 14:55:43 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
addCmd = &cobra.Command{
|
|
|
|
Use: "add url",
|
2018-01-30 14:31:31 +08:00
|
|
|
Short: "Bookmark the specified URL.",
|
2018-01-28 14:55:43 +08:00
|
|
|
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")
|
2018-01-28 14:55:43 +08:00
|
|
|
tags, _ := cmd.Flags().GetStringSlice("tags")
|
2018-01-30 15:57:36 +08:00
|
|
|
offline, _ := cmd.Flags().GetBool("offline")
|
2018-01-28 14:55:43 +08:00
|
|
|
|
|
|
|
// Save new bookmark
|
2018-02-12 22:06:53 +08:00
|
|
|
bookmark, err := addBookmark(url, title, excerpt, tags, offline)
|
2018-01-28 14:55:43 +08:00
|
|
|
if err != nil {
|
|
|
|
cError.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-02-12 22:06:53 +08:00
|
|
|
|
|
|
|
printBookmark(bookmark)
|
2018-01-28 14:55:43 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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.")
|
2018-01-28 14:55:43 +08:00
|
|
|
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.")
|
2018-01-28 14:55:43 +08:00
|
|
|
rootCmd.AddCommand(addCmd)
|
|
|
|
}
|
|
|
|
|
2018-02-12 22:06:53 +08:00
|
|
|
func addBookmark(url, title, excerpt string, tags []string, offline bool) (book model.Bookmark, err error) {
|
2018-01-30 15:57:36 +08:00
|
|
|
// Fetch data from internet
|
2018-01-30 17:16:25 +08:00
|
|
|
article := readability.Article{}
|
2018-01-30 15:57:36 +08:00
|
|
|
if !offline {
|
|
|
|
article, err = readability.Parse(url, 10*time.Second)
|
|
|
|
if err != nil {
|
2018-01-30 17:16:25 +08:00
|
|
|
cError.Println("Failed to fetch article from internet:", err)
|
|
|
|
article.URL = url
|
|
|
|
article.Meta.Title = "Untitled"
|
2018-01-30 15:57:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
// Prepare bookmark
|
|
|
|
bookmark := model.Bookmark{
|
|
|
|
URL: article.URL,
|
|
|
|
Title: article.Meta.Title,
|
|
|
|
ImageURL: article.Meta.Image,
|
|
|
|
Excerpt: article.Meta.Excerpt,
|
|
|
|
Author: article.Meta.Author,
|
|
|
|
MinReadTime: article.Meta.MinReadTime,
|
|
|
|
MaxReadTime: article.Meta.MaxReadTime,
|
|
|
|
Content: article.Content,
|
2018-02-17 22:49:16 +08:00
|
|
|
HTML: template.HTML(article.RawContent),
|
2018-01-30 17:16:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bookTags := make([]model.Tag, len(tags))
|
|
|
|
for i, tag := range tags {
|
|
|
|
bookTags[i].Name = tag
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmark.Tags = bookTags
|
|
|
|
|
2018-01-30 15:57:36 +08:00
|
|
|
// Set custom value
|
|
|
|
if title != "" {
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmark.Title = title
|
2018-01-30 15:57:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if excerpt != "" {
|
2018-01-30 17:16:25 +08:00
|
|
|
bookmark.Excerpt = excerpt
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 17:16:25 +08:00
|
|
|
// Save to database
|
2018-02-20 17:48:02 +08:00
|
|
|
bookmark.ID, err = DB.CreateBookmark(bookmark)
|
2018-01-28 14:55:43 +08:00
|
|
|
if err != nil {
|
2018-02-12 22:06:53 +08:00
|
|
|
return book, err
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|
|
|
|
|
2018-02-13 17:14:08 +08:00
|
|
|
bookmark.Modified = time.Now().UTC().Format("2006-01-02 15:04:05")
|
2018-02-12 22:06:53 +08:00
|
|
|
return bookmark, nil
|
2018-01-28 14:55:43 +08:00
|
|
|
}
|