Skip to content

Instantly share code, notes, and snippets.

@concubidated
Created September 3, 2020 14:02
Show Gist options
  • Save concubidated/4bd4ff4932078cba14b69a3a5de86acc to your computer and use it in GitHub Desktop.
Save concubidated/4bd4ff4932078cba14b69a3a5de86acc to your computer and use it in GitHub Desktop.
import sys, os
import mimetypes
import magic
import hashlib
import json
class Chart:
def __init__(self, chart_type, difficulty, level, author):
self.chart_type = chart_type
self.difficulty = difficulty
self.author = author
self.level = level
class Song:
def __init__(self, pack, title, subtitle, artist, credit, charts, hash):
self.pack = pack
self.title = title
self.subtitle = subtitle
self.artist = artist
self.credit = credit
self.charts = charts
self.hash = hash
songs = []
songs_path = "/data/Songs"
for pack in os.listdir(songs_path):
pack_dir = os.path.join(songs_path,pack)
if os.path.isdir(pack_dir):
for song in os.listdir(pack_dir):
song_dir = os.path.join(songs_path,pack_dir,song)
if os.path.isdir(song_dir):
for sm in os.listdir(song_dir):
if (sm.endswith(".sm")) or (sm.endswith(".ssc")):
charts = []
path = os.path.join(song_dir,sm)
hash = hashlib.sha1(open(path, 'rb').read()).hexdigest()
type = magic.Magic(mime=True, mime_encoding=True).from_file(path)
# Ran into improperly encoded SM files
if "unknown-8bit" not in type:
with open(path, "r") as simfile:
if "iso-8859-1" in type:
simfile = open(path, encoding='iso-8859-1')
for line in simfile:
if "#ARTIST:" in line:
artist = line.rstrip().split(":")[1][:-1]
if "#TITLE:" in line:
title = line.rstrip().split(":")[1][:-1]
if "#CREDIT:" in line:
credit = line.rstrip().split(":")[1][:-1]
if "#SUBTITLE:" in line:
subtitle = line.rstrip().split(":")[1][:-1]
if "dance-single:" in line:
author = simfile.readline().lstrip().split(':')[0]
difficulty = simfile.readline().lstrip().split(':')[0]
level = simfile.readline().lstrip().split(':')[0]
charts.append(Chart("dance-single", difficulty, level, author))
if "dance-double:" in line:
author = simfile.readline().lstrip().split(':')[0]
difficulty = simfile.readline().lstrip().split(':')[0]
level = simfile.readline().lstrip().split(':')[0]
charts.append(Chart("dance-double", difficulty, level, author))
songs.append(Song(pack, title, subtitle, artist, credit, charts, hash))
break
#Pretty print
#output = json.dumps(songs, default=lambda x: x.__dict__, indent=4)
output = json.dumps(songs, default=lambda x: x.__dict__)
outfile = open("output.json", "w")
outfile.write(output)
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment