Make pretty formatted timestamps using BB codes for Xenforo2 forum 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.

164 lines
5.0 KiB

  1. #!/usr/bin/ruby
  2. # BB_TIMESTAMPS.RB makes pretty formatted timestamps for Xenforo2 forum
  3. # posts. Write the timestamps in any (raw) text editor, e.g.
  4. #
  5. # 01:23:45 Something funny happens here at 1h, 23min, 45s
  6. # 01:56:02 Something else happens
  7. #
  8. # ...with a space separating the time from the comment, and newlines
  9. # separating each timestamp. Save the timestamps, e.g. to timestamps.txt.
  10. # Then run this script:
  11. # ./bb_timestamps.rb timestamps.txt
  12. # and the timestamps will be formatted nicely, using a colored, monospaced
  13. # Courier New fonr for the times so that they all line up pretty.
  14. # Optionally (but most usefully!) if the first line of timestamp.txt is
  15. # the link to a Youtube video/stream, the output timestamps will be
  16. # formatted with hyperlinks that can be clicked by a reader to jump to
  17. # that moment in the stream, e.g.
  18. # https://www.youtube.com/watch?v=XXXXXXXXXXX
  19. # will be hyperlinked in the formatted timestamps as
  20. # https://www.youtube.com/watch?v=XXXXXXXXXXX&time_continue=...
  21. # with the continue time determined automatically from each timestamp.
  22. #TODO: Works for Youtube, but perhaps this script could be used for e.g.
  23. # Bitchute or others by changing just this one line?
  24. TIMESTAMP_PARAMETER = "&time_continue="
  25. # Use a light blue color for the timestamps if no hyperlink
  26. TIMESTAMP_COLOR = "rgb(84, 172, 210)"
  27. # Use monospaced Courier New for the time font
  28. TIMESTAMP_FONT = "courier new"
  29. # A class to represent a TIMESTAMP with a given time and comment.
  30. # The BB code formatting for the time may be built up piece-by-piece
  31. # through the augment_... methods.
  32. #
  33. # Example:
  34. # # Create a new, vanilla timestamp and just print it
  35. # stamp = Timestamp.new("01:23:45", "Something funny happens")
  36. # puts stamp #=> 01:23:45 Something funny happens
  37. #
  38. # # Augment with a monospaced Courier New font
  39. # stamp.augment_font("courier new")
  40. # puts stamp #=> [FONT=courier new]01:23:45[/FONT] Something
  41. # # funny happens
  42. #
  43. # # Further augment with a pure red color rgb(255,0,0)
  44. # stamp.augment_color("rgb(255, 0, 0)")
  45. # puts stamp #=> [COLOR=rgb(255, 0, 0)][FONT=courier new]01:23:45
  46. # # [/FONT][/COLOR] Something funny happens
  47. #
  48. class Timestamp
  49. # Create a new timestamp with the given TIME and COMMENT.
  50. def initialize(time, comment)
  51. @time = time
  52. @comment = comment
  53. # No augment tags yet
  54. @front_tags = []
  55. @end_tags = []
  56. end
  57. # Return the printable string representation for this timestamp,
  58. # including all augment tags.
  59. def to_s()
  60. @front_tags.join("") + @time + @end_tags.reverse.join("") + " " + @comment
  61. end
  62. # Augment this timestamp with a BB code to COLOR the text.
  63. def augment_color(color)
  64. @front_tags << "[COLOR=#{color}]"
  65. @end_tags << "[/COLOR]"
  66. end
  67. # Augment this timestamp with a BB code to format the time
  68. # text in a given FONT.
  69. def augment_font(font)
  70. @front_tags << "[FONT=#{font}]"
  71. @end_tags << "[/FONT]"
  72. end
  73. # Augment this timestamp with a BB code to hyperlink to the
  74. # given URL.
  75. def augment_url(url)
  76. @front_tags << "[URL='#{url}']"
  77. @end_tags << "[/URL]"
  78. end
  79. end
  80. # Convert a given TIMESTAMP (e.g. "hrs:mins:secs" to the _h_m_s format
  81. # used by Youtube's time_continue parameter.
  82. #
  83. # Usage:
  84. # to_hms("01:23:45") #=> "1h23m45s"
  85. # to_hms("12:34") #=> "0h12m34s"
  86. # to_hms("56") #=> "0h0m56s"
  87. def to_hms(timestamp)
  88. hrsminssecs = timestamp.split(":").map {|str| str.to_i}
  89. # Prepend 0s if the timestamp doesn't explicitly state the hrs or mins
  90. while hrsminssecs.length < 3
  91. hrsminssecs.unshift(0)
  92. end
  93. hrs, mins, secs = hrsminssecs
  94. "#{hrs}h#{mins}m#{secs}s"
  95. end
  96. # Assume that the file full of timestamps is the first command line arg
  97. if ARGV.length < 1
  98. raise(ArgumentError, "No timestamps file specified in command line args")
  99. else
  100. timestamps_file = File.open(ARGV.join(""))
  101. end
  102. # Extract the first line, skip any leading whitespace
  103. first_line = timestamps_file.gets.strip
  104. while first_line.empty?
  105. first_line = timestamps_file.gets.strip
  106. end
  107. # If the first non-whitespace line is a URL, use that for the video
  108. # hyperlink. If not, color the timestamps so they stand out at least.
  109. if first_line.start_with?("http")
  110. url = first_line + TIMESTAMP_PARAMETER
  111. elsif first_line.start_with?("www")
  112. url = "https://" + first_line + TIMESTAMP_PARAMETER
  113. else
  114. url = nil
  115. # In this case we read a timestamp line, so we process it here
  116. time, comment = first_line.split(" ", 2)
  117. stamp = Timestamp.new(time, comment)
  118. stamp.augment_color(TIMESTAMP_COLOR)
  119. stamp.augment_font(TIMESTAMP_FONT)
  120. puts stamp.to_s + "\n"
  121. end
  122. # Process all of the timestamp lines (ignoring any blank lines)
  123. timestamps_file.each do |line|
  124. # Skip any preceding blank lines
  125. line.strip!
  126. if not line.empty?
  127. time, comment = line.split(" ", 2)
  128. stamp = Timestamp.new(time, comment)
  129. if url.nil?
  130. stamp.augment_color(TIMESTAMP_COLOR)
  131. else
  132. stamp.augment_url(url + to_hms(time))
  133. end
  134. stamp.augment_font(TIMESTAMP_FONT)
  135. puts stamp.to_s + "\n"
  136. end
  137. end
  138. # Done! Close the timestamp file.
  139. timestamps_file.close()