Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/andrioid/md-social. markdown social media poster (experimental)
Something went wrong. Try again.
md-social parse.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132package main
import ( "context" "errors" "fmt" "os" "time"
"github.com/urfave/cli/v3")
func parse(ctx context.Context, cmd *cli.Command) error { verbose = cmd.Bool("verbose") dir := cmd.StringArg("dir") if dir == "" { return fmt.Errorf("no directory, given: %s", dir) } dryRun = cmd.Bool("dryrun") baseURL := cmd.String("base-url") pubMaxDays := cmd.Int("publish-max-days")
processing := []Processor{} publishing := []Publisher{}
// modules if bsky, err := NewBluesky(ctx, cmd); err == nil { publishing = append(publishing, bsky) } else { if !errors.Is(err, ErrModuleSkipped) { return err } fmt.Println("[module] bluesky not loaded") } if ogiModule, err := NewOgImageGenerator(cmd); err == nil { processing = append(processing, ogiModule) } else { if !errors.Is(err, ErrModuleSkipped) { return err } fmt.Println("[module] ogi-image not loaded") }
// File handling files, err := getMarkdownFiles(dir) if err != nil { return err }
filesTotal := len(files) filesSkipped := 0 filesPublished := 0 filesFailed := 0 filesMutated := 0
for _, file := range files { // 0. Open and parse mdf, err := ParseMarkdownFile(file, dir, baseURL) if err != nil { // Failed opening or parsing // TODO: Handle continue } // TODO: mdf.IsOlderThan(period) then skip for _, proc := range processing { if err = proc.Process(ctx, mdf); err != nil { if verbose { fmt.Println(err) }
if errors.Is(err, ErrFileSkipped) { filesSkipped++ continue } filesFailed++ } }
for _, pub := range publishing { // TODO: Skip if older than pubMaxDays if pubMaxDays > 0 { cutoff := time.Now().AddDate(0, 0, -pubMaxDays) if mdf.Date().Before(cutoff) { if verbose { fmt.Printf("[verbose] skipped (older than %d days): %s\n", pubMaxDays, mdf.Filename) } filesSkipped++ continue } }
if mdf.GetSocial(pub.PublisherID()) != "" { if verbose { fmt.Println("[verbose] already published", mdf.Filename) } continue } if dryRun { fmt.Println("[publishing] skipped", mdf.Filename) continue } if err = pub.Publish(ctx, mdf); err != nil { if verbose { fmt.Println(err) } if errors.Is(err, ErrFileSkipped) { filesSkipped++ continue } filesFailed++ } }
if mdf.PendingWrite && !dryRun { wfile, err := os.Create(file) if err != nil { return err } defer wfile.Close() _, err = mdf.WriteTo(wfile) if err != nil { return err } filesMutated++ } }
fmt.Printf("Found %d markdown files. %d published, %d skipped.\n", filesTotal, filesPublished, filesSkipped)
return nil}