// Example script for downloading recording files
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"github.com/plivo/plivo-go/v7"
)
var (
AuthID = "<auth_id>"
AuthToken = "<auth_token>"
)
func main() {
client, err := plivo.NewClient(AuthID, AuthToken, &plivo.ClientOptions{})
if err != nil {
fmt.Println("Error", err.Error())
return
}
response, err := client.Recordings.List(
plivo.RecordingListParams{
AddTimeGreaterThan: "2023-04-01 00:00:00",
AddTimeLessThan: "2023-04-30 00:00:00",
Offset: 0,
Limit: 5,
},
)
if err != nil {
fmt.Println("Error", err.Error())
return
}
fmt.Printf("Found %d recordings.\n", len(response.Objects))
// Directory where the recordings will be saved
os.MkdirAll("recordings", os.ModePerm)
for _, recording := range response.Objects {
fmt.Println("Downloading recording: ", recording.RecordingURL)
filePath := filepath.Join("recordings", recording.RecordingID+recording.RecordingFormat)
err = downloadFile(filePath, recording.RecordingURL)
if err != nil {
fmt.Println("Error downloading file: ", err)
} else {
fmt.Println("Downloaded file to: ", filePath)
}
}
}
func downloadFile(filepath string, url string) error {
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
return err
}