Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Refactor booklist, formats, and indexer (closes #59)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Aug 13, 2018
1 parent 2bc35d0 commit ffa05bf
Show file tree
Hide file tree
Showing 40 changed files with 4,047 additions and 1,443 deletions.
6 changes: 3 additions & 3 deletions bookbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"strings"
"syscall"

_ "github.com/geek1011/BookBrowser/formats/epub"
_ "github.com/geek1011/BookBrowser/formats/pdf"
"github.com/geek1011/BookBrowser/modules/server"

_ "github.com/geek1011/BookBrowser/formats/all"
"github.com/geek1011/BookBrowser/modules/sigusr"
"github.com/geek1011/BookBrowser/modules/util"
"github.com/urfave/cli"
Expand Down Expand Up @@ -121,7 +121,7 @@ func main() {
s := server.NewServer(addr, bookdir, tempdir, curversion, true, nocovers)
s.RefreshBookIndex()

if len(*s.Books) == 0 {
if len(s.Indexer.BookList()) == 0 {
log.Fatalln("Fatal error: no books found")
}

Expand Down
30 changes: 30 additions & 0 deletions booklist/authorlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package booklist

import "sort"

type AuthorList []struct{ Name, ID string }

func (bl BookList) Authors() *AuthorList {
authors := AuthorList{}
done := map[string]bool{}
for _, b := range bl {
if b.Author == "" {
continue
}

if done[b.AuthorID()] {
continue
}
authors = append(authors, struct{ Name, ID string }{b.Author, b.AuthorID()})
done[b.AuthorID()] = true
}
return &authors
}

func (al AuthorList) Sorted(less func(a, b struct{ Name, ID string }) bool) AuthorList {
nal := al[:]
sort.SliceStable(nal, func(i, j int) bool {
return less(al[i], al[j])
})
return nal
}
40 changes: 40 additions & 0 deletions booklist/book.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package booklist

import (
"crypto/sha1"
"fmt"
"path/filepath"
"strings"
"time"
)

type Book struct {
Hash string
FilePath string
FileSize int64
ModTime time.Time

HasCover bool
Title string
Author string
Description string
Series string
SeriesIndex float64
Publisher string
}

func (b *Book) ID() string {
return b.Hash[:10]
}

func (b *Book) AuthorID() string {
return fmt.Sprintf("%x", sha1.Sum([]byte(b.Author)))[:10]
}

func (b *Book) SeriesID() string {
return fmt.Sprintf("%x", sha1.Sum([]byte(b.Series)))[:10]
}

func (b *Book) FileType() string {
return strings.Replace(strings.ToLower(filepath.Ext(b.FilePath)), ".", "", -1)
}
128 changes: 128 additions & 0 deletions booklist/booklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package booklist

import (
"sort"
"strings"
)

type BookList []*Book

func (bl BookList) Sorted(less func(a, b *Book) bool) BookList {
nbl := bl[:]
sort.SliceStable(nbl, func(i, j int) bool {
return less(bl[i], bl[j])
})
return nbl
}

func (bl BookList) Filtered(fn func(b *Book) bool) BookList {
nbl := BookList{}
for _, b := range bl {
if fn(b) {
nbl = append(nbl, b)
}
}
return nbl
}

func (bl BookList) Skip(n int) BookList {
if n >= len(bl) {
return BookList{}
}
return bl[n:]
}

func (bl BookList) Take(n int) BookList {
if n > len(bl) {
return bl
}
return bl[:n]
}

// SortBy sorts by sort, and returns a sorted copy. If sorter is invalid, it returns the original list.
//
// sort can be:
// - author-asc
// - author-desc
// - title-asc
// - title-desc
// - series-asc
// - series-desc
// - seriesindex-asc
// - seriesindex-desc
// - modified-desc
func (l BookList) SortBy(sort string) (nl BookList, sorted bool) {
sort = strings.ToLower(sort)

nb := l[:]

switch sort {
case "author-asc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Author != "" && b.Author != "" {
return a.Author < b.Author
}
return false
})
break
case "author-desc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Author != "" && b.Author != "" {
return a.Author > b.Author
}
return false
})
break
case "title-asc":
nb = nb.Sorted(func(a, b *Book) bool {
return a.Title < b.Title
})
break
case "title-desc":
nb = nb.Sorted(func(a, b *Book) bool {
return a.Title > b.Title
})
break
case "series-asc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Series != "" && b.Series != "" {
return a.Series < b.Series
}
return false
})
break
case "series-desc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Series != "" && b.Series != "" {
return a.Series > b.Series
}
return false
})
break
case "seriesindex-asc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Series != "" && b.Series != "" {
return a.SeriesIndex < b.SeriesIndex
}
return false
})
break
case "seriesindex-desc":
nb = nb.Sorted(func(a, b *Book) bool {
if a.Series != "" && b.Series != "" {
return a.SeriesIndex > b.SeriesIndex
}
return false
})
break
case "modified-desc":
nb = nb.Sorted(func(a, b *Book) bool {
return a.ModTime.Unix() > b.ModTime.Unix()
})
break
default:
return nb, false
}

return nb, true
}
30 changes: 30 additions & 0 deletions booklist/serieslist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package booklist

import "sort"

type SeriesList []struct{ Name, ID string }

func (bl BookList) Series() *SeriesList {
series := SeriesList{}
done := map[string]bool{}
for _, b := range bl {
if b.Series == "" {
continue
}

if done[b.SeriesID()] {
continue
}
series = append(series, struct{ Name, ID string }{b.Series, b.SeriesID()})
done[b.SeriesID()] = true
}
return &series
}

func (sl SeriesList) Sorted(less func(a, b struct{ Name, ID string }) bool) SeriesList {
nsl := sl[:]
sort.SliceStable(nsl, func(i, j int) bool {
return less(sl[i], sl[j])
})
return nsl
}
7 changes: 0 additions & 7 deletions formats/all/all.go

This file was deleted.

12 changes: 0 additions & 12 deletions formats/all/all_test.go

This file was deleted.

Loading

0 comments on commit ffa05bf

Please sign in to comment.