main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. )
  9. var (
  10. KEEP_SRT bool = false
  11. )
  12. func scan_dir(dir string) ([]string, error) {
  13. var r []string = []string{}
  14. ls, err := os.ReadDir(dir)
  15. if err != nil {
  16. return nil, err
  17. }
  18. for _, elem := range ls {
  19. if elem.IsDir() {
  20. rs, err := scan_dir(filepath.Join(dir, elem.Name()))
  21. if err != nil {
  22. return nil, err
  23. }
  24. for _, e := range rs {
  25. r = append(r, filepath.Join(elem.Name(), e))
  26. }
  27. } else {
  28. if strings.HasSuffix(elem.Name(), ".srt") {
  29. if !KEEP_SRT {
  30. continue
  31. }
  32. r = append(r, elem.Name())
  33. continue
  34. }
  35. if strings.HasSuffix(elem.Name(), ".info") {
  36. continue
  37. }
  38. r = append(r, elem.Name())
  39. }
  40. }
  41. return r, nil
  42. }
  43. func main() {
  44. // Keeps
  45. if len(os.Args) != 1 {
  46. for idx, arg := range os.Args {
  47. if idx == 0 { // Skip program name
  48. continue
  49. }
  50. switch strings.ToLower(arg) {
  51. case "srt":
  52. KEEP_SRT = true
  53. }
  54. }
  55. if KEEP_SRT {
  56. fmt.Println("Keeping .srt's")
  57. }
  58. }
  59. actions, err := scan_dir(".")
  60. if err != nil {
  61. fmt.Println("Err:", err)
  62. return
  63. }
  64. fmt.Println("Found:", len(actions))
  65. for _, a := range actions {
  66. fmt.Println(a)
  67. out, err := os.Create(filepath.Base(a))
  68. if err != nil {
  69. fmt.Println("Err (Create out):", err)
  70. continue
  71. }
  72. in, err := os.Open(a)
  73. if err != nil {
  74. out.Close()
  75. err1 := os.Remove(filepath.Base(a))
  76. if err1 != nil {
  77. fmt.Println("Err (Cleanup):", err1)
  78. }
  79. fmt.Println("Err (Open in):", err)
  80. continue
  81. }
  82. _, err = io.Copy(out, in)
  83. if err != nil {
  84. in.Close()
  85. out.Close()
  86. err1 := os.Remove(filepath.Base(a))
  87. if err1 != nil {
  88. fmt.Println("Err (Cleanup):", err1)
  89. }
  90. fmt.Println("Err (Copy in -> out):", err)
  91. continue
  92. }
  93. err = os.RemoveAll(filepath.Dir(a))
  94. if err != nil {
  95. fmt.Println("Err (Remove):", err)
  96. }
  97. }
  98. }