Skip to content

Instantly share code, notes, and snippets.

@z411
Last active June 5, 2018 10:02
Show Gist options
  • Save z411/04d528ec5e557b1acfe4f2225a781cec to your computer and use it in GitHub Desktop.
Save z411/04d528ec5e557b1acfe4f2225a781cec to your computer and use it in GitHub Desktop.
Trackma MAL XML exporter
#!/usr/bin/env python3
# Short script that loads a Trackma anime list and converts it
# to a MyAnimeList XML file. This way you can import your list
# in sites that support this format.
#
# Usage:
# ./trackma_export.py ~/.trackma/<user>.mal/anime.list
#
# It saves it as mal.xml in the working directory.
import sys
import pickle
import xml.etree.ElementTree as ET
animestatus = ["", "Watching", "Completed", "On-Hold", "Dropped", "", "Plan to Watch"]
def usage():
print("Tool to export a Trackma MAL anime list to a MyAnimeList XML file.")
print("Use it *ONLY* on MAL anime lists! They're located in ~/.trackma/<user>.mal/anime.list")
print()
print("Usage:")
print(" ./trackma_export.py ~/.trackma/<user>.mal/anime.list")
print("Example:")
print(" ./trackma_export.py ~/.trackma/ichigo.mal/anime.list")
def _date2str(date):
if date:
return date.strftime("%Y-%m-%d")
else:
return '0000-00-00'
def write_list(trackma_list):
try:
root = ET.Element("myanimelist")
for k, item in trackma_list.items():
child = ET.SubElement(root, "anime")
ET.SubElement(child, "series_animedb_id").text = str(item['id'])
ET.SubElement(child, "series_title").text = item['title']
ET.SubElement(child, "my_watched_episodes").text = str(item['my_progress'])
ET.SubElement(child, "my_start_date").text = _date2str(item['my_start_date'])
ET.SubElement(child, "my_finish_date").text = _date2str(item['my_start_date'])
ET.SubElement(child, "my_score").text = str(item['my_score'])
ET.SubElement(child, "my_status").text = animestatus[item['my_status']]
tree = ET.ElementTree(root)
tree.write("mal.xml", short_empty_elements=False)
print("MAL XML list saved to mal.xml")
except TypeError:
print("Invalid list.")
def main():
if len(sys.argv) < 2:
usage()
return
in_file = sys.argv[1]
trackma_list = None
try:
with open(in_file, 'rb') as f:
trackma_list = pickle.load(f)
except TypeError:
print("Not a Trackma list file.")
except FileNotFoundError:
print("File not found.")
if trackma_list:
write_list(trackma_list)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment