Skip to content

Commit

Permalink
Add import pocket command
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed May 28, 2018
1 parent 2e48ba8 commit a74773b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
72 changes: 72 additions & 0 deletions cmd/cmd-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,78 @@ func (h *cmdHandler) exportBookmarks(cmd *cobra.Command, args []string) {
fmt.Println("Export finished")
}

// importPockets is handler for importing bookmarks from Pocket exported HTML file.
// Accept exactly one argument, the file to be imported.
func (h *cmdHandler) importPockets(cmd *cobra.Command, args []string) {
// Open bookmark's file
srcFile, err := os.Open(args[0])
if err != nil {
cError.Println(err)
return
}
defer srcFile.Close()

// Parse bookmark's file
doc, err := goquery.NewDocumentFromReader(srcFile)
if err != nil {
cError.Println(err)
return
}

bookmarks := []model.Bookmark{}
doc.Find("a").Each(func(_ int, a *goquery.Selection) {
// Get metadata
title := a.Text()
url, _ := a.Attr("href")
strTags, _ := a.Attr("tags")
strModified, _ := a.Attr("time_added")
intModified, _ := strconv.ParseInt(strModified, 10, 64)
modified := time.Unix(intModified, 0)

// Get bookmark tags
tags := []model.Tag{}
for _, strTag := range strings.Split(strTags, ",") {
if strTag != "" {
tags = append(tags, model.Tag{Name: strTag})
}
}

// Add item to list
bookmark := model.Bookmark{
URL: url,
Title: normalizeSpace(title),
Modified: modified.Format("2006-01-02 15:04:05"),
Tags: tags,
}

bookmarks = append(bookmarks, bookmark)
})

// Save bookmarks to database
for _, book := range bookmarks {
// Make sure URL valid
parsedURL, err := nurl.Parse(book.URL)
if err != nil || !valid.IsRequestURL(book.URL) {
cError.Println("URL is not valid")
continue
}

// Clear fragment and UTM parameters from URL
parsedURL.Fragment = ""
clearUTMParams(parsedURL)
book.URL = parsedURL.String()

// Save book to database
book.ID, err = h.db.CreateBookmark(book)
if err != nil {
cError.Println(err)
continue
}

printBookmarks(book)
}
}

func printBookmarks(bookmarks ...model.Bookmark) {
for _, bookmark := range bookmarks {
// Create bookmark index
Expand Down
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ func NewShioriCmd(db dt.Database, dataDir string) *cobra.Command {
Run: hdl.exportBookmarks,
}

pocketCmd := &cobra.Command{
Use: "pocket source-file",
Short: "Import bookmarks from Pocket's exported HTML file",
Args: cobra.ExactArgs(1),
Run: hdl.importPockets,
}

// Create sub command that has its own sub command
accountCmd := account.NewAccountCmd(db)
serveCmd := serve.NewServeCmd(db, dataDir)
Expand Down Expand Up @@ -130,6 +137,6 @@ func NewShioriCmd(db dt.Database, dataDir string) *cobra.Command {
}

rootCmd.AddCommand(accountCmd, serveCmd, addCmd, printCmd, searchCmd,
updateCmd, deleteCmd, openCmd, importCmd, exportCmd)
updateCmd, deleteCmd, openCmd, importCmd, exportCmd, pocketCmd)
return rootCmd
}

0 comments on commit a74773b

Please sign in to comment.