Skip to content

Commit

Permalink
retry caption upload 8 times (1+7)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfram77 committed Dec 20, 2018
1 parent a3602df commit cf6872d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 15 additions & 6 deletions upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -15,6 +16,7 @@ import (
// Global variables
//
var reTemplate = regexp.MustCompile("\\$\\{.*?\\}")
var retries = 8

//
// Functions
Expand Down Expand Up @@ -173,21 +175,28 @@ func uploadThumbnail(srv *youtube.Service, id string, fil io.ReadCloser) {
}

func uploadCaption(srv *youtube.Service, id string, lng string, fil io.ReadCloser) {
var err error
var res *youtube.Caption
c := &youtube.Caption{
Snippet: &youtube.CaptionSnippet{},
}
c.Snippet.VideoId = id
c.Snippet.Language = lng
c.Snippet.Name = lng
req := srv.Captions.Insert("snippet", c).Sync(true)
res, err := req.Media(fil).Do()
if err != nil {
if res != nil {
log.Fatalf("Error uploading caption: %v, %v", err, res.HTTPStatusCode)
for i := 0; i < retries; i++ {
req := srv.Captions.Insert("snippet", c).Sync(true)
res, err = req.Media(fil).Do()
if err == nil {
break
} else if res == nil {
log.Printf("Error uploading caption: %v", err)
} else {
log.Fatalf("Error uploading caption: %v", err)
log.Printf("Error uploading caption: %v, %v", err, res.HTTPStatusCode)
}
}
if err != nil {
os.Exit(1)
}
}

func addToPlaylistID(srv *youtube.Service, pid string, sta string, id string) {
Expand Down
3 changes: 0 additions & 3 deletions youtubeuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"os"
"strings"
"time"

"golang.org/x/oauth2"
"google.golang.org/api/youtube/v3"
Expand Down Expand Up @@ -107,13 +106,11 @@ func main() {
logf("Uploading file '%s'...\n", f.Video)
video := uploadVideo(service, videoFile, upload, parseInt(f.UploadChunk, 0), quitChan)
logf("Upload successful! Video ID: %v\n", video.Id)
time.Sleep(time.Second)
id = video.Id
} else if id != "" {
logf("Updating video %v...\n", id)
updateVideo(service, id, upload)
logf("Update successful!\n")
time.Sleep(time.Second)
}
// upload thumbnail
if id != "" && thumbnailFile != nil {
Expand Down

0 comments on commit cf6872d

Please sign in to comment.