ytsearch.save 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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
  9. ## -Config File
  10. ## -Default Musicfile
  11. ## -Defualt retrys
  12. ## -Default save-to location
  13. ## -Default verbosity and logging location
  14. ##
  15. ## - Download over tor
  16. ## - Check if Tor installed
  17. ## - Warn user if NOT using tor
  18. ##
  19. ## - Multi-thread conversion mp3
  20. ##
  21. ## - Retry up to X times the link-generation routine
  22. ##
  23. ## - Allow discogs reference number as argument
  24. ## - ytsearch will parse discogs website with BeautifulSoup
  25. ## - Find the Artist, Album Track Titles
  26. ## - Create folder structure locally and create list.txt
  27. ## - Begin processing...
  28. ##
  29. ## - Logging and Verbosit levels
  30. ##
  31. ## - Check for dependencies
  32. ## - Tor?
  33. ## - Write Permissions in log files?
  34. ## - YoutubeSearch
  35. ##Vars
  36. DOWNLOAD=True
  37. MUSICFILE="list.txt"
  38. ##
  39. music=[] # list to hold dictionaries of songnum, Title, Artist
  40. logresults=[] # list to hold results of link creation attempts, and download attempts
  41. linkresults=[] #
  42. completed=[] # list to hold song numbers of completed downloads so they can be removed from MUSICFILE
  43. ##Open list.txt, read into music[]
  44. def readlist(file):
  45. songnum = 0
  46. with open(file) as f:
  47. for line in f:
  48. song={}
  49. (key, val) = line.split(", ")
  50. songnum += 1
  51. song['songnum'] = songnum
  52. song['Title'] = key
  53. song['Artist'] = val.rstrip()
  54. # song['raw'] = line
  55. music.append(song)
  56. f.close()
  57. return music
  58. def parselist(musiclist):
  59. for song in musiclist:
  60. searchterm = song['Title'] + " " + song['Artist'] + ' lyrics'
  61. dictlink={}
  62. try:
  63. ytresult = YoutubeSearch(searchterm, max_results=1).to_dict() ##increase timeout!!
  64. link = 'https://youtube.com' + ytresult[0]['link']
  65. cherokee2
  66. cherokee2
  67. logresults.append(song['Title'] + ", " + song['Artist'] + " Link Created")
  68. if DOWNLOAD:
  69. print("Attempting to download", song['Title'])
  70. downloadsong(link, song)
  71. else:
  72. print("ERROR: Not Downloading for some reason")
  73. except:
  74. logresults.append(song['Title'] + ", " + song['Artist'] + " COULD NOT CREATE LINK")
  75. cleanup(MUSICFILE)
  76. def downloadsong(link, song):
  77. try:
  78. os.system("youtube-dl --extract-audio --audio-format mp3 --audio-quality 0 --output '%(title)s.%(ext)s' --ignore-errors " + link)
  79. completed.append(song['songnum'])
  80. logresults.append(song['Title'] + ", " + song['Artist'] + " Audio downloaded")
  81. print("Download Complete!")
  82. except e as youtubedlexception:
  83. logresults.append(song['Title'] + ", " + song['Artist'] + " FAILED TO DOWNLOAD SONG (youtube-dl)")
  84. print(youtubedlexception)
  85. def cleanup(file):
  86. print("Cleaning completed files from list")
  87. print("Completed Downloads:", completed)
  88. linenum=0
  89. with open(file, "r") as f:
  90. lines = f.readlines()
  91. with open(file, "w") as f:
  92. for line in lines:
  93. linenum += 1
  94. if linenum not in completed:
  95. f.write(line)
  96. f.close()
  97. if __name__ == "__main__":
  98. readlist(MUSICFILE)
  99. parselist(music)
  100. print("------------")
  101. for r in logresults:
  102. print(r)
  103. #print(completed)