Convert WriteFreely quotes to Instagram / PixelFed posts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.4 KiB

  1. /*
  2. * Copyright © 2021 A Bunch Tell LLC.
  3. *
  4. * This file is part of text-pic.
  5. *
  6. * text-pic is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package main
  11. import (
  12. "flag"
  13. "io/ioutil"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/writeas/web-core/log"
  19. "github.com/writefreely/text-pic"
  20. )
  21. var (
  22. outputFile = flag.String("o", "out.png", "Image output filename")
  23. font = flag.String("font", "serif", "Post font (options: \"serif\", \"sans\", \"mono\")")
  24. instance = flag.String("h", "", "WriteFreely instance hostname (e.g. \"pencil.writefree.ly\")")
  25. author = flag.String("u", "", "WriteFreely author username (for multi-user instances)")
  26. size = flag.String("size", "1024", "Image size, either a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
  27. )
  28. func main() {
  29. flag.Parse()
  30. // Validate input
  31. if *instance == "" {
  32. log.Info("Aborting. Instance name (-i instance.tld) is required")
  33. flag.Usage()
  34. os.Exit(1)
  35. }
  36. if !textpic.IsValidFont(*font) {
  37. log.Info("Invalid --font given. Options: \"serif\", \"sans\", \"mono\"")
  38. os.Exit(1)
  39. }
  40. // Parse image dimensions and validate
  41. var w, h int
  42. if strings.ContainsRune(*size, 'x') {
  43. parts := strings.Split(*size, "x")
  44. if len(parts) != 2 {
  45. log.Info("Invalid --size given. Must be a single number for a square (e.g. \"900\") or a combined width and height (e.g. \"1080x1920\")")
  46. os.Exit(1)
  47. }
  48. var err error
  49. w, err = strconv.Atoi(parts[0])
  50. if err != nil {
  51. log.Info("Unable to parse --size dimension '%s': %s", parts[0], err)
  52. os.Exit(1)
  53. }
  54. h, err = strconv.Atoi(parts[1])
  55. if err != nil {
  56. log.Info("Unable to parse --size dimension '%s': %s", parts[1], err)
  57. os.Exit(1)
  58. }
  59. } else {
  60. sq, err := strconv.Atoi(*size)
  61. if err != nil {
  62. log.Info("Unable to parse --size: %s", err)
  63. os.Exit(1)
  64. }
  65. w = sq
  66. h = sq
  67. }
  68. log.Info("Reading input...")
  69. in, err := ioutil.ReadAll(os.Stdin)
  70. if err != nil {
  71. log.Error("read: %s", err)
  72. os.Exit(1)
  73. }
  74. log.Info("Generating image...")
  75. start := time.Now()
  76. err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), w, h, *outputFile)
  77. if err != nil {
  78. log.Error("%s", err)
  79. os.Exit(1)
  80. }
  81. log.Info("Completed in %s", time.Since(start))
  82. }