Skip to content

Commit

Permalink
feat ✨: hotfix proxying invidious apicalls, refacto search functions
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Hang <[email protected]>
  • Loading branch information
Banh-Canh committed Oct 1, 2024
1 parent 8b0f3f4 commit 2ba47b7
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ builds:
- arm64
- arm
ldflags:
- -s -w -X github.com/Banh-Canh/ytui/cmd.version=v{{.Version}}
- -s -w -X github.com/Banh-Canh/ytui/cmd.version=v{{- .Version }}
archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
Expand Down
2 changes: 1 addition & 1 deletion cmd/query_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ will be stored in there.`,
os.Exit(0)
}
utils.Logger.Info("Videos found in history.", zap.Int("video_count", len(result)))
selectedVideo, err := youtube.YoutubeResultMenu(result)
selectedVideo, err := youtube.YoutubeResultMenu(result, viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/query_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Press enter to run any of the videos.`,
}

utils.Logger.Info("Videos found.", zap.Int("video_count", len(*result)))
selectedVideo, err := youtube.YoutubeResultMenu(*result)
selectedVideo, err := youtube.YoutubeResultMenu(*result, viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/query_subscribed.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ It will also only pick from the 50 most relevants subscribed channels in your Yo
}
}
utils.Logger.Info("Retrieved videos from subscribed channels.", zap.Int("video_count", len(*result)))
selectedVideo, err := youtube.YoutubeResultMenu(*result)
selectedVideo, err := youtube.YoutubeResultMenu(*result, viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
os.Exit(0)
Expand Down
2 changes: 1 addition & 1 deletion cmd/show_suscribed.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ It will also only pick from the 50 most relevants subscribed channels in your Yo
channelList = viper.GetStringSlice("channels.subscribed")
utils.Logger.Info("Retrieved local subscribed channels.", zap.Int("channel_count", len(channelList)))
}
channels, err := youtube.GetAllChannelsInfo(channelList)
channels, err := youtube.GetAllChannelsInfo(channelList, viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Fatal("Failed to get all channels data.", zap.Error(err))
os.Exit(1)
Expand Down
16 changes: 11 additions & 5 deletions pkg/youtube/fzf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func fetchDescriptionsInBackground(
videoData []SearchResultItem,
descriptionCache map[string]string,
cacheLock *sync.RWMutex,
proxyURLString string,
) {
go func() {
for {
Expand All @@ -40,7 +41,7 @@ func fetchDescriptionsInBackground(
}

// Fetch the video description with retries
if err := fetchAndCacheDescription(video, descriptionCache, cacheLock); err != nil {
if err := fetchAndCacheDescription(video, descriptionCache, cacheLock, proxyURLString); err != nil {
utils.Logger.Error("Failed to fetch description.", zap.Error(err))
continue
}
Expand Down Expand Up @@ -73,9 +74,14 @@ func cleanDescription(description string) string {
}

// Fetches a single video description and stores it in cache.
func fetchAndCacheDescription(video SearchResultItem, descriptionCache map[string]string, cacheLock *sync.RWMutex) error {
func fetchAndCacheDescription(
video SearchResultItem,
descriptionCache map[string]string,
cacheLock *sync.RWMutex,
proxyURLString string,
) error {
for {
videoInfo, err := SearchVideoInfo(video.VideoID)
videoInfo, err := SearchVideoInfo(video.VideoID, proxyURLString)
if err != nil {
utils.Logger.Info("Fetching description failed. Retrying...", zap.String("videoTitle", video.Title), zap.Error(err))
}
Expand Down Expand Up @@ -139,13 +145,13 @@ func getVideoPreview(video SearchResultItem, descriptionCache map[string]string,
}

// Handles the interactive menu for video selection. Powered by fzf-like
func YoutubeResultMenu(videoData []SearchResultItem) (SearchResultItem, error) {
func YoutubeResultMenu(videoData []SearchResultItem, proxyURLString string) (SearchResultItem, error) {
// Cache to store video descriptions
descriptionCache := make(map[string]string)
cacheLock := sync.RWMutex{} // For thread-safe cache access

// Start background fetching of descriptions
fetchDescriptionsInBackground(videoData, descriptionCache, &cacheLock)
fetchDescriptionsInBackground(videoData, descriptionCache, &cacheLock, proxyURLString)

utils.Logger.Info("Opening search menu.")
idx, err := fuzzyfinder.Find(
Expand Down
Loading

0 comments on commit 2ba47b7

Please sign in to comment.