amuse.git

commit 93044567be67a856b101f77c547ba6ff9d06314e

Author: Adam <git@apiote.tk>

show progress in TV queue

 datastructure/item.go | 1 +
 datastructure/tvqueue.go | 16 ++++++++++++++++
 db/db.go | 42 ++++++++++++++++++++++++++++++++----------
 libamuse/common.go | 13 +++++++++++++
 libamuse/film.go | 12 ------------
 libamuse/serie.go | 1 +
 static/style/style.css | 4 ++++
 templates/tvqueue.html | 41 ++++++++++++++++++++++-------------------
 tmdb/serie.go | 8 +++++++-


diff --git a/datastructure/item.go b/datastructure/item.go
index 44ca31ef527796280cfee1e3f29e38916e646202..49049f609c6edc1ded63cd8fa5abdb1417e47353 100644
--- a/datastructure/item.go
+++ b/datastructure/item.go
@@ -16,6 +16,7 @@ 	Genres     string
 	Runtime    int
 	Collection int
 	Part       int
+	Episodes   int
 }
 
 func (i ItemInfo) IsUnreleased(itemType ItemType) bool {




diff --git a/datastructure/tvqueue.go b/datastructure/tvqueue.go
index 53a88961c7d51443a26e808e6c77a05d8432f8be..cfb4a96cd659d1c9647c0b5e9d0bc1838c45d50f 100644
--- a/datastructure/tvqueue.go
+++ b/datastructure/tvqueue.go
@@ -4,11 +4,20 @@ import (
 	"strconv"
 )
 
+func min(a, b int) int {
+	if a > b {
+		return b
+	} else {
+		return a
+	}
+} // todo replicated code from libamuse/serie.go
+
 type TvQueueEntry struct {
 	ItemInfo
 	Id              string
 	HasPrevious     bool
 	WatchedEpisodes int
+	SkippedEpisodes int
 }
 
 func (e TvQueueEntry) GetYears() string {
@@ -23,6 +32,13 @@ 		}
 	} else {
 		return strconv.FormatInt(int64(e.YearStart), 10) + "–"
 	}
+}
+
+func (e TvQueueEntry) CalculateProgress() int { // todo replicated code from libamuse/serie.go
+	if e.Episodes-e.SkippedEpisodes == 0 {
+		return 0
+	}
+	return min(e.WatchedEpisodes*100/(e.Episodes-e.SkippedEpisodes), 100)
 }
 
 type TvQueue struct {




diff --git a/db/db.go b/db/db.go
index 90e041fc25c069642c66ba889c67aa915a6c9403..ebbade87743bbaf7f8ac0959764d793c9b3c01c9 100644
--- a/db/db.go
+++ b/db/db.go
@@ -75,7 +75,7 @@ 	_, err = db.Exec(`create table experiences(username text, item_type text, item_id text, time datetime, foreign key(username) references users(username), primary key(username, item_type, item_id, time))`)
 	if err != nil && err.Error() != "table experiences already exists" {
 		return err
 	}
-	_, err = db.Exec(`create table item_cache (item_type text, item_id text, cover text, status text, title text, year_start int, year_end int, based_on text, genres text, runtime int, collection int, part int, ref_count int, primary key(item_type, item_id))`)
+	_, err = db.Exec(`create table item_cache (item_type text, item_id text, cover text, status text, title text, year_start int, year_end int, based_on text, genres text, runtime int, collection int, part int, ref_count int, episodes int, primary key(item_type, item_id))`)
 	if err != nil && err.Error() != "table item_cache already exists" {
 		return err
 	}
@@ -488,9 +488,9 @@ 		return err
 	}
 	defer db.Close()
 
