This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor booklist, formats, and indexer (closes #59)
- Loading branch information
Showing
40 changed files
with
4,047 additions
and
1,443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.