-
-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to download all of the images (#28)
fix #25
- Loading branch information
1 parent
020a8b5
commit bdaa205
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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) | ||
} | ||
} | ||
} | ||
} | ||
} |