-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrec.go
199 lines (162 loc) · 4.15 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package radikocast
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/olekukonko/tablewriter"
"github.com/yyoshiki41/go-radiko"
)
func RecProgram(stationID string, start string, areaID string, format string) error {
fmt.Printf("Rec %s %s\n", stationID, start)
r, err := recProgram(stationID, start, areaID, format)
if err != nil {
return fmt.Errorf("rec error: %w", err)
}
defer r.Dispose()
return nil
}
func RecAndUploadProgram(stationID string, start string, areaID string, bucket string, format string) error {
fmt.Printf("Rec %s %s\n", stationID, start)
r, err := recProgram(stationID, start, areaID, format)
if err != nil {
return fmt.Errorf("rec error: %w", err)
}
defer r.Dispose()
// put s3
fmt.Printf("bucket: %s\n", bucket)
err = putProgramToS3(r.audioPath, r.metadataPath, bucket)
if err != nil {
return fmt.Errorf("upload error: %w", err)
}
return nil
}
type recResult struct {
audioPath string
metadataPath string
tmpdir string
}
func (r *recResult) Dispose() {
os.RemoveAll(r.tmpdir)
}
func recProgram(stationID string, start string, areaID string, format string) (*recResult, error) {
startTime, err := time.ParseInLocation(datetimeLayout, start, location)
if err != nil {
return nil, err
}
td, err := ioutil.TempDir("", "radikocast")
if err != nil {
return nil, err
}
var ff string
switch format {
case "m4a":
ff = AudioFormatM4A
case "mp3":
ff = AudioFormatMP3
default:
return nil, fmt.Errorf("Bad format: %s", format)
}
output := OutputConfig{
DirFullPath: td,
FileBaseName: fmt.Sprintf("%s-%s", startTime.In(location).Format(datetimeLayout), stationID),
FileFormat: ff,
}
if err := output.SetupDir(); err != nil {
return nil, err
}
if output.IsExist() {
return nil, fmt.Errorf("Dup rec: %s", output.AbsPath())
}
spin := spinner.New(spinner.CharSets[9], time.Second)
spin.Start()
defer spin.Stop()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client, err := getClient(ctx, areaID)
if err != nil {
return nil, err
}
_, err = client.AuthorizeToken(ctx)
if err != nil {
return nil, err
}
uri, err := client.TimeshiftPlaylistM3U8(ctx, stationID, startTime)
if err != nil {
return nil, err
}
chunklist, err := radiko.GetChunklistFromM3U8(uri)
if err != nil {
return nil, err
}
aacDir, err := output.TempAACDir()
if err != nil {
return nil, err
}
defer os.RemoveAll(aacDir) // clean up
if err := BulkDownload(chunklist, aacDir); err != nil {
return nil, err
}
concatedFile, err := ConcatAACFilesFromList(ctx, aacDir)
if err != nil {
return nil, err
}
cv, err := NewConverter(ff)
if err != nil {
return nil, err
}
err = cv.Convert(ctx, concatedFile, output.AbsPath())
if err != nil {
return nil, err
}
// dump metadata
pg, err := client.GetProgramByStartTime(ctx, stationID, startTime)
if err != nil {
return nil, err
}
printInfo(stationID, pg.Title, pg.Info)
metadata, err := NewMetadata(pg, output.AbsPath())
if err != nil {
return nil, fmt.Errorf("metadata error: %w", err)
}
metadataPath := strings.Replace(output.AbsPath(),
fmt.Sprintf(".%s", output.AudioExt()), ".json", 1)
if err := writeJson(&metadata, metadataPath); err != nil {
return nil, fmt.Errorf("failed to write metadata: %w", err)
}
return &recResult{
audioPath: output.AbsPath(),
metadataPath: metadataPath,
}, nil
}
func printInfo(stationID, title, desc string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"STATION ID", "TITLE", "DESC"})
table.Append([]string{stationID, title, desc})
fmt.Print("\n")
table.Render()
}
func writeJson(data interface{}, dst string) error {
f, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
enc := json.NewEncoder(f)
return enc.Encode(data)
}
func putProgramToS3(audioPath string, metadataPath string, bucket string) error {
storage := NewS3(bucket)
err := storage.PutObjectFromFile(audioPath, "audio/mpeg")
if err != nil {
return err
}
err = storage.PutObjectFromFile(metadataPath, "application/json")
if err != nil {
return err
}
return nil
}