mike пре 5 година
комит
ff56306128
1 измењених фајлова са 82 додато и 0 уклоњено
  1. 82 0
      ytsearch

+ 82 - 0
ytsearch

@@ -0,0 +1,82 @@
+#!/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 - remove successful downloads from text file!!
+## -assign each line read a number
+## -remember numbers that are successful downloads
+## -at end of script, delete lines from those numbers
+## -OR-
+## -Find lines that match successful downloads
+
+DOWNLOAD=True
+
+musicfile="list.txt"
+
+music=[]
+songnum = 0
+with open(musicfile) 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()
+
+logresults=[]
+linkresults=[]
+completed=[]
+for song in music:
+  searchterm = song['Title'] + " " + song['Artist'] + ' lyrics'
+  dictlink={}
+  try:
+    ytresult = YoutubeSearch(searchterm, max_results=1).to_dict()
+    link = 'https://youtube.com' + ytresult[0]['link']
+    dictlink['Title'] = song['Title']
+    dictlink['Artist'] = song['Artist']
+    dictlink['link'] = link
+    linkresults.append(dictlink)
+    logresults.append(song['Title'] + ", " + song['Artist'] + " Link Created")
+    if DOWNLOAD:
+       print("Attempting to download", song['Title'])
+       try: 
+         os.system("youtube-dl --extract-audio --audio-format mp3 --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)
+    else:
+      print("ERROR: Not Downloading for some reason")
+  except:
+    logresults.append(song['Title'] + ", " + song['Artist'] + " COULD NOT CREATE LINK")
+
+print("------------")
+for r in logresults:
+  print(r)
+
+
+print(completed)
+
+print("Cleaning completed files from list")
+
+linenum=0
+with open(musicfile, "r") as f:
+  lines = f.readlines()
+with open(musicfile, "w") as f:
+  for line in lines:
+    linenum += 1
+    if linenum not in completed:
+     f.write(line)
+
+f.close()