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.

87 lines
2.3 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/text-pic"
  19. "github.com/writeas/web-core/log"
  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("i", "write.as", "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. log.Info("Starting...")
  30. flag.Parse()
  31. // Validate input
  32. if !textpic.IsValidFont(*font) {
  33. log.Info("Invalid font given. Options: \"serif\", \"sans\", \"mono\"")
  34. os.Exit(1)
  35. }
  36. // Parse image dimensions and validate
  37. var w, h int
  38. if strings.ContainsRune(*size, 'x') {
  39. parts := strings.Split(*size, "x")
  40. if len(parts) != 2 {
  41. 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\")")
  42. os.Exit(1)
  43. }
  44. var err error
  45. w, err = strconv.Atoi(parts[0])
  46. if err != nil {
  47. log.Info("Unable to parse --size dimension '%s': %s", parts[0], err)
  48. os.Exit(1)
  49. }
  50. h, err = strconv.Atoi(parts[1])
  51. if err != nil {
  52. log.Info("Unable to parse --size dimension '%s': %s", parts[1], err)
  53. os.Exit(1)
  54. }
  55. } else {
  56. sq, err := strconv.Atoi(*size)
  57. if err != nil {
  58. log.Info("Unable to parse --size: %s", err)
  59. os.Exit(1)
  60. }
  61. w = sq
  62. h = sq
  63. }
  64. log.Info("Reading input...")
  65. in, err := ioutil.ReadAll(os.Stdin)
  66. if err != nil {
  67. log.Error("read: %s", err)
  68. os.Exit(1)
  69. }
  70. log.Info("Generating image...")
  71. start := time.Now()
  72. err = textpic.GenerateImage(textpic.NewContentOptions(*instance, *author, false, *font, string(in)), w, h, *outputFile)
  73. if err != nil {
  74. log.Error("%s", err)
  75. os.Exit(1)
  76. }
  77. log.Info("Completed in %s", time.Since(start))
  78. }