-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrec.go
176 lines (154 loc) · 4.09 KB
/
rec.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package radigo
import (
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/mitchellh/cli"
"github.com/olekukonko/tablewriter"
"github.com/yyoshiki41/go-radiko"
"github.com/yyoshiki41/radigo/internal"
)
type recCommand struct {
ui cli.Ui
}
func (c *recCommand) Run(args []string) int {
var stationID, start, areaID, fileType string
f := flag.NewFlagSet("rec", flag.ContinueOnError)
f.StringVar(&stationID, "id", "", "id")
f.StringVar(&start, "start", "", "start")
f.StringVar(&start, "s", "", "start")
f.StringVar(&areaID, "area", "", "area")
f.StringVar(&areaID, "a", "", "area")
f.StringVar(&fileType, "output", AudioFormatAAC, "output")
f.StringVar(&fileType, "o", AudioFormatAAC, "output")
f.Usage = func() { c.ui.Error(c.Help()) }
if err := f.Parse(args); err != nil {
return 1
}
if stationID == "" {
c.ui.Error("StationID is empty.")
return 1
}
startTime, err := time.ParseInLocation(datetimeLayout, start, location)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Invalid start time format '%s': %s", start, err))
return 1
}
if fileType != AudioFormatAAC && fileType != AudioFormatMP3 {
c.ui.Error(fmt.Sprintf(
"Unsupported audio format: %s", fileType))
return 1
}
output, err := NewOutputConfig(
fmt.Sprintf("%s-%s", startTime.In(location).Format(datetimeLayout), stationID),
fileType)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to configure output: %s", err))
return 1
}
if err := output.SetupDir(); err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to setup the output dir: %s", err))
return 1
}
if output.IsExist() {
c.ui.Error(fmt.Sprintf(
"the output file already exists: %s", output.AbsPath()))
return 1
}
c.ui.Output("Now downloading.. ")
spin := spinner.New(spinner.CharSets[9], time.Second)
spin.Start()
defer spin.Stop()
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
client, err := getClient(ctx, areaID)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to construct a radiko Client: %s", err))
return 1
}
_, err = client.AuthorizeToken(ctx)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to get auth_token: %s", err))
return 1
}
go func() {
pg, err := client.GetProgramByStartTime(ctx, stationID, startTime)
if err != nil {
ctxCancel()
c.ui.Error(fmt.Sprintf(
"Failed to get the program: %s", err))
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"STATION ID", "TITLE"})
table.Append([]string{stationID, pg.Title})
fmt.Print("\n")
table.Render()
}()
uri, err := client.TimeshiftPlaylistM3U8(ctx, stationID, startTime)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to get playlist.m3u8: %s", err))
return 1
}
chunklist, err := radiko.GetChunklistFromM3U8(uri)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to get chunklist: %s", err))
return 1
}
aacDir, err := output.TempAACDir()
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to create the aac dir: %s", err))
return 1
}
defer os.RemoveAll(aacDir) // clean up
if err := internal.BulkDownload(chunklist, aacDir); err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to download aac files: %s", err))
return 1
}
concatedFile, err := ConcatAACFilesFromList(ctx, aacDir)
if err != nil {
c.ui.Error(fmt.Sprintf(
"Failed to concat aac files: %s", err))
return 1
}
var retErr error
switch output.AudioFormat() {
case AudioFormatAAC:
retErr = os.Rename(concatedFile, output.AbsPath())
case AudioFormatMP3:
retErr = ConvertAACtoMP3(ctx, concatedFile, output.AbsPath())
}
if retErr != nil {
c.ui.Error(fmt.Sprintf(
"Failed to output a result file: %s", retErr))
return 1
}
c.ui.Output(fmt.Sprintf("Completed!\n%s", output.AbsPath()))
return 0
}
func (c *recCommand) Synopsis() string {
return "Record a radiko program"
}
func (c *recCommand) Help() string {
return strings.TrimSpace(`
Usage: radigo rec [options]
Record a radiko program.
Options:
-id=name Station id
-start,s=201610101000 Start time
-area,a=name Area id
-output,o=aac Output file type (aac, mp3)
`)
}