ytsearch.save.6_16_2020 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/python3
  2. from youtube_search import YoutubeSearch
  3. import os
  4. ##Import list of songs, artists from list.txt
  5. ##parse this file into a list of dictionaries
  6. ##for each dictionary pair, search youtube and output formatted links in file
  7. ##
  8. ##FUTURE - remove successful downloads from text file!!
  9. ## -assign each line read a number
  10. ## -remember numbers that are successful downloads
  11. ## -at end of script, delete lines from those numbers
  12. ## -OR-
  13. ## -Find lines that match successful downloads
  14. ##
  15. ## - Differentiae between Masters and Relesses when taking doscogs argument
  16. DOWNLOAD=True
  17. musicfile="list.txt"
  18. music=[]
  19. songnum = 0
  20. with open(musicfile) as f:
  21. for line in f:
  22. song={}
  23. (key, val) = line.split(", ")
  24. songnum += 1
  25. song['songnum'] = songnum
  26. song['Title'] = key
  27. song['Artist'] = val.rstrip()
  28. song['raw'] = line
  29. music.append(song)
  30. f.close()
  31. logresults=[]
  32. linkresults=[]
  33. completed=[]
  34. for song in music:
  35. searchterm = song['Title'] + " " + song['Artist'] + ' lyrics -Video'
  36. dictlink={}
  37. try:
  38. ytresult = YoutubeSearch(searchterm, max_results=1).to_dict()
  39. link = 'https://youtube.com' + ytresult[0]['link']
  40. dictlink['Title'] = song['Title']
  41. dictlink['Artist'] = song['Artist']
  42. dictlink['link'] = link
  43. linkresults.append(dictlink)
  44. logresults.append(song['Title'] + ", " + song['Artist'] + " Link Created")
  45. if DOWNLOAD:
  46. print("Attempting to download", song['Title'])
  47. try:
  48. os.system("youtube-dl --extract-audio --audio-format mp3 --output '%(title)s.%(ext)s' --ignore-errors " + link)
  49. completed.append(song['songnum'])
  50. logresults.append(song['Title'] + ", " + song['Artist'] + " Audio downloaded")
  51. print("Download Complete!")
  52. except e as youtubedlexception:
  53. logresults.append(song['Title'] + ", " + song['Artist'] + " FAILED TO DOWNLOAD SONG (youtube-dl)")
  54. print(youtubedlexception)
  55. else:
  56. print("ERROR: Not Downloading for some reason")
  57. except:
  58. logresults.append(song['Title'] + ", " + song['Artist'] + " COULD NOT CREATE LINK")
  59. print("------------")
  60. for r in logresults:
  61. print(r)
  62. print(completed)
  63. print("Cleaning completed files from list")
  64. linenum=0
  65. with open(musicfile, "r") as f:
  66. lines = f.readlines()
  67. with open(musicfile, "w") as f:
  68. for line in lines:
  69. linenum += 1
  70. if linenum not in completed:
  71. f.write(line)
  72. f.close()