#!/usr/bin/python3 from youtube_search import YoutubeSearch import os ##Import list of songs, artists from list.txt ##parse this file into a list of dictionaries ##for each dictionary pair, search youtube and output formatted links in file ## ## FUTURE ## -Config File ## -Default Musicfile ## -Defualt retrys ## -Default save-to location ## -Default verbosity and logging location ## ## - Download over tor ## - Check if Tor installed ## - Warn user if NOT using tor ## ## - Multi-thread conversion mp3 ## ## - Retry up to X times the link-generation routine ## ## - Allow discogs reference number as argument ## - ytsearch will parse discogs website with BeautifulSoup ## - Find the Artist, Album Track Titles ## - Create folder structure locally and create list.txt ## - Begin processing... ## ## - Logging and Verbosit levels ## ## - Check for dependencies ## - Tor? ## - Write Permissions in log files? ## - YoutubeSearch ##Vars DOWNLOAD=True MUSICFILE="list.txt" ## music=[] # list to hold dictionaries of songnum, Title, Artist logresults=[] # list to hold results of link creation attempts, and download attempts linkresults=[] # completed=[] # list to hold song numbers of completed downloads so they can be removed from MUSICFILE ##Open list.txt, read into music[] def readlist(file): songnum = 0 with open(file) as f: for line in f: song={} (key, val) = line.split(", ") songnum += 1 song['songnum'] = songnum song['Title'] = key song['Artist'] = val.rstrip() # song['raw'] = line music.append(song) f.close() return music def parselist(musiclist): for song in musiclist: searchterm = song['Title'] + " " + song['Artist'] + ' lyrics' dictlink={} try: ytresult = YoutubeSearch(searchterm, max_results=1).to_dict() ##increase timeout!! link = 'https://youtube.com' + ytresult[0]['link'] cherokee2 cherokee2 logresults.append(song['Title'] + ", " + song['Artist'] + " Link Created") if DOWNLOAD: print("Attempting to download", song['Title']) downloadsong(link, song) else: print("ERROR: Not Downloading for some reason") except: logresults.append(song['Title'] + ", " + song['Artist'] + " COULD NOT CREATE LINK") cleanup(MUSICFILE) def downloadsong(link, song): try: os.system("youtube-dl --extract-audio --audio-format mp3 --audio-quality 0 --output '%(title)s.%(ext)s' --ignore-errors " + link) completed.append(song['songnum']) logresults.append(song['Title'] + ", " + song['Artist'] + " Audio downloaded") print("Download Complete!") except e as youtubedlexception: logresults.append(song['Title'] + ", " + song['Artist'] + " FAILED TO DOWNLOAD SONG (youtube-dl)") print(youtubedlexception) def cleanup(file): print("Cleaning completed files from list") print("Completed Downloads:", completed) linenum=0 with open(file, "r") as f: lines = f.readlines() with open(file, "w") as f: for line in lines: linenum += 1 if linenum not in completed: f.write(line) f.close() if __name__ == "__main__": readlist(MUSICFILE) parselist(music) print("------------") for r in logresults: print(r) #print(completed)