def kupletter_to_ass(username, password, outfilename):
    sourcefile = "data_2017.txt"
    allowEmptyLines = True

    lyrics = kuplett_parser.get_all_lyrics(sourcefile, username, password)

    #parse read text and write to an ASS file
    ass_parser = Raw_to_ass_parser(
        30, 1)  #increment 1 second for each new line. Start at 30 seconds.

    # Load dictionary for mapping singers
    data = kuplett_parser.load_data(sourcefile)
    ass_parser.style_dictionary = data.get("dictionary")
    if "multilinesplitter" in data.get("meta").keys():
        ass_parser.multi_line_keyword = data.get("meta").get(
            "multilinesplitter")

    delimiter = ':'  #separates singer from lyrics

    counter = 0
    fileContent = ""
    currentOldContentOffset = 0
    for lyric in lyrics:
        #run through the file one time to parse metadata
        meta = get_metadata(lyric, delimiter)
        padding = "kommentar:"
        fileContent += ass_parser.parse_line_to_ass(padding, delimiter,
                                                    allowEmptyLines) + "\n"

        title_line = "kommentar: Titel:" + meta.titel
        mel_line = "kommentar: Melodi:" + meta.melodi
        auth_line = u"kommentar: Författare:" + meta.forf
        arr_line = "kommentar: Arr:" + meta.arr
        medv_line = "kommentar: Medverkande:" + meta.medv
        fileContent += ass_parser.parse_line_to_ass(title_line, delimiter,
                                                    allowEmptyLines) + "\n"
        fileContent += ass_parser.parse_line_to_ass(mel_line, delimiter,
                                                    allowEmptyLines) + "\n"
        fileContent += ass_parser.parse_line_to_ass(auth_line, delimiter,
                                                    allowEmptyLines) + "\n"
        fileContent += ass_parser.parse_line_to_ass(arr_line, delimiter,
                                                    allowEmptyLines) + "\n"
        fileContent += ass_parser.parse_line_to_ass(medv_line, delimiter,
                                                    allowEmptyLines) + "\n"

        fileContent += ass_parser.parse_line_to_ass(padding, delimiter,
                                                    allowEmptyLines) + "\n"

        #if the first line does not have a singer
        #we will interpret it and following lines as if everyone is singing
        #NOTE: empty lines are intepreted as belonging to the previous singer
        #but we manually override this behavior for the first line only.
        #NOTE: its likely that the preprocessing will remove these lines anyway
        ass_parser.empty_style = "ALLA"
        filename = data.get("urls")[counter]
        counter += 1

        # Read data from old file (if such exists)
        useOldFile, oldContent, oldContentOffset = getOldData(
            filename, outfilename, currentOldContentOffset)
        currentOldContentOffset = oldContentOffset
        # Find diff in files
        diff = diff_tool.find_unchanged_lines(filename, lyric, allowEmptyLines)
        # Fetch the new data
        newContent = preprocess_ass(lyric, delimiter)
        # Process each diff.
        for d in diff:
            if d.isNewLine or not useOldFile:
                ass_line = ass_parser.parse_line_to_ass(
                    newContent[d.line], delimiter, allowEmptyLines)
                if len(ass_line) > 0:
                    fileContent += ass_line + "\n"
            else:
                if d.line + oldContentOffset >= len(oldContent):
                    print(
                        "Dammit, now we are outside the valid intervals. Did anyone change the header size? (title etc.) See getOldData() for fix."
                    )
                fileContent += oldContent[d.line + oldContentOffset]

    #open to write ASS output to
    if len(os.path.dirname(outfilename).strip()) > 0 and not os.path.exists(
            os.path.dirname(outfilename)):
        os.makedirs(os.path.dirname(outfilename))
    outfile = codecs.open(outfilename, 'w', 'utf-8')

    #write preamble to the ASS file
    outfile.write(kuplett_parser.get_ass_header(sourcefile))
    #write content
    outfile.write(fileContent)
    outfile.close()
