2019-05-21 11:31:40 +08:00
|
|
|
package cmd
|
|
|
|
|
2019-05-23 10:22:47 +08:00
|
|
|
import (
|
2022-10-04 19:54:33 +08:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2019-05-23 10:22:47 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-10-09 23:05:52 +08:00
|
|
|
"strconv"
|
2019-05-23 10:22:47 +08:00
|
|
|
"strings"
|
2022-10-09 23:05:52 +08:00
|
|
|
"time"
|
2019-05-23 10:22:47 +08:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2019-09-20 17:48:57 +08:00
|
|
|
"github.com/go-shiori/shiori/internal/core"
|
2019-05-23 10:22:47 +08:00
|
|
|
"github.com/go-shiori/shiori/internal/model"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
2019-05-21 11:31:40 +08:00
|
|
|
|
|
|
|
func importCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "import source-file",
|
|
|
|
Short: "Import bookmarks from HTML file in Netscape Bookmark format",
|
|
|
|
Args: cobra.ExactArgs(1),
|
2019-05-23 10:22:47 +08:00
|
|
|
Run: importHandler,
|
2019-05-21 11:31:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolP("generate-tag", "t", false, "Auto generate tag from bookmark's category")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2019-05-23 10:22:47 +08:00
|
|
|
|
|
|
|
func importHandler(cmd *cobra.Command, args []string) {
|
|
|
|
// Parse flags
|
|
|
|
generateTag := cmd.Flags().Changed("generate-tag")
|
|
|
|
|
|
|
|
// If user doesn't specify, ask if tag need to be generated
|
|
|
|
if !generateTag {
|
|
|
|
var submit string
|
|
|
|
fmt.Print("Add parents folder as tag? (y/N): ")
|
|
|
|
fmt.Scanln(&submit)
|
|
|
|
|
|
|
|
generateTag = submit == "y"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open bookmark's file
|
|
|
|
srcFile, err := os.Open(args[0])
|
|
|
|
if err != nil {
|
|
|
|
cError.Printf("Failed to open %s: %v\n", args[0], err)
|
2019-12-13 15:55:55 +08:00
|
|
|
os.Exit(1)
|
2019-05-23 10:22:47 +08:00
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
|
|
|
|
// Parse bookmark's file
|
|
|
|
bookmarks := []model.Bookmark{}
|
|
|
|
mapURL := make(map[string]struct{})
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(srcFile)
|
|
|
|
if err != nil {
|
|
|
|
cError.Printf("Failed to parse bookmark: %v\n", err)
|
2019-12-13 15:55:55 +08:00
|
|
|
os.Exit(1)
|
2019-05-23 10:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
doc.Find("dt>a").Each(func(_ int, a *goquery.Selection) {
|
|
|
|
// Get related elements
|
|
|
|
dt := a.Parent()
|
|
|
|
dl := dt.Parent()
|
|
|
|
h3 := dl.Parent().Find("h3").First()
|
|
|
|
|
|
|
|
// Get metadata
|
|
|
|
title := a.Text()
|
|
|
|
url, _ := a.Attr("href")
|
|
|
|
strTags, _ := a.Attr("tags")
|
|
|
|
|
2022-10-09 23:05:52 +08:00
|
|
|
dateStr, fieldExists := a.Attr("last_modified")
|
|
|
|
if !fieldExists {
|
|
|
|
dateStr, _ = a.Attr("add_date")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Using now as default date in case no last_modified nor add_date are present
|
|
|
|
modifiedDate := time.Now()
|
|
|
|
if dateStr != "" {
|
|
|
|
modifiedTsInt, err := strconv.Atoi(dateStr)
|
|
|
|
if err != nil {
|
|
|
|
cError.Printf("Skip %s: date field is not valid: %s", url, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedDate = time.Unix(int64(modifiedTsInt), 0)
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:48:57 +08:00
|
|
|
// Clean up URL
|
|
|
|
url, err = core.RemoveUTMParams(url)
|
|
|
|
if err != nil {
|
2019-05-23 10:22:47 +08:00
|
|
|
cError.Printf("Skip %s: URL is not valid\n", url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-10 09:13:13 +08:00
|
|
|
// Make sure title is valid Utf-8
|
2019-09-22 11:55:22 +08:00
|
|
|
title = validateTitle(title, url)
|
2019-08-10 09:13:13 +08:00
|
|
|
|
2019-05-23 10:22:47 +08:00
|
|
|
// Check if the URL already exist before, both in bookmark
|
|
|
|
// file or in database
|
|
|
|
if _, exist := mapURL[url]; exist {
|
|
|
|
cError.Printf("Skip %s: URL already exists\n", url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-30 18:19:36 +08:00
|
|
|
_, exist, err := db.GetBookmark(cmd.Context(), 0, url)
|
2022-10-04 19:54:33 +08:00
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
2022-09-30 18:19:36 +08:00
|
|
|
cError.Printf("Skip %s: Get Bookmark fail, %v", url, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
2019-05-23 10:22:47 +08:00
|
|
|
cError.Printf("Skip %s: URL already exists\n", url)
|
|
|
|
mapURL[url] = struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get bookmark tags
|
|
|
|
tags := []model.Tag{}
|
|
|
|
for _, strTag := range strings.Split(strTags, ",") {
|
|
|
|
strTag = normalizeSpace(strTag)
|
|
|
|
if strTag != "" {
|
|
|
|
tags = append(tags, model.Tag{Name: strTag})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get category name for this bookmark
|
|
|
|
// and add it as tags (if necessary)
|
|
|
|
category := normalizeSpace(h3.Text())
|
|
|
|
if category != "" && generateTag {
|
|
|
|
tags = append(tags, model.Tag{Name: category})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add item to list
|
|
|
|
bookmark := model.Bookmark{
|
2022-10-09 23:05:52 +08:00
|
|
|
URL: url,
|
|
|
|
Title: title,
|
|
|
|
Tags: tags,
|
|
|
|
Modified: modifiedDate.Format(model.DatabaseDateFormat),
|
2019-05-23 10:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mapURL[url] = struct{}{}
|
|
|
|
bookmarks = append(bookmarks, bookmark)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Save bookmark to database
|
2022-10-12 05:47:38 +08:00
|
|
|
bookmarks, err = db.SaveBookmarks(cmd.Context(), true, bookmarks...)
|
2019-05-23 10:22:47 +08:00
|
|
|
if err != nil {
|
|
|
|
cError.Printf("Failed to save bookmarks: %v\n", err)
|
2019-12-13 15:55:55 +08:00
|
|
|
os.Exit(1)
|
2019-05-23 10:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print imported bookmark
|
|
|
|
fmt.Println()
|
|
|
|
printBookmarks(bookmarks...)
|
|
|
|
}
|