Browse Source

Using a Timestamp class for the BB code augments is much simpler than what I was doing before

master
Yotsubaaa 2 years ago
parent
commit
181ef8679f
  1. 163
      bb_timestamps.rb
  2. 132
      kiwi_timestamps.rb

163
bb_timestamps.rb

@ -0,0 +1,163 @@
#!/usr/bin/ruby
# BB_TIMESTAMPS.RB makes pretty formatted timestamps for Xenforo2 forum
# posts. Write the timestamps in any (raw) text editor, e.g.
#
# 01:23:45 Something funny happens here at 1h, 23min, 45s
# 01:56:02 Something else happens
#
# ...with a space separating the time from the comment, and newlines
# separating each timestamp. Save the timestamps, e.g. to timestamps.txt.
# Then run this script:
# ./bb_timestamps.rb timestamps.txt
# and the timestamps will be formatted nicely, using a colored, monospaced
# Courier New fonr for the times so that they all line up pretty.
# Optionally (but most usefully!) if the first line of timestamp.txt is
# the link to a Youtube video/stream, the output timestamps will be
# formatted with hyperlinks that can be clicked by a reader to jump to
# that moment in the stream, e.g.
# https://www.youtube.com/watch?v=XXXXXXXXXXX
# will be hyperlinked in the formatted timestamps as
# https://www.youtube.com/watch?v=XXXXXXXXXXX&time_continue=...
# with the continue time determined automatically from each timestamp.
#TODO: Works for Youtube, but perhaps this script could be used for e.g.
# Bitchute or others by changing just this one line?
TIMESTAMP_PARAMETER = "&time_continue="
# Use a light blue color for the timestamps if no hyperlink
TIMESTAMP_COLOR = "rgb(84, 172, 210)"
# Use monospaced Courier New for the time font
TIMESTAMP_FONT = "courier new"
# A class to represent a TIMESTAMP with a given time and comment.
# The BB code formatting for the time may be built up piece-by-piece
# through the augment_... methods.
#
# Example:
# # Create a new, vanilla timestamp and just print it
# stamp = Timestamp.new("01:23:45", "Something funny happens")
# puts stamp #=> 01:23:45 Something funny happens
#
# # Augment with a monospaced Courier New font
# stamp.augment_font("courier new")
# puts stamp #=> [FONT=courier new]01:23:45[/FONT] Something
# # funny happens
#
# # Further augment with a pure red color rgb(255,0,0)
# stamp.augment_color("rgb(255, 0, 0)")
# puts stamp #=> [COLOR=rgb(255, 0, 0)][FONT=courier new]01:23:45
# # [/FONT][/COLOR] Something funny happens
#
class Timestamp
# Create a new timestamp with the given TIME and COMMENT.
def initialize(time, comment)
@time = time
@comment = comment
# No augment tags yet
@front_tags = []
@end_tags = []
end
# Return the printable string representation for this timestamp,
# including all augment tags.
def to_s()
@front_tags.join("") + @time + @end_tags.reverse.join("") + " " + @comment
end
# Augment this timestamp with a BB code to COLOR the text.
def augment_color(color)
@front_tags << "[COLOR=#{color}]"
@end_tags << "[/COLOR]"
end
# Augment this timestamp with a BB code to format the time
# text in a given FONT.
def augment_font(font)
@front_tags << "[FONT=#{font}]"
@end_tags << "[/FONT]"
end
# Augment this timestamp with a BB code to hyperlink to the
# given URL.
def augment_url(url)
@front_tags << "[URL='#{url}']"
@end_tags << "[/URL]"
end
end
# Convert a given TIMESTAMP (e.g. "hrs:mins:secs" to the _h_m_s format
# used by Youtube's time_continue parameter.
#
# Usage:
# to_hms("01:23:45") #=> "1h23m45s"
# to_hms("12:34") #=> "0h12m34s"
# to_hms("56") #=> "0h0m56s"
def to_hms(timestamp)
hrsminssecs = timestamp.split(":").map {|str| str.to_i}
# Prepend 0s if the timestamp doesn't explicitly state the hrs or mins
while hrsminssecs.length < 3
hrsminssecs.unshift(0)
end
hrs, mins, secs = hrsminssecs
"#{hrs}h#{mins}m#{secs}s"
end
# Assume that the file full of timestamps is the first command line arg
if ARGV.length < 1
raise(ArgumentError, "No timestamps file specified in command line args")
else
timestamps_file = File.open(ARGV.join(""))
end
# Extract the first line, skip any leading whitespace
first_line = timestamps_file.gets.strip
while first_line.empty?
first_line = timestamps_file.get.strip
end
# If the first non-whitespace line is a URL, use that for the video
# hyperlink. If not, color the timestamps so they stand out at least.
if first_line.start_with?("http")
url = first_line + TIMESTAMP_PARAMETER
elsif first_line.start_with?("www")
url = "https://" + first_line + TIMESTAMP_PARAMETER
else
url = nil
# In this case we read a timestamp line, so we process it here
time, comment = first_line.split(" ", 2)
stamp = Timestamp.new(time, comment)
stamp.augment_color(TIMESTAMP_COLOR)
stamp.augment_font(TIMESTAMP_FONT)
puts stamp.to_s + "\n"
end
# Process all of the timestamp lines (ignoring any blank lines)
timestamps_file.each do |line|
# Skip any preceding blank lines
line.strip!
if not line.empty?
time, comment = line.split(" ", 2)
stamp = Timestamp.new(time, comment)
if url.nil?
stamp.augment_color(TIMESTAMP_COLOR)
else
stamp.augment_url(url + to_hms(time))
end
stamp.augment_font(TIMESTAMP_FONT)
puts stamp.to_s + "\n"
end
end
# Done! Close the timestamp file.
timestamps_file.close()