def kupletter_to_ass(username,password,outfilename):
    sourcefile = "data_2016.txt"
    allowEmptyLines = True

    lyrics=kuplett_parser.get_all_lyrics(sourcefile, username, password);
    
    #parse read text and write to an ASS file
    ass_parser=Raw_to_ass_parser(30,1) #increment 1 second for each new line. Start at 30 seconds.
    
    # Load dictionary for mapping singers
    data = kuplett_parser.load_data(sourcefile);
    ass_parser.style_dictionary=data.get("dictionary")
    if "multilinesplitter" in data.get("meta").keys():
        ass_parser.multi_line_keyword = data.get("meta").get("multilinesplitter")

    delimiter=':' #separates singer from lyrics

    counter = 0
    fileContent = "";
    currentOldContentOffset = 0
    for lyric in lyrics:
        #run through the file one time to parse metadata
        meta=get_metadata(lyric,delimiter)
        padding="kommentar:"
        fileContent += ass_parser.parse_line_to_ass(padding,delimiter,allowEmptyLines)+"\n"

        title_line="kommentar: Titel:"+meta.titel
        mel_line="kommentar: Melodi:"+meta.melodi
        auth_line=u"kommentar: Författare:"+meta.forf
        arr_line="kommentar: Arr:"+meta.arr
        medv_line="kommentar: Medverkande:"+meta.medv
        fileContent += ass_parser.parse_line_to_ass(title_line,delimiter,allowEmptyLines)+"\n"
        fileContent += ass_parser.parse_line_to_ass(mel_line,delimiter,allowEmptyLines)+"\n"
        fileContent += ass_parser.parse_line_to_ass(auth_line,delimiter,allowEmptyLines)+"\n"
        fileContent += ass_parser.parse_line_to_ass(arr_line,delimiter,allowEmptyLines)+"\n"
        fileContent += ass_parser.parse_line_to_ass(medv_line,delimiter,allowEmptyLines)+"\n"

        fileContent += ass_parser.parse_line_to_ass(padding,delimiter,allowEmptyLines)+"\n"
        
        #if the first line does not have a singer
        #we will interpret it and following lines as if everyone is singing
        #NOTE: empty lines are intepreted as belonging to the previous singer
        #but we manually override this behavior for the first line only.
        #NOTE: its likely that the preprocessing will remove these lines anyway
        ass_parser.empty_style="ALLA"
        filename = data.get("urls")[counter]
        counter += 1
        
        # Read data from old file (if such exists)
        useOldFile, oldContent, oldContentOffset = getOldData(filename, outfilename, currentOldContentOffset)
        currentOldContentOffset = oldContentOffset
        # Find diff in files
        diff = diff_tool.find_unchanged_lines(filename, lyric, allowEmptyLines)
        # Fetch the new data
        newContent = preprocess_ass(lyric,delimiter)
        # Process each diff.
        for d in diff:
            if d.isNewLine or not useOldFile:
                ass_line=ass_parser.parse_line_to_ass(newContent[d.line],delimiter,allowEmptyLines)
                if len(ass_line) > 0:
                    fileContent += ass_line+"\n"
            else:
                if d.line+oldContentOffset >= len(oldContent):
                    print("Dammit, now we are outside the valid intervals. Did anyone change the header size? (title etc.) See getOldData() for fix.")
                fileContent += oldContent[d.line+oldContentOffset]
    
    #open to write ASS output to
    if len(os.path.dirname(outfilename).strip()) > 0 and not os.path.exists(os.path.dirname(outfilename)):
        os.makedirs(os.path.dirname(outfilename))
    outfile = codecs.open(outfilename, 'w','utf-8')
    
    #write preamble to the ASS file
    outfile.write(kuplett_parser.get_ass_header(sourcefile))
    #write content
    outfile.write(fileContent)
    outfile.close()
Exemplo n.º 3
0
# -*- coding: utf-8 -*-
#above is needed for åäö
from raw_to_ass import Raw_to_ass_parser
from raw_to_ass import seconds_to_time,time_to_seconds


#time conversion tests
test=Raw_to_ass_parser(2,3)
print "start "+test.time_start
print "end "+test.time_end
test.increment_time()
print "time step:"+str(test.time_step)
print "start "+test.time_start
print "end "+test.time_end
test.increment_time()
print "time step:"+str(test.time_step)
print "start "+test.time_start
print "end "+test.time_end

print seconds_to_time(3600+60+1.11)

print time_to_seconds(seconds_to_time(6))
print time_to_seconds(seconds_to_time(66))
print time_to_seconds(seconds_to_time(666))
print time_to_seconds(seconds_to_time(6666))

#print line and map things to style
#whitespaces are stripped from the text part.
line='A: Jag är Anna'
test.style_dictionary["a"]="ANNA"
print test.parse_line_to_ass(line,':')