def toText(self, url): (title, artist, data) = self.parseURL(url) fileName = dataToName(title, artist, TEXT) textFile = os.path.join(self.textFolder, fileName) print textFile with open(textFile, "w") as outfile: outfile.write(data.encode("utf-8")) return fileName
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)
""" 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)
# 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))
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)