log-location 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/data/data/com.termux/files/usr/bin/python3
  2. import datetime
  3. import json
  4. import os
  5. import pytz
  6. import subprocess
  7. import sys
  8. import yaml
  9. def to_iso6801_basic(dt):
  10. return dt.isoformat().replace(':', '').replace('-', '') \
  11. .replace('+0000', 'Z')
  12. def compute(target_dir_path):
  13. loc_json = subprocess.check_output(['termux-location', '-p', 'network']) \
  14. .decode(sys.stdout.encoding)
  15. req_time = datetime.datetime.now(pytz.utc)
  16. loc_attr = yaml.load(loc_json)
  17. target_path = os.path.join(
  18. target_dir_path,
  19. '{}.yml'.format(to_iso6801_basic(req_time)),
  20. )
  21. with open(target_path, 'w') as f:
  22. f.write(yaml.dump(
  23. loc_attr,
  24. default_flow_style=False,
  25. ))
  26. def _init_argparser():
  27. import argparse
  28. argparser = argparse.ArgumentParser(description=None)
  29. argparser.add_argument(
  30. '--target-dir',
  31. metavar='PATH',
  32. dest='target_dir_path',
  33. default='.',
  34. )
  35. return argparser
  36. def main(argv):
  37. argparser = _init_argparser()
  38. try:
  39. import argcomplete
  40. argcomplete.autocomplete(argparser)
  41. except ImportError:
  42. pass
  43. args = argparser.parse_args(argv)
  44. compute(**vars(args))
  45. return 0
  46. if __name__ == "__main__":
  47. sys.exit(main(sys.argv[1:]))