Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- LyricGenius Script: Download and Convert to Text Files
- -------------------------------------------------------
- This script downloads song lyrics using the 'LyricsGenius' python library and converts them into text files
- by removing unnecessary special characters. The purpose of the script is to prepare the lyrics as better data
- to be used for AI training.
- Script by: Creatide (Sakari Niittymaa - 2023)
- '''
- import os
- import json
- from lyricsgenius import Genius
- class Lyrics():
- def __init__(self):
- self.artist_name = 'Nirvana'
- # Remove or replace these symbols and words
- self.remove_symbols = {
- 'д': 'ä', 'Д': 'Ä',
- 'ц': 'ö', 'Ц': 'Ö',
- '/': '', '_': '', '--': '',
- 'I:': '', 'II:': '', 'III:': '',
- 'San.': '', 'san.': '', 'Säv.': '', 'säv.': '',
- 'Kertosäe2:': '', 'Kertosäe:': '' ,
- 'Embed': '', 'You might also like': '',
- 'CHORUS': '', 'Chorus': '',
- 'VERSE': '', 'Verse': '',
- 'Intro': '', 'INTRO': '',
- 'SOLOS:': '', 'SOLO:': '', '(solo)': '',
- 'PRE-:': '', 'PRE-': '',
- 'C-PART': '', 'Spoken:': '',
- '(x2)': '', '( x2)': '', '(2x)': '', '(x3)': '', '(3x)': '', '( x3)': '',
- '()': '', '(?)': '', '(???)': '',
- '(original)': '',
- "Refren':": '', "Refrain:": '', "Refren'": '',
- }
- self.json_filename = ''
- # DISABLE DOWNLOADING AND ONLY CLEAN TEXT FILES
- # ---------------------------------------------
- self.download_lyrics()
- # Convert and clean text to files
- self.convert_to_txt()
- # Download Lyrics ---------------------------------------------------------------------------- #
- # https://lyricsgenius.readthedocs.io/en/master/usage.html
- def download_lyrics(self):
- genius = Genius('INSERT_GENIUS_TOKEN_HERE')
- genius.remove_section_headers = True
- genius.response_format = 'plain'
- artist = genius.search_artist(self.artist_name)
- if artist:
- artist.save_lyrics()
- # Extract Lyrics From JSON And Save To Separated Files --------------------------------------- #
- def convert_to_txt(self):
- self.json_filename = self.clean_name('Lyrics_' + self.clean_artist_name(self.artist_name) + '.json')
- self.json_filename = self.json_filename.replace(' ', '')
- # Check if json file exist
- if os.path.exists(self.json_filename):
- json_data = {}
- def save_text_file(data):
- filename = self.clean_name(data['song']) + ' - ' + self.clean_name(data['artist']) + '.txt'
- file_path = os.path.join(os.getcwd(), 'Lyrics', data['artist'])
- if not os.path.exists(file_path):
- os.makedirs(file_path)
- file_path = os.path.join(file_path, filename)
- # if os.path.exists(file_path):
- with open(file_path, 'w', encoding='utf-8') as f:
- reformatted_data = ''
- for lyric in data['lyrics']:
- reformatted_data += lyric
- # Try to split string from target word like "Lyrics"
- if len(reformatted_data.split('Lyrics', 1)) > 1:
- reformatted_data = reformatted_data.split('Lyrics', 1)[1]
- # If needed replace character with another
- for k, v in self.remove_symbols.items():
- reformatted_data = reformatted_data.replace(k, v)
- # Save file
- f.write(str(reformatted_data))
- with open(self.json_filename) as json_file:
- json_data = json.load(json_file)
- print('File Found:', self.json_filename)
- for song in json_data['songs']:
- data = {
- 'artist': song['artist'],
- 'song': song['title'],
- 'lyrics': song['lyrics'],
- }
- print(song['title'])
- save_text_file(data)
- # Clean names to avoid path errors
- def clean_name(self, name):
- return_name = name
- remove_symbols = [*'<>:"/\|?*']
- for i in remove_symbols:
- return_name = return_name.replace(i, '')
- return return_name
- # Clean artist names to make path work
- def clean_artist_name(self, name):
- return_name = name
- remove_symbols = [*'<>:"/\|?*() ']
- for i in remove_symbols:
- return_name = return_name.replace(i, '')
- return return_name
- # Main ------------------------------------------------------------------------------------------- #
- def main():
- Lyrics()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment