udm-dns.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. import re
  3. from datetime import timedelta
  4. import argparse
  5. import unificontrol
  6. from __future__ import print_function
  7. import os.path
  8. from google.auth.transport.requests import Request
  9. from google.oauth2.credentials import Credentials
  10. from google_auth_oauthlib.flow import InstalledAppFlow
  11. from googleapiclient.discovery import build
  12. from googleapiclient.errors import HttpError
  13. import math
  14. SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
  15. SPREADSHEET_ID = ''
  16. class ClientEntry:
  17. def __init__(self, entryNumber, hostname, address, macAddress, k8s):
  18. self.entryNumber = entryNumber
  19. self.hostname = hostname
  20. self.address = address
  21. self.macAddress = macAddress
  22. self.k8s = k8s
  23. def formatYamlEntry(self):
  24. entry = " {} {} # {}\n".format(self.hostname, self.address, self.macAddress)
  25. return entry
  26. def main():
  27. Subtitles = []
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument("--k8s", required=False, help="Render k8s hostname yaml", action='store_true')
  30. parser.add_argument("--hardware", required=False, help="Render hardware hostname yaml", action='store_true')
  31. parser.add_argument("--yamlfile", required=False, help="Filename to store rendered yaml", type=str)
  32. parser.add_argument("--unifi", required=False, help="Sync client addresses to unifi controller", action='store_true')
  33. parser.add_argument("--sheet", required=True, help="Google Sheet ID to read from", type=str)
  34. args = parser.parse_args()
  35. SPREADSHEET_ID = args.sheet
  36. print("Opening gsheet: {}".format(args.sheet))
  37. with open(args.file, "r+") as file:
  38. entryNumber = 0
  39. text = ""
  40. timestamp = ""
  41. for line in file:
  42. 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)
  43. if line == "\n":
  44. if (entryNumber > 0) and (timestamp is not None):
  45. s = SubtitleEntry(entryNumber, text, timestamp.groups()[0], timestamp.groups()[1])
  46. Subtitles.append(s)
  47. text = ""
  48. timestamp = ""
  49. entryNumber = 0
  50. elif re.match(r"^[0-9]+$", line):
  51. entryNumber = int(line.rstrip())
  52. elif m1:
  53. timestamp = m1
  54. else:
  55. text += line
  56. if args.inplace == True:
  57. args.outfile = args.file
  58. args.file = args.file + ".backup"
  59. with open(args.file, 'w') as o:
  60. print("Writing backup to " + args.file)
  61. for entry in Subtitles:
  62. print(entry.formatEntry(), file=o)
  63. print("Updating subtitle file: " + args.outfile)
  64. lastEntryNumber = Subtitles[-1].entryNumber
  65. with open(args.outfile, 'w') as o:
  66. print("Shifting all entries by {} seconds".format(args.seconds))
  67. for idx, entry in enumerate(Subtitles):
  68. if idx == 0:
  69. print("First subtitle line: {}".format(entry.text))
  70. if args.strip_first and entry.text != "":
  71. print("Stripping first line - {}".format(entry.text))
  72. entry.text = ""
  73. if idx == lastEntryNumber - 1:
  74. print("Last subtitle line: {}".format(entry.text))
  75. if args.strip_last and entry.text != "":
  76. print("Stripping last line - {}".format(entry.text))
  77. entry.text = ""
  78. entry.shift(args.seconds)
  79. print(entry.formatEntry(), file=o)
  80. if __name__ == "__main__":
  81. main()