def getAllSongs(self): """ Updates ALL_SONGS.json with data from all songs """ allSongs = [] for fileName in sorted(os.listdir(JSON_FOLDER)): newSong = {} songID = nameToID(fileName) [title, artist] = idToData(songID) # tags = [] with open(os.path.join(JSON_FOLDER, fileName)) as dataFile: data = json.load(dataFile) # Song title, called label for jQuery autocomplete newSong["label"] = data["id"] newSong["artist"] = data["artist"] newSong["title"] = data["title"] newSong["value"] = data["id"] # URL friendly i.e. love_story - taylor_swift newSong["id"] = songID urlInfo = { "title": idToData(songID)[0], "artist": idToData(songID)[1] } newSong["url"] = "/song/{artist}/{title}".format(**urlInfo) allSongs.append(newSong) with open(ALL_SONGS_PATH, "w") as outfile: json.dump(allSongs, outfile, indent=2, sort_keys=True)
def getAllSongs(self): """ Updates allSongs.json with data from all songs """ allSongs = [] for fileName in sorted(os.listdir(JSON_FOLDER)): newSong = {} songID = nameToID(fileName) [title, artist] = idToData(songID) # tags = [] with open(os.path.join(JSON_FOLDER, fileName)) as data_file: data = json.load(data_file) newSong["label"] = data['title'] newSong["title"] = data['title'] newSong["artist"] = data['artist'] newSong["value"] = data['id'] # URL friendly i.e. love_story - taylor_swift newSong["id"] = songID newSong["id_artist"] = idToData(songID)[1] newSong["id_title"] = idToData(songID)[0] allSongs.append(newSong) with open('allSongs.json', 'w') as outfile: json.dump(allSongs, outfile, indent=2, sort_keys=True)
def toJSON(self, fileName): """ Text file of a song --> JSON file of a song """ textFile = os.path.join(self.textFolder, fileName) f = open(textFile, "r") lines = f.readlines() lines = [x.rstrip() for x in lines] data = {} songID = nameToID(fileName) [title, artist] = idToData(songID) data["title"] = title data["artist"] = artist data["id"] = songID data["lines"] = [] print title allChords = [] def updateAllChords(line): for chord in line.split(): # Chords with a bass note if "/" in chord: chord = chord.split("/")[0] if chord not in allChords: allChords.append(chord) linesIter = iter(lines) # Putting meta data in the file is pretty sketchy # Move to a db eventually #36 firstLine = lines[0] capo = "CAPO " if firstLine.startswith(capo): data["capo"] = firstLine.split(capo)[1] next(linesIter) for line in linesIter: if isLabel(line): data["lines"].append({"label": line}) elif isChordLine(line): while True: next_line = next(linesIter) lyrics = next_line if isLyricLine(next_line) else "" data["lines"].append({"lyrics": lyrics, "chord": line}) updateAllChords(line) line = next_line if isLabel(line): data["lines"].append({"label": line}) break elif not isChordLine(line): break elif line: data["lines"].append({"lyrics": line, "chord": ""}) # FIXME: should we preserve blank lines? data["allChords"] = allChords title = clean(title) artist = clean(artist) fileName = dataToName(title, artist, JSON) jsonFile = os.path.join(JSON_FOLDER, fileName) with open(jsonFile, "w") as outfile: json.dump(data, outfile, indent=2, sort_keys=True)
# Test to make sure the the /json and /text folders are in sync # We keep text files in case we need to manually edit the tabs import os from helpers import clean, dataToName, idToData from parser import TEXT_FOLDER, JSON_FOLDER allText = os.listdir(TEXT_FOLDER) allJSON = os.listdir(JSON_FOLDER) def strip(file): return file.split(".txt")[0] for i in xrange(len(allText)): [title, artist] = idToData(strip(allText[i])) title = clean(title) artist = clean(artist) fileName = dataToName(title, artist, "json") assert fileName in allJSON, fileName assert len(allText) == len(allJSON), \ "{} {}".format(len(allText), len(allJSON))
""" spaces => _ delete apostrophes """ import os from helpers import nameToID, idToData, dataToName def makeURLFriendly(string): return string.lower().replace("'", "").replace(" ", "_") newJSON = os.path.join(os.getcwd(), 'json2') """ TO DO adjust parser.py script so that we automatically name the JSON files with friendly names TO DO restructure the /data folder, the Python code doesn't necessarily need to live in static? maybe this can wait until we have the import functionality """ for fileName in os.listdir(newJSON): songID = nameToID(fileName) title = makeURLFriendly(idToData(songID)[0]) artist = makeURLFriendly(idToData(songID)[1]) newName = dataToName(title, artist, 'json') fullFile = os.path.join(newJSON, fileName) newFullFile = fullFile.replace(fileName, newName) os.rename(fullFile, newFullFile)
def toJSON(self, fileName): """ Text file of a song --> JSON file of a song """ textFile = os.path.join(textFolder, fileName) f = open(textFile, 'r') lines = f.readlines() lines = [x.rstrip() for x in lines] data = {} songID = nameToID(fileName) [title, artist] = idToData(songID) data['title'] = title data['artist'] = artist data['id'] = songID data['lines'] = [] print title allChords = [] def updateAllChords(line): for chord in line.split(): # Chords with a bass note if "/" in chord: chord = chord.split("/")[0] if chord not in allChords: allChords.append(chord) lines_iter = iter(lines) first_line = lines[0] capo = "CAPO " if first_line.startswith(capo): data["capo"] = first_line.split(capo)[1] next(lines_iter) for line in lines_iter: if isLabel(line): data['lines'].append({'label': line}) elif isChordLine(line): while True: next_line = next(lines_iter) lyrics = next_line if isLyricLine(next_line) else '' data['lines'].append({'lyrics': lyrics, 'chord': line}) updateAllChords(line) line = next_line if isLabel(line): data['lines'].append({'label': line}) break elif not isChordLine(line): break elif line: data['lines'].append({'lyrics': line, 'chord': ''}) # FIXME: should we preserve blank lines? data['allChords'] = allChords title = clean(title) artist = clean(artist) fileName = dataToName(title, artist, JSON) jsonFile = os.path.join(JSON_FOLDER, fileName) with open(jsonFile, 'w') as outfile: json.dump(data, outfile, indent=2, sort_keys=True)