shiori/cmd/import.go

126 lines
2.7 KiB
Go
Raw Normal View History

2018-02-25 17:04:12 +08:00
package cmd
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/RadhiFadlillah/shiori/model"
"github.com/spf13/cobra"
2018-02-25 17:04:12 +08:00
)
var (
importCmd = &cobra.Command{
2018-02-26 09:19:37 +08:00
Use: "import source-file",
Short: "Import bookmarks from HTML file in Netscape Bookmark format",
2018-02-25 17:04:12 +08:00
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
generateTag := cmd.Flags().Changed("generate-tag")
if !generateTag {
var submitGenerateTag string
fmt.Print("Add parents folder as tag? (y/n): ")
fmt.Scanln(&submitGenerateTag)
generateTag = submitGenerateTag == "y"
}
err := importBookmarks(args[0], generateTag)
if err != nil {
cError.Println(err)
return
}
},
}
)
func init() {
importCmd.Flags().BoolP("generate-tag", "t", false, "Auto generate tag from bookmark's category")
rootCmd.AddCommand(importCmd)
}
func importBookmarks(pth string, generateTag bool) error {
// Open file
srcFile, err := os.Open(pth)
if err != nil {
return err
}
defer srcFile.Close()
// Parse file
doc, err := goquery.NewDocumentFromReader(srcFile)
if err != nil {
return err
}
2018-03-02 17:12:31 +08:00
// Loop each bookmark item
2018-02-25 17:04:12 +08:00
bookmarks := []model.Bookmark{}
2018-03-02 17:12:31 +08:00
doc.Find("dt>a").Each(func(_ int, a *goquery.Selection) {
// Get related elements
dt := a.Parent()
dl := dt.Parent()
// Get metadata
title := a.Text()
url, _ := a.Attr("href")
2018-03-03 18:02:24 +08:00
strTags, _ := a.Attr("tags")
2018-03-02 17:12:31 +08:00
strModified, _ := a.Attr("last_modified")
intModified, _ := strconv.ParseInt(strModified, 10, 64)
modified := time.Unix(intModified, 0)
2018-03-03 18:02:24 +08:00
// Get bookmark tags
tags := []model.Tag{}
for _, strTag := range strings.Split(strTags, ",") {
if strTag != "" {
tags = append(tags, model.Tag{Name: strTag})
}
2018-03-03 18:02:24 +08:00
}
2018-03-02 17:12:31 +08:00
// Get bookmark excerpt
excerpt := ""
if dd := dt.Next(); dd.Is("dd") {
excerpt = dd.Text()
}
2018-02-25 17:04:12 +08:00
2018-03-02 17:12:31 +08:00
// Get category name for this bookmark
2018-03-03 18:02:24 +08:00
// and add it as tags (if necessary)
2018-03-02 17:12:31 +08:00
category := ""
if dtCategory := dl.Prev(); dtCategory.Is("h3") {
category = dtCategory.Text()
category = normalizeSpace(category)
category = strings.ToLower(category)
category = strings.Replace(category, " ", "-", -1)
}
2018-02-25 17:04:12 +08:00
2018-03-02 17:12:31 +08:00
if category != "" && generateTag {
2018-03-03 18:02:24 +08:00
tags = append(tags, model.Tag{Name: category})
2018-03-02 17:12:31 +08:00
}
// Add item to list
bookmark := model.Bookmark{
URL: url,
Title: normalizeSpace(title),
Excerpt: normalizeSpace(excerpt),
Modified: modified.Format("2006-01-02 15:04:05"),
Tags: tags,
}
2018-02-25 17:04:12 +08:00
2018-03-02 17:12:31 +08:00
bookmarks = append(bookmarks, bookmark)
2018-02-25 17:04:12 +08:00
})
// Save bookmarks to database
for _, book := range bookmarks {
result, err := addBookmark(book, true)
if err != nil {
cError.Printf("URL %s already exists\n\n", book.URL)
continue
}
printBookmark(result)
}
return nil
}