mark/model_bookmark.go

95 lines
1.6 KiB
Go
Raw Permalink Normal View History

2017-09-20 22:12:17 +00:00
package main
2017-09-21 14:46:59 +00:00
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/PuerkitoBio/goquery"
)
2017-09-20 22:12:17 +00:00
type Bookmark struct {
Id int
Name string
2017-09-21 14:46:59 +00:00
Url *url.URL
2017-09-20 22:12:17 +00:00
Desc string
Tags []string
Created time.Time
LastUsed time.Time
}
2017-09-21 14:46:59 +00:00
func NewBookmark(url *url.URL) *Bookmark {
2017-09-20 22:12:17 +00:00
// First, search for a bookmark with this same URL
// If we didn't find one, create a new bookmark struct
b := Bookmark{
2017-09-21 14:46:59 +00:00
Id: 0,
Url: url,
Created: time.Now(),
2017-09-20 22:12:17 +00:00
}
return &b
}
2017-09-21 14:46:59 +00:00
func (b *Bookmark) DownloadDetail() error {
fmt.Println("Downloading Page: ", b.Url.String())
resp, err := http.Get(b.Url.String())
if err != nil {
return err
}
defer resp.Body.Close()
var doc *goquery.Document
doc, err = goquery.NewDocumentFromResponse(resp)
if err != nil {
return err
}
title := doc.Find("title").Text()
fmt.Println("Bookmark Title: " + title)
metas := doc.Find("meta")
metas.Each(func(i int, s *goquery.Selection) {
// For each item, check the 'name' attr
if v, has := s.Attr("name"); has {
if v == "description" {
str := s.Data
2017-09-21 14:46:59 +00:00
fmt.Println(str)
//b.Desc = s.Get(i)
}
}
})
return nil
}
func (b *Bookmark) SetName(nm string) {
b.Name = nm
}
func (b *Bookmark) SetUrl(url *url.URL) {
b.Url = url
}
func (b *Bookmark) SetDescription(desc string) {
b.Desc = desc
}
func (b *Bookmark) AddTag(tg string) {
for _, v := range b.Tags {
if v == tg {
return
}
}
b.Tags = append(b.Tags, tg)
}
func (b *Bookmark) RemoveTag(tg string) {
remId := -1
for idx := range b.Tags {
if b.Tags[idx] == tg {
remId = idx
break
}
}
if remId >= 0 {
b.Tags = append(b.Tags[:remId], b.Tags[remId+1:]...)
}
}