diff --git a/db/db.go b/db/db.go index ae66276..4e78a43 100644 --- a/db/db.go +++ b/db/db.go @@ -6,6 +6,8 @@ import ( "fmt" "os" "sync" + + "gitlab.com/balki/ytui/pubsub" ) type DownloadStatus string @@ -18,14 +20,13 @@ var ( ) type Item struct { - Id int `json:"id"` - Date string `json:"date"` - URL string `json:"url"` - Title string `json:"title"` - Approved bool `json:"approved"` - Status DownloadStatus `json:"status"` - FileName string `json:"file_name"` - Progress string `json:"-"` + Id int `json:"id"` + Date string `json:"date"` + URL string `json:"url"` + Title string `json:"title"` + Status DownloadStatus `json:"status"` + FileName string `json:"file_name"` + Pt pubsub.ProgressTracker `json:"-"` } type Jdb struct { @@ -37,16 +38,21 @@ type Db struct { mutex sync.Mutex lastId int path string + index map[string]int } -func (d *Db) Add(i Item) int { +func (d *Db) Add(i Item) (int, bool) { d.mutex.Lock() defer d.mutex.Unlock() + if id, ok := d.index[i.URL]; ok { + return id, false + } i.Id = d.lastId d.lastId++ d.items = append(d.items, i) d.save() - return i.Id + d.index[i.URL] = i.Id + return i.Id, true } func (d *Db) Update(id int, persist bool, f func(*Item)) error { @@ -101,14 +107,17 @@ func Load(path string) (*Db, error) { return nil, err } m := 0 + indexMap := map[string]int{} for _, item := range jd.Items { if item.Id > m { m = item.Id } + indexMap[item.URL] = item.Id } return &Db{ items: jd.Items, path: path, lastId: m + 1, + index: indexMap, }, nil } diff --git a/main.go b/main.go index c662459..210859e 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "time" "gitlab.com/balki/ytui/db" + "gitlab.com/balki/ytui/pubsub" ) //go:embed templates/index.html @@ -24,12 +25,11 @@ var page string const port = 8080 var ( - ytdlCmd = []string{"youtube-dl"} - videosPath = "./vids" - videosUrl = "/vids" - cachePath = "./cache" - dbPath = "./db.json" - approval bool = false + ytdlCmd = []string{"youtube-dl"} + videosPath = "./vids" + videosUrl = "/vids" + cachePath = "./cache" + dbPath = "./db.json" ) var d *db.Db @@ -41,7 +41,6 @@ func parse() { flag.StringVar(&videosUrl, "videosurl", videosUrl, "Prefix of the url, i.e. https://domain.com//