Add CLI flags

This commit is contained in:
2024-12-30 06:03:25 -07:00
parent 5ed6b120d3
commit fa6008d854
3 changed files with 29 additions and 12 deletions

View File

@@ -5,5 +5,15 @@ Download images from reddit.
```bash
go mod tidy
go run main.go
go run main.go pics oldschoolcool thewaywewere MilitaryPorn EarthPorn
```
```bash
docker run --restart unless-stopped \
-v ${PWD}/subreddits:/data/subreddits \
ghcr.io/cwpearson/reimager --every 1800 --out-dir /data/subreddits \
pics \
EarthPorn \
oldschoolcool \
thewaywewere
```

23
main.go
View File

@@ -1,7 +1,10 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/cwpearson/reddit-images/rate_limit"
@@ -10,12 +13,16 @@ import (
func main() {
subreddits := []string{
"pics",
"oldschoolcool",
"thewaywewere",
"MilitaryPorn",
"EarthPorn",
// Define and parse flags
every := flag.Int64("every", 60*30, "Optional: Run every N seconds")
outDir := flag.String("out-dir", "subreddits", "Optional: Output directory path")
flag.Parse()
subreddits := flag.Args()
if len(subreddits) == 0 {
fmt.Fprintf(os.Stderr, "Error: At least one string argument is required\n\n")
fmt.Fprintf(os.Stderr, "Usage: %s [--every N] [--out-dir path] string1 [string2 ...]\n", os.Args[0])
os.Exit(1)
}
rl := rate_limit.NewRateLimit()
@@ -23,10 +30,10 @@ func main() {
for {
for _, subreddit := range subreddits {
r := reddit.NewReddit(rl, subreddit)
r.Get()
r.Get(*outDir)
}
when := time.Now().Add(time.Minute * time.Duration(30))
when := time.Now().Add(time.Second * time.Duration(*every))
log.Println("sleep until", when)
time.Sleep(time.Until(when))
}

View File

@@ -117,11 +117,11 @@ func getImage(rl *rate_limit.RateLimit, url, outDir, stem string) error {
return os.WriteFile(outPath, contents, 0644)
}
func (r *Reddit) Get() {
func (r *Reddit) Get(outDir string) {
var children []ChildData
var err error
outDir := filepath.Join("subreddits", r.subreddit)
outDir = filepath.Join(outDir, r.subreddit)
err = os.MkdirAll(outDir, 0755)
if err != nil && !os.IsExist(err) {
log.Println("ERROR: couldn't create out directory", outDir)
@@ -173,7 +173,7 @@ func (r *Reddit) Get() {
if len(parts) == 2 {
imgUrl := fmt.Sprintf("https://i.redd.it/%s.%s", meta.Id, parts[1])
stem := fmt.Sprintf("%d_%s_%d_%s", int64(child.Created), shortTitle, mi, meta.Id)
stem := fmt.Sprintf("%d_%s_%s", int64(child.Created), shortTitle, meta.Id)
if _, ok := existing[stem]; ok {
log.Println(stem, "already downloaded")