132
kiwi_timestamps.rb

@ -1,132 +0,0 @@
#!/usr/bin/ruby
##
# KIWI_TIMESTAMPS.RB makes pretty formatted timestamps for Kiwi Farms posts.
# Created by Yotsubaaa, October 24 2019
#
# This script automates the process of making timestamps for YouTube streams
# and videos. Write the timestamps in any (raw) text editor, e.g.
#
# 01:23:45 Something funny happens here at 1h, 23min, 45s
# 01:56:02 Something else happens
#
# ...with spaces separating the time from the comment, and newlines
# separating each timestamp. Save the timestamps, e.g. to timestamps.txt.
# Then run this script:
# ./kiwi_timestamps.rb timestamps.txt
# and the timestamps will be formatted nicely, using a colored, monospaced
# Courier New font for the times so that they all line up pretty.
#
# Optionally (BUT most usefully!), if the first line of timestamp.txt
# is the link to the YouTube video/stream, the output timestamps will be
# formatted with hyperlinks that can be clicked by the reader to jump to
# that moment in the stream, e.g.
# https://www.youtube.com/watch?v=XXXXXXXXXXX
# will be hyperlinked in the formatted timestamps as
# https://www.youtube.com/watch?v=XXXXXXXXXXX&time_continue=____
# with the continue time determined automatically from each timestamp.
# Jumping to certain times in YouTube videos/streams uses this &time_continue
# parameter on the hyperlink. You might be able to hyperlink timestamps for
# other video sites (including BitChute once/if they implement this) using
# this same script by changing just this one line.
#TODO: Look into this maybe?
TIMESTAMP_PARAMETER = "&time_continue="
# Use a light blue color for the timestamps if no hyperlink
TIMESTAMP_COLOR = "rgb(84, 172, 210)"
# Use monospaced Courier New for the timestamps
TIMESTAMP_FONT = "courier new"
##
# Augment the given string STR with a BB code to COLOR the text.
#
# Usage:
# augment_color("test string", "rgb(255, 0, 0)")
# #=> "[COLOR=rgb(255, 0, 0)]test string[/COLOR]"
def augment_color(str, color)
"[COLOR=#{color}]#{str}[/COLOR]"
end
##
# Augment the given string STR with a BB code to format the text in the
# given FONT.
#
# Usage:
# augment_font("test string", "courier new")
# #=> "[FONT=courier new]test string[/FONT]"
def augment_font(str, font)
"[FONT=#{font}]#{str}[/FONT]"
end
##
# Augment the given string STR with a BB code to hyperlink to the given URL.
#
# Usage:
# augment_url("test string", "https://www.youtube.com")
# #=> "[URL='https://www.youtube.com']test string[/URL]"
def augment_url(str, url)
"[URL='#{url}']#{str}[/URL]"
end
##
# Convert a given TIMESTAMP (e.g. "hrs:mins:secs") to the _h_m_s format,
# as required for YouTube's time_continue parameter.
#
# Usage:
# to_hms("01:23:45") #=> "1h23m45s"
# to_hms("12:34") #=> "0h12m34s"
# to_hms("56") #=> "0h0m56s"
def to_hms(timestamp)
hrsminssecs = timestamp.split(":").map { |str| str.to_i }
# Prepend 0s if the timestamp doesn't explicitly state the hrs or mins
while hrsminssecs.length < 3
hrsminssecs.unshift(0)
end
hrs, mins, secs = hrsminssecs
"#{hrs}h#{mins}m#{secs}s"
end
# Read in the file full of timestamps (assumed to be first command line arg)
timestamps_file = File.open(ARGV[0])
# Extract the first line (skipping any leading white space)
first_line = timestamps_file.gets.strip
while first_line.empty?
first_line = timestamps_file.gets.strip
end
# If the first line is a link, use that for the url hyperlink.
# If not, color the timestamps so they stand out.
if first_line.start_with?("http")
url = first_line + TIMESTAMP_PARAMETER
augment = lambda { |time| augment_url(time, url + to_hms(time)) }
elsif first_line.start_with?("www")
url = "https://" + first_line + TIMESTAMP_PARAMETER
augment = lambda { |time| augment_url(time, url + to_hms(time)) }
else
augment = lambda { |time| augment_color(time, TIMESTAMP_COLOR) }
# Process this first timestamp line
time, comment = first_line.split(" ", 2)
formatted_time = augment_font(augment.call(time), TIMESTAMP_FONT)
puts formatted_time + " " + comment + "\n"
end
# Process all of the timestamp lines (ignoring any blank lines)
timestamps_file.each do |line|
line.strip!
if not line.empty?
time, comment = line.split(" ", 2)
formatted_time = augment_font(augment.call(time), TIMESTAMP_FONT)
puts formatted_time + " " + comment + "\n"
end
end
# Done. Close the timestamp file.
timestamps_file.close()
Loading…
Cancel
Save