File size: 5,918 Bytes
a1de66d
 
fbfdf9e
a830900
 
 
00a404f
a1de66d
 
 
 
00a404f
a1de66d
 
 
 
 
 
00a404f
fbfdf9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8091cf
 
 
cfa921b
deed607
 
cfa921b
beb6438
 
deed607
cfa921b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b21a71c
49acd1b
cfa921b
 
 
49acd1b
cfa921b
 
49acd1b
cfa921b
 
 
 
 
b21a71c
cfa921b
49acd1b
a1de66d
40d6d53
 
 
cfa921b
b21a71c
7692c5b
cfa921b
4da4ce9
7692c5b
 
cfa921b
 
 
 
 
e041827
4da4ce9
 
cfa921b
4da4ce9
e041827
cfa921b
4da4ce9
 
 
7692c5b
 
 
40d6d53
 
 
7692c5b
4da4ce9
7ac5048
7692c5b
 
0da5d4f
 
7692c5b
 
 
 
4da4ce9
fbfdf9e
7692c5b
 
 
 
 
0962db3
7692c5b
 
 
 
4da4ce9
7692c5b
 
 
 
cfa921b
 
 
 
 
0962db3
6f681ea
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
import re
import os
import glob

st.set_page_config(layout="wide")

def process_line(line):
    if re.search(r'\b[A-G][#b]?m?\b', line):
        line = re.sub(r'\b([A-G][#b]?m?)\b', r"<img src='\1.png' style='height:20px;'>", line)
    return line

def process_chord_sheet(chord_sheet):
    processed_lines = []
    for line in chord_sheet.split('\n'):
        processed_line = process_line(line)
        processed_lines.append(processed_line)
    return '<br>'.join(processed_lines)

def create_search_url_wikipedia(artist_song):
    base_url = "https://www.wikipedia.org/search-redirect.php?family=wikipedia&language=en&search="
    return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')

def create_search_url_youtube(artist_song):
    base_url = "https://www.youtube.com/results?search_query="
    return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')

def create_search_url_chords(artist_song):
    base_url = "https://www.ultimate-guitar.com/search.php?search_type=title&value="
    return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and')

def create_search_url_lyrics(artist_song):
    base_url = "https://www.google.com/search?q="
    return base_url + artist_song.replace(' ', '+').replace('–', '%E2%80%93').replace('&', 'and') + '+lyrics'

def songupdate():
    st.write(st.session_state.EnhancedChordSheet)

def load_song_file2(filename):
    with open(filename, "r") as file:
        chord_sheet = file.read()
        st.session_state['chord_sheet'] = chord_sheet  # Load the content into session state
        processed_sheet = process_chord_sheet(chord_sheet)
        st.markdown(processed_sheet, unsafe_allow_html=True)

def load_song_file(filename):
    with open(filename, "r") as file:
        chord_sheet = file.read()
        # Instead of directly modifying the session state, return the chord sheet content
        return chord_sheet

def song_update():
    if 'selected_file' in st.session_state:
        song_name, artist_name = parse_filename(st.session_state.selected_file)
        st.session_state.song_name = song_name
        st.session_state.artist_name = artist_name

def parse_filename(filename):
    base_name = os.path.splitext(filename)[0]
    song_name, artist_name = base_name.split(' by ')
    return song_name.replace("_", " "), artist_name.replace("_", " ")

def auto_save():
    song_name = st.session_state.get('song_name', '')
    artist_name = st.session_state.get('artist_name', '')
    chord_sheet = st.session_state.get('chord_sheet', '')

    if song_name and artist_name and chord_sheet:
        filename = song_name + " by " + artist_name + ".txt"
        with open(filename, "w") as file:
            
            
            chord_sheet_text = st.session_state.get('chord_sheet', '')
            file.write(chord_sheet_text)
            
        st.session_state['char_count'] = len(chord_sheet)
        st.success(f"Auto-saved to {filename}")

def main():
    st.title('🎵 Song Files')
    with st.expander("Select Song File", expanded=True):
        all_files = [f for f in glob.glob("*.txt") if ' by ' in f]
        selected_file = st.selectbox("Choose a file", all_files, on_change=song_update, key='selected_file')

    col1, col2, col3 = st.columns([1, 1, 4])

    with col1:
        song_name_input = st.text_input("🎵 Song:", key='song_name', on_change=auto_save)
        artist_name_input = st.text_input("🎤 Artist:", key='artist_name', on_change=auto_save)

        if 'selected_file' in st.session_state and st.session_state.selected_file:
            # Update the session state before creating the text area widget
            st.session_state['chord_sheet'] = load_song_file(st.session_state.selected_file)

        # Save functionality
        if st.button("💾 Save", key="save_song"):
            if song_name_input and artist_name_input:
                filename = song_name_input + " by " + artist_name_input + ".txt"
                with open(filename, "w") as file:
                    file.write(chord_sheet_area)
                st.success("Chord sheet saved to file: " + filename)
            else:
                st.error("Both Song Name and Artist Name are required.")


    with col2:

        st.header("🎼 Available Songs")
        for file in all_files:
            song_info = os.path.splitext(file)[0].replace("_", " ")
            icol1, icol2 = st.columns([4, 2])
            with icol1:
                #st.markdown(f"* {song_info}")

                if selected_file:
                    #st.markdown(f"**Song:** {song_info}")
                    st.markdown("**" + song_info + "**")

                    load_song_file(selected_file)
                    song_info = os.path.splitext(selected_file)[0].replace("_", " ")

            with icol2:

                if selected_file:
                    st.markdown(f"[📚Wikipedia]({create_search_url_wikipedia(song_info)})")
                    st.markdown(f"[🎥YouTube]({create_search_url_youtube(song_info)})")
                    st.markdown(f"[🎸Chords]({create_search_url_chords(song_info)})")
                    st.markdown(f"[🎶Lyrics]({create_search_url_lyrics(song_info)})")

    with col3:
        
        # Now create the text area widget with the updated session state
        chord_sheet_area = st.text_area("Chord Sheet", value=st.session_state.get('chord_sheet', ''), height=600, key='chord_sheet', on_change=auto_save)

        char_count_msg = f"Character Count: {st.session_state.get('char_count', 0)}"
        st.write(char_count_msg)
        
        

    # Load chord sheet from selected file into the text area
    if 'selected_file' in st.session_state and st.session_state.selected_file:
        load_song_file(st.session_state.selected_file)

        
if __name__ == '__main__':
    main()