niittymaa

LyricGenius Script - Download and Convert to Text Files

Feb 14th, 2023 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | Music | 0 0
  1. '''
  2. LyricGenius Script: Download and Convert to Text Files
  3. -------------------------------------------------------
  4. This script downloads song lyrics using the 'LyricsGenius' python library and converts them into text files
  5. by removing unnecessary special characters. The purpose of the script is to prepare the lyrics as better data
  6. to be used for AI training.
  7.  
  8. Script by: Creatide (Sakari Niittymaa - 2023)
  9. '''
  10.  
  11. import os
  12. import json
  13. from lyricsgenius import Genius
  14.  
  15. class Lyrics():
  16.     def __init__(self):
  17.        
  18.         self.artist_name = 'Nirvana'
  19.        
  20.         # Remove or replace these symbols and words
  21.         self.remove_symbols = {
  22.             'д': 'ä', 'Д': 'Ä',
  23.             'ц': 'ö', 'Ц': 'Ö',
  24.             '/': '', '_': '', '--': '',
  25.             'I:': '', 'II:': '', 'III:': '',
  26.             'San.': '', 'san.': '', 'Säv.': '', 'säv.': '',
  27.             'Kertosäe2:': '', 'Kertosäe:': '' ,
  28.             'Embed': '', 'You might also like': '',
  29.             'CHORUS': '', 'Chorus': '',
  30.             'VERSE': '', 'Verse': '',
  31.             'Intro': '', 'INTRO': '',
  32.             'SOLOS:': '', 'SOLO:': '', '(solo)': '',
  33.             'PRE-:': '', 'PRE-': '',
  34.             'C-PART': '', 'Spoken:': '',              
  35.             '(x2)': '', '( x2)': '', '(2x)': '', '(x3)': '', '(3x)': '', '( x3)': '',
  36.             '()': '', '(?)': '', '(???)': '',
  37.             '(original)': '',
  38.             "Refren':": '', "Refrain:": '', "Refren'": '',
  39.         }
  40.        
  41.         self.json_filename = ''
  42.        
  43.         # DISABLE DOWNLOADING AND ONLY CLEAN TEXT FILES
  44.         # ---------------------------------------------
  45.         self.download_lyrics()
  46.        
  47.         # Convert and clean text to files
  48.         self.convert_to_txt()
  49.    
  50.    
  51.     # Download Lyrics ---------------------------------------------------------------------------- #
  52.     # https://lyricsgenius.readthedocs.io/en/master/usage.html
  53.    
  54.     def download_lyrics(self):
  55.         genius = Genius('INSERT_GENIUS_TOKEN_HERE')
  56.         genius.remove_section_headers = True
  57.         genius.response_format = 'plain'
  58.         artist = genius.search_artist(self.artist_name)
  59.         if artist:
  60.             artist.save_lyrics()
  61.  
  62.     # Extract Lyrics From JSON And Save To Separated Files --------------------------------------- #
  63.  
  64.     def convert_to_txt(self):
  65.        
  66.         self.json_filename = self.clean_name('Lyrics_' + self.clean_artist_name(self.artist_name) + '.json')
  67.         self.json_filename = self.json_filename.replace(' ', '')
  68.  
  69.         # Check if json file exist
  70.         if os.path.exists(self.json_filename):
  71.  
  72.             json_data = {}
  73.  
  74.             def save_text_file(data):
  75.                                              
  76.                 filename = self.clean_name(data['song']) + ' - ' + self.clean_name(data['artist']) + '.txt'
  77.                 file_path = os.path.join(os.getcwd(), 'Lyrics', data['artist'])
  78.                
  79.                 if not os.path.exists(file_path):
  80.                     os.makedirs(file_path)
  81.                
  82.                 file_path = os.path.join(file_path, filename)
  83.                
  84.                 # if os.path.exists(file_path):
  85.                 with open(file_path, 'w', encoding='utf-8') as f:
  86.                     reformatted_data = ''
  87.                     for lyric in data['lyrics']:
  88.                         reformatted_data += lyric
  89.                     # Try to split string from target word like "Lyrics"
  90.                     if len(reformatted_data.split('Lyrics', 1)) > 1:
  91.                         reformatted_data = reformatted_data.split('Lyrics', 1)[1]
  92.                     # If needed replace character with another
  93.                     for k, v in self.remove_symbols.items():
  94.                         reformatted_data = reformatted_data.replace(k, v)
  95.                    
  96.                     # Save file
  97.                     f.write(str(reformatted_data))                
  98.  
  99.             with open(self.json_filename) as json_file:
  100.                 json_data = json.load(json_file)
  101.                 print('File Found:', self.json_filename)
  102.            
  103.             for song in json_data['songs']:
  104.                 data = {
  105.                     'artist': song['artist'],
  106.                     'song': song['title'],
  107.                     'lyrics': song['lyrics'],
  108.                 }
  109.                 print(song['title'])
  110.                 save_text_file(data)
  111.        
  112.     # Clean names to avoid path errors
  113.     def clean_name(self, name):        
  114.         return_name = name
  115.         remove_symbols = [*'<>:"/\|?*']
  116.         for i in remove_symbols:
  117.             return_name = return_name.replace(i, '')
  118.         return return_name
  119.    
  120.     # Clean artist names to make path work
  121.     def clean_artist_name(self, name):        
  122.         return_name = name
  123.         remove_symbols = [*'<>:"/\|?*() ']
  124.         for i in remove_symbols:
  125.             return_name = return_name.replace(i, '')
  126.         return return_name
  127.    
  128. # Main ------------------------------------------------------------------------------------------- #
  129.    
  130. def main():
  131.     Lyrics()
  132.    
  133. if __name__ == '__main__':
  134.     main()
Advertisement
Add Comment
Please, Sign In to add comment