Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Need to offset some timestamps in a YouTube comment by a constant value? This program is for you.
- I made this to fix any comments with timestamps on YouTube livestreams that had been edited after the fact.
- """
- numbers = ["0","1","2","3","4","5","6","7","8","9"]
- #__________________________________________________________________________________________________________
- def convert(time, mode):
- if mode == "to seconds":
- #print("debugme:",time)
- if time[0] != '-': positive = True
- else:
- positive = False
- time = time[1:]
- time += ':' # Marks end of string
- hms = 0
- number = ""
- seconds = 0
- #print("debugpos:",positive)
- for char in time:
- if char in numbers: number += char
- elif char == ":":
- if hms == 0: seconds += int(number) * 3600 # Convert hours to seconds
- elif hms == 1: seconds += int(number) * 60 # Convert minutes to seconds
- else: seconds += int(number) # No need to convert seconds any further.
- hms += 1 # Increment to move onto h/m/s.
- number = "" # Reset
- if positive: return seconds
- return -seconds
- elif mode == "to h:m:s":
- hours = time // 3600
- minutes = str( (time % 3600) // 60 ).rjust(2, '0')
- seconds = str( (time % 3600) % 60 ).rjust(2, '0')
- return f"{hours}:{minutes}:{seconds}"
- #__________________________________________________________________________________________________________
- def query():
- filename = input("Enter file name: ")
- #filename = filename + ".txt"
- while True:
- offset = input("\nEnter offset: ") # Example input: "0:29:44"
- if offset[0] == "+":
- sign = "+"
- offset = offset[1:]
- elif offset[0] == "-":
- sign = "-"
- offset = offset[1:]
- else:
- print("INFO: ± ommitted, defaulting to negative.")
- sign = "-"
- # Error handling
- invalid = False
- if len(offset) != 7:
- print("Invalid! Timestamp must be in this form ±h:mm:ss\n")
- continue
- for char in offset:
- if (char not in numbers) and (char != ':'):
- print("Invalid! Timestamp must only contain numbers and colons (:) to seperate h/m/s.")
- invalid = True
- break
- if invalid: continue
- elif (offset[2] in ["6","7","8","9"]) or (offset[5] in ["6","7","8","9"]):
- print("\nInvalid! Minutes and seconds must not exceed 59.")
- continue
- else: break
- if sign == "+":
- return filename, convert(offset, 'to seconds')
- return filename, convert("-"+offset, 'to seconds')
- #__________________________________________________________________________________________________________
- def fix(line, offset):
- if not line:
- print()
- return line # Dealing with an empty line.
- line += ' ' # Marks end of line.
- index = 0
- next = 0
- text = ""
- flag = False
- to_fix = {}
- count = 1
- # Checking each character in the line.
- for i in range(len(line)-1): # Using range, so I can also index the end of the timestamp.
- if i < next: continue # Skips to end of timestamp, if previously found.
- elif i == next and text != "":
- text += line[i]
- next = i
- try:
- # DETECTING A NUMBER, INDICATIVE OF A TIMESTAMP
- if line[i] in numbers and line[i+7] not in numbers:
- # Testing for false positives ___________________________
- if line[i+1] != ':' or line [i+4] != ':':
- text += line[i]
- continue
- #________________________________________________________
- if text != "": # Avoids adding it at the beginning of the line.
- to_fix[str(count)] = text # Example: {"2", " - Game name (Trailer:"}
- text = "" # Reset
- count += 1
- to_fix[str(count)] = line[i:i+7] # Example: "1":"1:45:02"}
- count += 1
- next = i+7
- else: text += line[i] # Storing the text parts character by character.
- except IndexError: text += line[i]
- to_fix[str(count)] = text
- #print(to_fix)
- line = ""
- for key in to_fix:
- try:
- if to_fix[key][0] in numbers:
- ts_inSeconds = convert(to_fix[key],"to seconds")
- to_fix[key] = convert(ts_inSeconds + offset, "to h:m:s")
- except: pass
- line += to_fix[key]
- print(line) # Output to console.
- return line
- #__________________________________________________________________________________________________________
- def start():
- filename, offset = query()
- print(f"\nOffsetting by {offset}s...\n\n")
- with open((filename + ".txt"), 'r', encoding="utf-8") as f:
- lines = f.readlines()
- #print(lines)
- newlines = []
- count = 0
- for line in lines:
- count += 1
- with open((filename + "_new.txt"), 'a', encoding="utf-8") as new:
- new.write(fix( line.strip(), offset) +'\n')
- #__________________________________________________________________________________________________________
- if __name__ == "__main__":
- start()
Add Comment
Please, Sign In to add comment