This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
76 lines (68 loc) · 1.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"log"
"os"
"github.com/akamensky/argparse"
ui "github.com/gizak/termui/v3"
itunesapi "github.com/goulinkh/podcast-cli/itunes-api"
"github.com/goulinkh/podcast-cli/rss"
podcastcliui "github.com/goulinkh/podcast-cli/ui"
)
func main() {
parser := argparse.NewParser("podcast-cli", "CLI podcast player")
podcastSearchQuery := parser.String("s", "search", &argparse.Options{Required: false, Help: "your podcast's name"})
rssUrl := parser.String("r", "rss", &argparse.Options{Required: false, Help: "custom podcast rss source"})
offset := parser.Int("o", "offset", &argparse.Options{Required: false, Help: "play episode number"})
err := parser.Parse(os.Args)
if err != nil {
log.Fatalln("Error:", parser.Usage(err))
return
}
err = podcastcliui.InitUI()
if err != nil {
log.Fatal(err)
}
if podcastSearchQuery != nil && *podcastSearchQuery != "" {
podcasts, err := itunesapi.FindPodcasts(*podcastSearchQuery)
if err != nil {
log.Fatalln("Error: Failed to search for podcasts")
}
podcastsWidget := &podcastcliui.PodcastsUI{Podcasts: podcasts}
podcastsWidget.InitComponents()
podcastcliui.Show(podcastsWidget)
} else if rssUrl != nil && *rssUrl != "" {
episodes, err := rss.ParseEpisodes(*rssUrl)
if err != nil {
log.Fatalln("Error: Failed to get episodes from the url: " + *rssUrl)
}
episodesWidget := &podcastcliui.EpisodesUI{Episodes: episodes}
episodesWidget.InitComponents()
podcastcliui.Show(episodesWidget)
if offset != nil {
episodesWidget.Play(*offset)
}
} else {
genres, err := itunesapi.GetGenres()
if err != nil {
log.Fatal(err)
}
genreWidget := &podcastcliui.GenresUI{
Genres: genres,
}
genreWidget.InitComponents()
podcastcliui.Show(genreWidget)
}
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
cmd, err := podcastcliui.HandleKeyEvent(&e)
if err != nil {
log.Fatal(err)
}
if cmd == podcastcliui.Exit {
return
}
}
}
}