|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | + |
| 15 | + "golang.org/x/sync/semaphore" |
| 16 | +) |
| 17 | + |
| 18 | +func downloadAllEpisodes(maxConcurrent int) error { |
| 19 | + dir, err := mkdirSpongebob() |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + |
| 24 | + episodesUrls, _ := getEpisodes() |
| 25 | + |
| 26 | + // asynchronously download all episodes but max {maxConcurrent} episode at a time. |
| 27 | + // source: https://gist.github.com/AntoineAugusti/80e99edfe205baf7a094?permalink_comment_id=4088548#gistcomment-4088548 |
| 28 | + sem := semaphore.NewWeighted(int64(maxConcurrent)) |
| 29 | + ctx := context.TODO() |
| 30 | + var wg sync.WaitGroup |
| 31 | + |
| 32 | + start := time.Now() |
| 33 | + |
| 34 | + for i := 0; i < len(episodesUrls); i++ { |
| 35 | + wg.Add(1) |
| 36 | + |
| 37 | + go func(i int) { |
| 38 | + _ = sem.Acquire(ctx, 1) |
| 39 | + defer sem.Release(1) |
| 40 | + defer wg.Done() |
| 41 | + |
| 42 | + episodeUrl := episodesUrls[i] |
| 43 | + |
| 44 | + videoSource := extractVideo(episodeUrl) |
| 45 | + |
| 46 | + newFileName, err := getNewFileNameWithDir(dir, i, videoSource) |
| 47 | + if err != nil { |
| 48 | + fmt.Printf("Error while get new file name with dir: %v\n", err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + fmt.Printf("downloading\t%d %s\n", i, videoSource) |
| 53 | + |
| 54 | + if err := downloadFile(newFileName, videoSource); err != nil { |
| 55 | + fmt.Printf("failed\t%d %s: %v\n", i, videoSource, err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + fmt.Printf("success\t\t%d %s\n", i, videoSource) |
| 60 | + }(i) |
| 61 | + } |
| 62 | + |
| 63 | + wg.Wait() |
| 64 | + |
| 65 | + fmt.Printf("total time %v\n", time.Since(start)) |
| 66 | + |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func mkdirSpongebob() (string, error) { |
| 71 | + spongebobDir := filepath.Join(".", "spongebob") |
| 72 | + if err := os.MkdirAll(spongebobDir, os.ModePerm); err != nil { |
| 73 | + return "", err |
| 74 | + } |
| 75 | + |
| 76 | + return spongebobDir, nil |
| 77 | +} |
| 78 | + |
| 79 | +func getNewFileNameWithDir(dir string, index int, videoSource string) (string, error) { |
| 80 | + newFileName := fmt.Sprintf("%d-", index) |
| 81 | + newFileName = filepath.Join(dir, newFileName) |
| 82 | + |
| 83 | + videoSourceSplitted := strings.Split(videoSource, "/") |
| 84 | + if len(videoSourceSplitted) == 0 { |
| 85 | + return "", errors.New("error split video source") |
| 86 | + } |
| 87 | + |
| 88 | + videoSourceFileName := videoSourceSplitted[len(videoSourceSplitted)-1] |
| 89 | + newFileName += videoSourceFileName |
| 90 | + return newFileName, nil |
| 91 | +} |
| 92 | + |
| 93 | +func downloadFile(filepath string, videoUrl string) error { |
| 94 | + out, err := os.Create(filepath) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + defer out.Close() |
| 99 | + |
| 100 | + resp, err := http.Get(videoUrl) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + defer resp.Body.Close() |
| 105 | + |
| 106 | + if resp.StatusCode != http.StatusOK { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + _, err = io.Copy(out, resp.Body) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
0 commit comments