Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 064c565

Browse files
authored
Merge pull request #19 from goulinkh/feat_rss_with_offset
Closes #14
2 parents aaa57b9 + 3f78583 commit 064c565

File tree

5 files changed

+83
-11
lines changed

5 files changed

+83
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
# binary
99
podcast-cli
10+
main
1011

1112
# Test binary, built with `go test -c`
1213
*.test

main.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
ui "github.com/gizak/termui/v3"
99

1010
itunesapi "github.com/goulinkh/podcast-cli/itunes-api"
11+
"github.com/goulinkh/podcast-cli/rss"
1112
podcastcliui "github.com/goulinkh/podcast-cli/ui"
1213
)
1314

1415
func main() {
1516

1617
parser := argparse.NewParser("podcast-cli", "CLI podcast player")
1718
podcastSearchQuery := parser.String("s", "search", &argparse.Options{Required: false, Help: "your podcast's name"})
19+
rssUrl := parser.String("r", "rss", &argparse.Options{Required: false, Help: "custom podcast rss source"})
20+
offset := parser.Int("o", "offset", &argparse.Options{Required: false, Help: "play episode number"})
1821
err := parser.Parse(os.Args)
1922
if err != nil {
2023
log.Fatalln("Error:", parser.Usage(err))
@@ -25,7 +28,26 @@ func main() {
2528
log.Fatal(err)
2629
}
2730

28-
if podcastSearchQuery == nil || *podcastSearchQuery == "" {
31+
if podcastSearchQuery != nil && *podcastSearchQuery != "" {
32+
podcasts, err := itunesapi.FindPodcasts(*podcastSearchQuery)
33+
if err != nil {
34+
log.Fatalln("Error: Failed to search for podcasts")
35+
}
36+
podcastsWidget := &podcastcliui.PodcastsUI{Podcasts: podcasts}
37+
podcastsWidget.InitComponents()
38+
podcastcliui.Show(podcastsWidget)
39+
} else if rssUrl != nil && *rssUrl != "" {
40+
episodes, err := rss.ParseEpisodes(*rssUrl)
41+
if err != nil {
42+
log.Fatalln("Error: Failed to get episodes from the url: " + *rssUrl)
43+
}
44+
episodesWidget := &podcastcliui.EpisodesUI{Episodes: episodes}
45+
episodesWidget.InitComponents()
46+
podcastcliui.Show(episodesWidget)
47+
if offset != nil {
48+
episodesWidget.Play(*offset)
49+
}
50+
} else {
2951
genres, err := itunesapi.GetGenres()
3052
if err != nil {
3153
log.Fatal(err)
@@ -35,15 +57,7 @@ func main() {
3557
}
3658
genreWidget.InitComponents()
3759
podcastcliui.Show(genreWidget)
38-
} else {
39-
// make a search
40-
podcasts, err := itunesapi.FindPodcasts(*podcastSearchQuery)
41-
if err != nil {
42-
log.Fatalln("Error: Failed to search for podcasts")
43-
}
44-
podcastsWidget := &podcastcliui.PodcastsUI{Podcasts: podcasts}
45-
podcastsWidget.InitComponents()
46-
podcastcliui.Show(podcastsWidget)
60+
4761
}
4862

4963
uiEvents := ui.PollEvents()

rss/parser.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package rss
2+
3+
import (
4+
"encoding/xml"
5+
"io/ioutil"
6+
"net/http"
7+
"strconv"
8+
9+
itunesapi "github.com/goulinkh/podcast-cli/itunes-api"
10+
)
11+
12+
type Rss struct {
13+
Channel struct {
14+
Item []struct {
15+
Title string `xml:"title"`
16+
PubDate string `xml:"pubDate"`
17+
Author string `xml:"author"`
18+
Description string `xml:"description"`
19+
Image struct {
20+
Href string `xml:"href,attr"`
21+
} `xml:"image"`
22+
Enclosure struct {
23+
URL string `xml:"url,attr"`
24+
Length int `xml:"length,attr"`
25+
Type string `xml:"type,attr"`
26+
} `xml:"enclosure"`
27+
Duration int `xml:"duration"`
28+
} `xml:"item"`
29+
} `xml:"channel"`
30+
}
31+
32+
func ParseEpisodes(rssUrl string) ([]*itunesapi.Episode, error) {
33+
resp, err := http.Get(rssUrl)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
rss, err := ioutil.ReadAll(resp.Body)
39+
var podcast Rss
40+
err = xml.Unmarshal(rss, &podcast)
41+
episodes := make([]*itunesapi.Episode, len(podcast.Channel.Item))
42+
for i, e := range podcast.Channel.Item {
43+
episodes[i] =
44+
&itunesapi.Episode{
45+
Artwork: e.Image.Href,
46+
AudioURL: e.Enclosure.URL,
47+
Description: e.Description,
48+
DurationInMilliseconds: e.Duration * 1000,
49+
Id: strconv.Itoa(i),
50+
ReleaseDate: e.PubDate,
51+
Title: e.Title,
52+
}
53+
}
54+
return episodes, nil
55+
}

ui/audio_player.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ func (ap *AudioPlayerWidget) Play(playlist []*itunesapi.Episode, index int) {
9494
ap.audioPositionWidget.Percent = (position * 100) / audioDuration
9595
}
9696
if ap.audioPositionWidget.Percent == 100 {
97-
audioplayer.Streamer.Close()
9897
ap.Play(playlist, index+1)
9998
return
10099
}

ui/episodes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func (e *EpisodesUI) HandleEvent(event *ui.Event) (Command, error) {
4242
}
4343
return Nothing, nil
4444
}
45+
func (e *EpisodesUI) Play(index int) {
46+
audioPlayerWidget.Play(e.Episodes, index)
4547

48+
}
4649
func (e *EpisodesUI) initGridWidget() error {
4750
if e.listWidget == nil {
4851
return errors.New("List widget is not initialized")

0 commit comments

Comments
 (0)