#!/usr/bin/env python import re from datetime import timedelta import argparse import unificontrol from __future__ import print_function import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError import math SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'] SPREADSHEET_ID = '' class ClientEntry: def __init__(self, entryNumber, hostname, address, macAddress, k8s): self.entryNumber = entryNumber self.hostname = hostname self.address = address self.macAddress = macAddress self.k8s = k8s def formatYamlEntry(self): entry = " {} {} # {}\n".format(self.hostname, self.address, self.macAddress) return entry def main(): Subtitles = [] parser = argparse.ArgumentParser() parser.add_argument("--k8s", required=False, help="Render k8s hostname yaml", action='store_true') parser.add_argument("--hardware", required=False, help="Render hardware hostname yaml", action='store_true') parser.add_argument("--yamlfile", required=False, help="Filename to store rendered yaml", type=str) parser.add_argument("--unifi", required=False, help="Sync client addresses to unifi controller", action='store_true') parser.add_argument("--sheet", required=True, help="Google Sheet ID to read from", type=str) args = parser.parse_args() SPREADSHEET_ID = args.sheet print("Opening gsheet: {}".format(args.sheet)) with open(args.file, "r+") as file: entryNumber = 0 text = "" timestamp = "" for line in file: m1 = re.match(r"^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})", line) if line == "\n": if (entryNumber > 0) and (timestamp is not None): s = SubtitleEntry(entryNumber, text, timestamp.groups()[0], timestamp.groups()[1]) Subtitles.append(s) text = "" timestamp = "" entryNumber = 0 elif re.match(r"^[0-9]+$", line): entryNumber = int(line.rstrip()) elif m1: timestamp = m1 else: text += line if args.inplace == True: args.outfile = args.file args.file = args.file + ".backup" with open(args.file, 'w') as o: print("Writing backup to " + args.file) for entry in Subtitles: print(entry.formatEntry(), file=o) print("Updating subtitle file: " + args.outfile) lastEntryNumber = Subtitles[-1].entryNumber with open(args.outfile, 'w') as o: print("Shifting all entries by {} seconds".format(args.seconds)) for idx, entry in enumerate(Subtitles): if idx == 0: print("First subtitle line: {}".format(entry.text)) if args.strip_first and entry.text != "": print("Stripping first line - {}".format(entry.text)) entry.text = "" if idx == lastEntryNumber - 1: print("Last subtitle line: {}".format(entry.text)) if args.strip_last and entry.text != "": print("Stripping last line - {}".format(entry.text)) entry.text = "" entry.shift(args.seconds) print(entry.formatEntry(), file=o) if __name__ == "__main__": main()