history_converter.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # MadHelix is a Java Swing-based GUI frontend for SoundHelix.
  3. # Copyright (C) 2017-2019, 2023 UltrasonicMadness
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 3 only,
  7. # as published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import json
  17. from datetime import datetime, timezone
  18. from time import mktime
  19. from xml.etree import ElementTree
  20. if __name__ == "__main__":
  21. json_input_path = "history.json"
  22. xml_output_path = "history.xml"
  23. madhelix_history = ElementTree.Element("madhelix-history")
  24. with open(json_input_path) as json_input:
  25. for json_entry in json.load(json_input):
  26. current_date = json_entry["date"]
  27. local_time = datetime(current_date["year"], current_date["month"],
  28. current_date["day"], hour=current_date["hour"],
  29. minute=current_date["minute"],
  30. second=current_date["second"])
  31. timestamp = mktime(local_time.astimezone(timezone.utc).timetuple())
  32. xml_entry = ElementTree.Element("entry", name=json_entry["name"],
  33. style=json_entry["style"], length=str(json_entry["length"]),
  34. timestamp=str(int(timestamp)))
  35. madhelix_history.append(xml_entry)
  36. output_tree = ElementTree.ElementTree(madhelix_history)
  37. ElementTree.indent(output_tree)
  38. output_tree.write(xml_output_path)