-	_, err = db.Exec(`insert into item_cache values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+	_, err = db.Exec(`insert into item_cache values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
 	                  on conflict(item_type, item_id) do update set ref_count = ref_count + ?`,
-		itemType, itemId, itemInfo.Cover, itemInfo.Status, itemInfo.Title, itemInfo.YearStart, itemInfo.YearEnd, itemInfo.BasedOn, itemInfo.Genres, itemInfo.Runtime, itemInfo.Collection, itemInfo.Part, refs, refs)
+		itemType, itemId, itemInfo.Cover, itemInfo.Status, itemInfo.Title, itemInfo.YearStart, itemInfo.YearEnd, itemInfo.BasedOn, itemInfo.Genres, itemInfo.Runtime, itemInfo.Collection, itemInfo.Part, refs, itemInfo.Episodes, refs)
 	if err != nil {
 		return err
 	}
@@ -505,7 +505,7 @@ 		return err
 	}
 	defer db.Close()
 
-	db.Exec(`update item_cache set cover = ?, status = ?, title = ?, year_start = ?, year_end = ?, based_on = ?, genres = ?, runtime = ?, collection = ?, part = ? where item_type = ? and item_id = ?`, itemInfo.Cover, itemInfo.Status, itemInfo.Title, itemInfo.YearStart, itemInfo.YearEnd, itemInfo.BasedOn, itemInfo.Genres, itemInfo.Runtime, itemInfo.Collection, itemInfo.Part, itemType, itemId)
+	db.Exec(`update item_cache set cover = ?, status = ?, title = ?, year_start = ?, year_end = ?, based_on = ?, genres = ?, runtime = ?, collection = ?, part = ?, episodes = ? where item_type = ? and item_id = ?`, itemInfo.Cover, itemInfo.Status, itemInfo.Title, itemInfo.YearStart, itemInfo.YearEnd, itemInfo.BasedOn, itemInfo.Genres, itemInfo.Runtime, itemInfo.Collection, itemInfo.Part, itemInfo.Episodes, itemType, itemId)
 
 	return nil
 }
@@ -717,7 +717,7 @@ 	if filter != "" {
 		whereClause = "and c1.title like '%" + filter + "%'"
 	}
 
-	rows, err := db.Query(`select item_id, episodes_watched, cover, status, based_on, genres, title, year_start, year_end, substr(e.id, 1, pos-1) as series_id from wantlist w left join (select count(time) as episodes_watched, item_id as id, instr(item_id, '/') as pos from experiences where item_type = 'tvserie' and time != '0001-01-01 00:00:00+00:00' and username = ? group by substr(id, 1, pos-1)) e on item_id = series_id natural join item_cache c where item_type = 'tvserie' and username = ? `+whereClause+` order by title limit ?,18`, username, username, offset)
+	rows, err := db.Query(`select item_id, cover, status, based_on, genres, title, year_start, year_end, substr(e.id, 1, pos-1) as series_id, episodes from wantlist w left join (select item_id as id, instr(item_id, '/') as pos from experiences where item_type = 'tvserie' group by substr(id, 1, pos-1)) e on item_id = series_id natural join item_cache c where item_type = 'tvserie' and username = ? `+whereClause+` order by title limit ?,18`, username, offset)
 
 	if err != nil {
 		fmt.Fprintf(os.Stderr, "Select err: %v\n", err)
@@ -729,17 +729,39 @@ 	for rows.Next() {
 		var (
 			entry            datastructure.TvQueueEntry
 			episodes_watched *int
+			episodes_skipped *int
 			series_id        *int
 		)
-		err := rows.Scan(&entry.Id, &episodes_watched, &entry.Cover, &entry.Status, &entry.BasedOn, &entry.Genres, &entry.Title, &entry.YearStart, &entry.YearEnd, &series_id)
+		err := rows.Scan(&entry.Id, &entry.Cover, &entry.Status, &entry.BasedOn, &entry.Genres, &entry.Title, &entry.YearStart, &entry.YearEnd, &series_id, &entry.Episodes)
 		if err != nil {
 			fmt.Println("Scan error")
 			return datastructure.TvQueue{}, err
 		}
-		if episodes_watched == nil {
-			entry.WatchedEpisodes = 0
-		} else {
-			entry.WatchedEpisodes = *episodes_watched
+
+		if series_id != nil {
+			row := db.QueryRow(`select count(time) from experiences where item_type = 'tvserie' and username = ? and item_id like ? || '/%' and time != '0001-01-01 00:00:00+00:00'`, username, *series_id)
+			err = row.Scan(&episodes_watched)
+			if err != nil {
+				fmt.Println("Scan error")
+				return datastructure.TvQueue{}, err
+			}
+			row = db.QueryRow(`select count(time) from experiences where item_type = 'tvserie' and username = ? and item_id like ? || '/%' and time == '0001-01-01 00:00:00+00:00'`, username, *series_id)
+			err = row.Scan(&episodes_skipped)
+			if err != nil {
+				fmt.Println("Scan error")
+				return datastructure.TvQueue{}, err
+			}
+
+			if episodes_watched == nil {
+				entry.WatchedEpisodes = 0
+			} else {
+				entry.WatchedEpisodes = *episodes_watched
+			}
+			if episodes_skipped == nil {
+				entry.SkippedEpisodes = 0
+			} else {
+				entry.SkippedEpisodes = *episodes_skipped
+			}
 		}
 
 		tvQueue.List = append(tvQueue.List, entry)




diff --git a/libamuse/common.go b/libamuse/common.go
index 031c57de1d09eb0d685f27d0d11a8171afa04acf..8605e85dc1ff7fa0764b9f715babab9aa01e994e 100644
--- a/libamuse/common.go
+++ b/libamuse/common.go
@@ -120,6 +120,19 @@ 	show.SetOnWantList(isOnList)
 	return gott.Tuple(args), err
 }
 
+func updateCache(args ...interface{}) (interface{}, error) {
+	data := args[0].(*RequestData)
+	result := args[1].(*Result)
+
+	item := result.result.(datastructure.Item)
+
+	itemInfo := item.GetItemInfo()
+
+	err := db.UpdateCacheItem(item.GetItemType(), data.id, itemInfo)
+	return gott.Tuple(args), err
+}
+
+
 type RequestData struct {
 	id         string
 	etag       string




diff --git a/libamuse/film.go b/libamuse/film.go
index 52de5eaa24c1429fb45a6ec4af13db22eb238212..fef437b32c4f0be1bb962247ce33bd91e2f713f5 100644
--- a/libamuse/film.go
+++ b/libamuse/film.go
@@ -56,18 +56,6 @@ 	}
 	return gott.Tuple(args), nil
 }
 
-func updateCache(args ...interface{}) (interface{}, error) {
-	data := args[0].(*RequestData)
-	result := args[1].(*Result)
-
-	item := result.result.(datastructure.Item)
-
-	itemInfo := item.GetItemInfo()
-
-	err := db.UpdateCacheItem(item.GetItemType(), data.id, itemInfo)
-	return gott.Tuple(args), err
-}
-
 func renderFilm(args ...interface{}) interface{} {
 	result := args[1].(*Result)
 	film := result.result.(*tmdb.Film)




diff --git a/libamuse/serie.go b/libamuse/serie.go
index 7d9c0c54bfdd449762b5707c0096a7b38da57d3b..c3e4c9987a26369b2c855a64efdb0fd0d0c93a90 100644
--- a/libamuse/serie.go
+++ b/libamuse/serie.go
@@ -183,6 +183,7 @@ 		Bind(getSeasons).
 		Bind(getBasedOn).
 		Bind(mergeCredits).
 		Bind(isOnWantList).
+		Bind(updateCache).
 		Map(countAllEpisodes).
 		Bind(calculateProgress).
 		Bind(findNextEpisode).




diff --git a/static/style/style.css b/static/style/style.css
index 25fb2c011c8432d0b2dc232cae69d483a8606766..e91cdd77e84a39baa1850b5671a85232fd76198e 100644
--- a/static/style/style.css
+++ b/static/style/style.css
@@ -778,6 +778,10 @@ .height-_25 {
 	height: .25rem;
 }
 
+.height-_1 {
+	height: .1rem;
+}
+
 .height-fill {
 	height: 100%;
 }




diff --git a/templates/tvqueue.html b/templates/tvqueue.html
index 6549f900da3a9e2cbdc079c0145ad5ae1ba3e5c5..789ecfcbe7e88513c1bc8fd72894de96eb1558dc 100644
--- a/templates/tvqueue.html
+++ b/templates/tvqueue.html
@@ -57,26 +57,29 @@ 			
<div class="flex flex-row flex-wrap flex-justify-space flex-align-start"> {{range .Data.List}} <a href="/tvseries/{{.Id}}" class="decoration-none force-width-18 margin-tb-1 no-outline"> - <div class="flex"> - <div> - {{if .Cover}} - <img src="https://image.tmdb.org/t/p/w154{{.Cover}}" class="width-154px{{if .IsUnreleased "tvseries"}} bw{{end}}" /> - {{else}} - <img src="/static/img/tv_empty.webp" class="width-154px" /> - {{end}} - <!-- todo progress --> + <div> + <div class="flex"> + <div> + {{if .Cover}} + <img src="https://image.tmdb.org/t/p/w154{{.Cover}}" class="width-154px{{if .IsUnreleased "tvseries"}} bw{{end}}" /> + {{else}} + <img src="/static/img/tv_empty.webp" class="width-154px" /> + {{end}} + <!-- todo progress --> + </div> + <div class="margin-lr-1"> + <p class="sans">{{.Title}}</p> + <p class="sans font-_875 text-grey">{{.GetYears}}</p> + <p class="sans font-_875"> + {{.GetGenres $.Data.Genres}} + </p> + <p class="font-_875 text-grey"> + <!-- todo based on --> + </p> + <!-- todo if last episode < week old then NEW --> + </div> </div> - <div class="margin-lr-1"> - <p class="sans">{{.Title}}</p> - <p class="sans font-_875 text-grey">{{.GetYears}}</p> - <p class="sans font-_875"> - {{.GetGenres $.Data.Genres}} - </p> - <p class="sans font-_875 text-grey">Watched episodes: {{.WatchedEpisodes}}</p> - <p class="font-_875 text-grey"> - <!-- todo based on --> - </p> - <!-- todo if last episode < week old then NEW --> + <div class="bg-accent height-_1" style="width: {{.CalculateProgress}}%"> </div> </div> </a> diff --git a/tmdb/serie.go b/tmdb/serie.go index f223662c12d02ee01b20e9dc5cf2f67cbc346fcc..6bcde322b8470abecfaaaa0ac9fa04d3a9d02155 100644 --- a/tmdb/serie.go +++ b/tmdb/serie.go @@ -60,7 +60,7 @@ Last_air_date_str string `json:"last_air_date"` Last_air_date time.Time Last_episode_to_air Episode `json:"last_episode_to_air"` Name string - Number_of_episodes int `json:"number_of_episodes"` // todo to item_cache and to calculating + Number_of_episodes int `json:"number_of_episodes"` Original_name string `json:"original_name"` Overview string Poster_path string `json:"poster_path"` @@ -97,6 +97,11 @@ if len(years) > 1 { yearEnd, _ = strconv.ParseInt(years[1], 10, 32) } + episodes := 0 + for _, season := range s.Seasons { + episodes += len(season.Episodes) + } + itemInfo := datastructure.ItemInfo{ Cover: s.Poster_path, Status: s.Status, @@ -104,6 +109,7 @@ Title: s.Original_name, YearStart: int(yearStart), YearEnd: int(yearEnd), Genres: genres, + Episodes: episodes, // BasedOn }