Skip to content

Commit

Permalink
feat: add script to download all of the images (#28)
Browse files Browse the repository at this point in the history
fix #25
  • Loading branch information
jletey authored and nikitavoloboev committed Mar 3, 2019
1 parent 020a8b5 commit bdaa205
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions download-img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"

parser "github.com/nikitavoloboev/markdown-parser"
)

func getMdFiles(path string) []string {
var fileNames []string
files, err := filepath.Glob(path + "/*.md")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
fileNames = append(fileNames, f)
}
dir, err := ioutil.ReadDir(path + "/")
if err != nil {
log.Fatal(err)
}
for _, f := range dir {
if f.IsDir() && f.Name() != ".git" {
fileNames = append(fileNames, getMdFiles(path+"/"+f.Name())...)
}
}
return fileNames
}

func main() {
// Get all of the existing MD files
fileNames := getMdFiles(".")
// Get all of the image links and download them from all of the MD files
for _, f := range fileNames {
fileHandle, _ := os.Open("./" + f)
defer fileHandle.Close()
scanner := bufio.NewScanner(fileHandle)
for scanner.Scan() {
link := parser.ParseImageLink(scanner.Text())
if link != "" {
fmt.Println(link)
cmd := exec.Command("wget", link, "-P", "./imgs")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
}
}
}

0 comments on commit bdaa205

Please sign in to comment.