Created
February 23, 2023 14:10
-
-
Save kirugan/a38304dc3765da409a86446ab6fa4687 to your computer and use it in GitHub Desktop.
Skyeng lessons downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
"regexp" | |
"strings" | |
"sync" | |
) | |
// change this to your dir | |
const lessonsDir = "/Users/home/Documents/skyeng_lessons" | |
var semaphore = make(chan struct{}, 10) | |
func main() { | |
// same | |
fd, err := os.Open("/Users/home/Desktop/ws/tools/tmp/lessons.txt") | |
if err != nil { | |
panic(err) | |
} | |
sc := bufio.NewScanner(fd) | |
var wg sync.WaitGroup | |
for sc.Scan() { | |
wg.Add(1) | |
url := sc.Text() | |
go func() { | |
defer wg.Done() | |
semaphore <- struct{}{} | |
downloadFile(url) | |
<-semaphore | |
}() | |
} | |
wg.Wait() | |
fmt.Println("Finished") | |
if err := sc.Err(); err != nil { | |
panic(err) | |
} | |
} | |
var re = regexp.MustCompile("\\d{4}/\\d{2}/\\d{2}") | |
func downloadFile(url string) { | |
defer func() { | |
if r := recover(); r != nil { | |
panic(fmt.Errorf("%v (url=%s)", r, url)) | |
} | |
}() | |
fmt.Println("Downloading", url) | |
dateStr := re.FindStringSubmatch(url)[0] | |
dateStr = strings.ReplaceAll(dateStr, "/", "-") | |
filename := fmt.Sprintf("skyeng-%s.mp3", dateStr) | |
path := lessonsDir + "/" + filename | |
fd, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666) | |
if err != nil { | |
panic(err) | |
} | |
resp, err := http.Get(url) | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
_, err = io.Copy(fd, resp.Body) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script expects txt file where each line corresponds to some URL