Skip to content

Commit

Permalink
rename the package and make hls a subpackage
Browse files Browse the repository at this point in the history
also, fix the cli tool
  • Loading branch information
otommod committed Dec 20, 2018
1 parent b2d33d6 commit 23278bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
15 changes: 7 additions & 8 deletions cmd/dam/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net/url"
"net/http"
"os"

"github.com/otommod/godam"
"github.com/otommod/go-dam/hls"
)

var (
Expand Down Expand Up @@ -35,17 +36,15 @@ func main() {
playlist := flag.CommandLine.Arg(0)
filename := flag.CommandLine.Arg(1)

u, err := url.Parse(playlist)
if err != nil {
log.Fatal(err)
}

fd, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}

err = godam.HLS(u, fd)
hlsClient := hls.Client{
Client: http.DefaultClient,
}
err = hlsClient.Download(context.TODO(), playlist, fd)
if err != nil {
log.Fatal(err)
}
Expand Down
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dam

import (
"net/http"
)

type HTTPError struct {
*http.Response
}

func (e HTTPError) Error() string {
return e.Status
}
29 changes: 11 additions & 18 deletions hls.go → hls/hls.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package godam
package hls

import (
"bytes"
Expand All @@ -11,18 +11,11 @@ import (
"time"

"github.com/grafov/m3u8"
"github.com/otommod/go-dam"
"golang.org/x/sync/errgroup"
)

type HTTPError struct {
*http.Response
}

func (e HTTPError) Error() string {
return e.Status
}

type HLS struct {
type Client struct {
Client *http.Client
}

Expand All @@ -43,15 +36,15 @@ func (w readCloserWithCancel) Close() error {
return w.ReadCloser.Close()
}

func (h HLS) ListVariants(uri string) ([]*m3u8.Variant, error) {
func (h Client) ListVariants(uri string) ([]*m3u8.Variant, error) {
r, err := h.Client.Get(uri)
if err != nil {
return nil, err
} else if r.StatusCode != 200 {
return nil, HTTPError{r}
return nil, dam.HTTPError{r}
}

playlist, playlistType, err := DecodeFrom(r.Body, uri)
playlist, playlistType, err := parseM3U8(r.Body, uri)
if err != nil {
return nil, err
} else if playlistType != m3u8.MASTER {
Expand All @@ -62,7 +55,7 @@ func (h HLS) ListVariants(uri string) ([]*m3u8.Variant, error) {
return master.Variants, nil
}

func (h HLS) readPlaylist(ctx context.Context, uri string) (*MediaPlaylist, error) {
func (h Client) readPlaylist(ctx context.Context, uri string) (*MediaPlaylist, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
Expand All @@ -79,11 +72,11 @@ func (h HLS) readPlaylist(ctx context.Context, uri string) (*MediaPlaylist, erro

r.Body = readCloserWithCancel{r.Body, cancel}
if r.StatusCode != 200 {
return nil, HTTPError{r}
return nil, dam.HTTPError{r}
}
defer r.Body.Close()

playlist, playlistType, err := DecodeFrom(r.Body, uri)
playlist, playlistType, err := parseM3U8(r.Body, uri)
if err != nil {
return nil, err
} else if playlistType != m3u8.MEDIA {
Expand All @@ -93,7 +86,7 @@ func (h HLS) readPlaylist(ctx context.Context, uri string) (*MediaPlaylist, erro
return playlist.(*MediaPlaylist), nil
}

func (h HLS) Download(ctx context.Context, uri string, dst io.Writer) error {
func (h Client) Download(ctx context.Context, uri string, dst io.Writer) error {
g, ctx := errgroup.WithContext(ctx)

segDataCh := make(chan io.ReadCloser)
Expand Down Expand Up @@ -180,7 +173,7 @@ func (h HLS) Download(ctx context.Context, uri string, dst io.Writer) error {

segData.Body = readCloserWithCancel{segData.Body, cancel}
if segData.StatusCode != 200 {
return HTTPError{segData}
return dam.HTTPError{segData}
}

select {
Expand Down
4 changes: 2 additions & 2 deletions m3u8.go → hls/m3u8.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package godam
package hls

import (
"bytes"
Expand Down Expand Up @@ -45,7 +45,7 @@ func splitKV(line string) []string {
})
}

func DecodeFrom(r io.Reader, playlistURI string) (playlist m3u8.Playlist, playlistType m3u8.ListType, err error) {
func parseM3U8(r io.Reader, playlistURI string) (playlist m3u8.Playlist, playlistType m3u8.ListType, err error) {
var playlistURL *url.URL
if playlistURL, err = url.Parse(playlistURI); err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion retry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package godam
package dam

import (
"net/http"
Expand Down

0 comments on commit 23278bb

Please sign in to comment.