Error popup and presence playback time

This commit is contained in:
Apologieze
2025-03-20 18:06:46 -04:00
parent f980c903ff
commit 09b33177c2
6 changed files with 88 additions and 28 deletions

View File

@@ -3,9 +3,10 @@
| Status | Priority | Task |
|----------------|-----------|-------------------------------------------------------------------------------|
| 📝 To do | 🟡 Medium | Make skipping option actually work |
| 📝 To do | 🟡 Medium | Custom discord presence |
| 📝 To do | 🟢 Low | Some anime like "Monster" can only get linked by searching for "Monster 2008" |
| 📝 To do | 🟢 Low | Add other sources for different languages |
| 🚧 In Progress | 🟡 Medium | |
| ✅ Done | 🟡 Medium | Custom discord presence |
| ✅ Done | 🔴 High | Fix "Last saved" message bug on first selected anime |
| ✅ Done | 🔴 High | Opening a new app check if already running instance |
| ✅ Done | 🔴 High | Add option to watch untracked last previous episode |
@@ -14,6 +15,6 @@
### Small todo
- [ ] Watching last episode should refresh data
- [x] Watching last episode should refresh data
- [ ] Having an indicator if new ep is out

View File

@@ -6,6 +6,7 @@ import (
"AnimeGUI/src/config"
"AnimeGUI/src/richPresence"
"AnimeGUI/verniy"
"errors"
"fmt"
"github.com/charmbracelet/log"
"os"
@@ -97,14 +98,15 @@ type AllAnimeIdData struct {
Name string
}
func OnPlayButtonClick(animeName string, animeData *verniy.MediaList, savingWatch bool) {
func OnPlayButtonClick(animeName string, animeData *verniy.MediaList, savingWatch bool) error {
if mpvPresent == false {
log.Error("MPV is not yet dl")
return
return errors.New("please wait for MPV to finish downloading")
}
if animeData == nil {
log.Error("Anime data is nil")
return
return errors.New("no selected anime")
}
var allAnimeId string
animeProgress := 0
@@ -116,12 +118,12 @@ func OnPlayButtonClick(animeName string, animeData *verniy.MediaList, savingWatc
allAnimeId = searchAllAnimeData(anilist.AnimeToRomaji(animeData.Media), animeData.Media.Episodes, animeProgress)
if allAnimeId == "" {
log.Error("Failed to get allAnimeId")
return
return errors.New("failed to link anime")
}
err, _ := curd.LocalUpdateAnime(databaseFile, animeData.Media.ID, allAnimeId, animeProgress, 0, 0, animeName)
if err != nil {
log.Error("Can't update database file", err)
return
return errors.New("can't update database file")
} else {
log.Info("Successfully updated database file")
}
@@ -142,20 +144,20 @@ func OnPlayButtonClick(animeName string, animeData *verniy.MediaList, savingWatc
url, err := curd.GetEpisodeURL(userCurdConfig, allAnimeId, animeProgress)
if err != nil {
log.Error(err)
return
return err
}
fmt.Println("--Got all urls")
finalLink := curd.PrioritizeLink(url)
if len(finalLink) < 5 {
log.Error("No valid link found")
return
return errors.New("couldn't find episode")
}
fmt.Println("Final Link:", finalLink)
mpvSocketPath, err := curd.StartVideo(finalLink, []string{}, fmt.Sprintf("%s - Episode %d", animeName, animeProgress))
if err != nil {
log.Error(err)
return
return err
}
fmt.Println("MPV Socket Path:", mpvSocketPath)
playingAnime := curd.Anime{AnilistId: animeData.Media.ID, AllanimeId: allAnimeId}
@@ -170,6 +172,7 @@ func OnPlayButtonClick(animeName string, animeData *verniy.MediaList, savingWatc
}
}
playingAnimeLoop(playingAnime, animeData, savingWatch)
return nil
}
func searchAllAnimeData(animeName string, epNumber *int, animeProgress int) string {
@@ -244,6 +247,7 @@ func playingAnimeLoop(playingAnime curd.Anime, animeData *verniy.MediaList, savi
}
presenceAnime := richPresence.PresenceAnime{Name: playingAnime.Title.English, Ep: playingAnime.Ep.Number + 1, ImageLink: *animeData.Media.CoverImage.Large, PlaybackTime: 0, Duration: playingAnime.Ep.Duration, TotalEp: playingAnime.TotalEpisodes}
var pauseCounter int
for {
time.Sleep(1 * time.Second)
timePos, err := curd.MPVSendCommand(playingAnime.Ep.Player.SocketPath, []interface{}{"get_property", "time-pos"})
@@ -273,8 +277,20 @@ func playingAnimeLoop(playingAnime curd.Anime, animeData *verniy.MediaList, savi
}
if timePos != nil && playingAnime.Ep.Duration != 0 {
if timing, ok := timePos.(float64); ok {
playingAnime.Ep.Player.PlaybackTime = int(timing + 0.5)
log.Infof("Video position: %d seconds", playingAnime.Ep.Player.PlaybackTime)
tempTime := int(timing + 0.5)
if tempTime == playingAnime.Ep.Player.PlaybackTime {
pauseCounter++
} else {
pauseCounter = 0
presenceAnime.Paused = false
richPresence.ResetTime(playingAnime.Ep.Player.PlaybackTime, playingAnime.Ep.Duration)
}
if pauseCounter == 2 {
log.Info("pause")
presenceAnime.Paused = true
}
playingAnime.Ep.Player.PlaybackTime = tempTime
log.Infof("Video position: %d seconds", tempTime)
if config.Setting.DiscordPresence {
presenceAnime.PlaybackTime = playingAnime.Ep.Player.PlaybackTime
richPresence.SetAnimeActivity(&presenceAnime)

20
src/errorPopUp.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"fyne.io/fyne/v2/dialog"
"unicode"
)
func showErrorPopUp(err error) {
errorText := capitalizeFirstChar(err.Error())
dialog.ShowInformation("Error", errorText, window)
}
func capitalizeFirstChar(s string) string {
if len(s) == 0 {
return s
}
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
return string(r)
}

View File

@@ -166,7 +166,10 @@ func selectCorrectLinking(allAnimeList []AllAnimeIdData, animeName string, anime
if tempAnime != nil {
localAnime = tempAnime
}
OnPlayButtonClick(animeName, animeSelected, true)
err = OnPlayButtonClick(animeName, animeSelected, true)
if err != nil {
showErrorPopUp(err)
}
}
dialogC.Resize(fyne.NewSize(600, 900))
@@ -301,7 +304,10 @@ func initMainApp() {
if animeName.Text == "" {
return
}
OnPlayButtonClick(animeName.Text, animeSelected, true)
err := OnPlayButtonClick(animeName.Text, animeSelected, true)
if err != nil {
showErrorPopUp(err)
}
})
playButton.IconPlacement = widget.ButtonIconTrailingText

View File

@@ -21,7 +21,10 @@ func initPlayMorePopUp() {
return
}
playMorePopUp.Hide()
OnPlayButtonClick(animeName.Text, animeSelected, false)
err := OnPlayButtonClick(animeName.Text, animeSelected, false)
if err != nil {
showErrorPopUp(err)
}
})
playPreviousButton.IconPlacement = widget.ButtonIconTrailingText

View File

@@ -15,6 +15,7 @@ type PresenceAnime struct {
ImageLink string
PlaybackTime int
Duration int
Paused bool
}
type PresenceState int
@@ -26,13 +27,14 @@ const (
const advert = "Free, open-source, crossplatform Anime streaming app :D"
var timeNow = time.Now()
var timeStartApp = time.Now()
func ResetTime(waitingTime int) {
go func() {
time.Sleep(time.Duration(waitingTime) * time.Second)
timeNow = time.Now()
}()
var timeBegin = timeStartApp
var timeEnd = timeBegin.Add(time.Minute * 30)
func ResetTime(PlaybackTime, Duration int) {
timeBegin = time.Now().Add(-time.Second * time.Duration(PlaybackTime))
timeEnd = timeBegin.Add(time.Second * time.Duration(Duration))
}
func InitDiscordRichPresence() {
if !config.Setting.DiscordPresence {
@@ -74,7 +76,7 @@ func SetMenuActivity() {
},*/
},
Timestamps: &client.Timestamps{
Start: &timeNow,
Start: &timeStartApp,
//End: &then,
},
})
@@ -90,11 +92,19 @@ func SetAnimeActivity(anime *PresenceAnime) {
SetMenuActivity()
return
}
var timeStamps *client.Timestamps = nil
if !anime.Paused {
timeStamps = &client.Timestamps{
Start: &timeBegin,
End: &timeEnd,
}
}
err := client.SetActivity(client.Activity{
Type: client.ActivityTypeWatching,
State: fmt.Sprintf("%s remaining", numberToTime(anime.Duration-anime.PlaybackTime)),
Details: fmt.Sprintf("%s Episode %d/%d", anime.Name, anime.Ep, anime.TotalEp),
State: fmt.Sprintf("Episode %d/%d %s", anime.Ep, anime.TotalEp, pausedToString(anime.Paused)),
Details: fmt.Sprintf("%s", anime.Name),
LargeImage: anime.ImageLink,
LargeText: anime.Name,
SmallImage: "main-image",
@@ -114,10 +124,7 @@ func SetAnimeActivity(anime *PresenceAnime) {
Url: "https://uwu.apologize.fr/preview",
},*/
},
Timestamps: &client.Timestamps{
Start: &timeNow,
//End: &then,
},
Timestamps: timeStamps,
})
if err != nil {
@@ -125,6 +132,13 @@ func SetAnimeActivity(anime *PresenceAnime) {
}
}
func pausedToString(paused bool) string {
if paused {
return "PAUSED"
}
return ""
}
func numberToTime(seconds int) string {
minutes := seconds / 60
seconds %= 60