working without api key

This commit is contained in:
pika 2024-12-12 11:37:43 +01:00
parent b97b89efb8
commit 999376e5f7
5 changed files with 371 additions and 166 deletions

View file

@ -2,7 +2,7 @@ package tui
import (
"fmt"
"log"
// "log"
"strings"
"time"
@ -19,7 +19,7 @@ type videoItem struct {
// Implement list.Item for videoItem
func (v videoItem) Title() string { return v.Video.Title }
func (v videoItem) Description() string { return fmt.Sprintf("%s | %s | %s | %s", v.Video.Channel, v.Video.Duration, v.Video.UploadDate, v.Video.Views) }
func (v videoItem) Description() string { return fmt.Sprintf("%s | %s | %s", v.Video.Channel, v.Video.Duration, v.Video.UploadDate) }
func (v videoItem) FilterValue() string { return v.Video.Title }
type model struct {
@ -31,6 +31,13 @@ type model struct {
width int
height int
clock time.Time
isLoading bool
spinner spinner
}
type spinner struct {
frames []string
current int
}
// Initial model setup
@ -50,6 +57,10 @@ func initialModel() model {
clock: time.Now(),
width: 80, // Default width
height: 24, // Default height
spinner: spinner{
frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
current: 0,
},
}
}
@ -85,33 +96,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.showSearch {
query := m.searchBar.Value()
if query == "" {
return m, nil // Ignore empty queries
}
// Fetch video results
videos, err := scraper.FetchVideos(query)
if err != nil {
m.err = err
log.Printf("Error fetching videos: %v", err)
return m, nil
}
// Populate the results list
items := make([]list.Item, len(videos))
for i, v := range videos {
items[i] = videoItem{Video: v}
}
m.videos = videos
m.results.SetItems(items)
m.showSearch = false
m.searchBar.Blur()
} else {
// Handle video selection
if i := m.results.Index(); i != -1 {
selectedVideo := m.videos[i]
cmd := player.PlayVideo(selectedVideo.URL)
return m, tea.ExecProcess(cmd, nil)
}
m.isLoading = true
// Return both the loading animation and the search command
return m, tea.Batch(
tickCmd(),
func() tea.Msg {
videos, err := scraper.FetchVideos(query)
if err != nil {
return searchErrorMsg{err: err}
}
return searchResultMsg{videos: videos}
},
)
}
// Handle video selection
if i := m.results.Index(); i != -1 {
selectedVideo := m.videos[i]
cmd := player.PlayVideo(selectedVideo.URL)
return m, tea.ExecProcess(cmd, nil)
}
return m, nil
}
@ -121,7 +127,27 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.height = msg.Height
m.results.SetSize(msg.Width, msg.Height-3) // Leave space for search bar
case searchResultMsg:
m.isLoading = false
items := make([]list.Item, len(msg.videos))
for i, v := range msg.videos {
items[i] = videoItem{Video: v}
}
m.videos = msg.videos
m.results.SetItems(items)
m.showSearch = false
m.searchBar.Blur()
return m, nil
case searchErrorMsg:
m.isLoading = false
m.err = msg.err
return m, nil
case time.Time:
if m.isLoading {
m.spinner.current = (m.spinner.current + 1) % len(m.spinner.frames)
}
m.clock = msg
return m, tickCmd()
}
@ -148,11 +174,14 @@ func (m model) View() string {
s.WriteString(clock + "\n\n")
}
// Always show search bar at the top
// Show search bar with loading animation if searching
searchText := "Press '/' to search"
if m.showSearch {
searchText = m.searchBar.View()
}
if m.isLoading {
searchText += " " + m.spinner.frames[m.spinner.current] + " Searching..."
}
s.WriteString(searchText + "\n\n")
// Show results
@ -177,3 +206,11 @@ func tickCmd() tea.Cmd {
return t
})
}
type searchResultMsg struct {
videos []scraper.Video
}
type searchErrorMsg struct {
err error
}