#!/usr/bin/env python3 |
importurllib.request |
importurllib.parse |
importre |
importcsv |
importsys |
importsubprocess |
deffind_ytb_id(name): |
# code taken from : https://www.codeproject.com/Articles/873060/Python-Search-Youtube-for-Video |
query_string=urllib.parse.urlencode({'search_query' : name}) |
html_content=urllib.request.urlopen('http://www.youtube.com/results?'+query_string) |
search_results=re.findall(r'href='/watch?v=(.{11})', html_content.read().decode()) |
ifsearch_resultsisnotNone: |
print(name+' http://www.youtube.com/watch?v='+search_results[0]) |
returnsearch_results[0] |
print(name+' Was not found X.X') |
returnNone |
defparse_csv(file): |
ids=list() |
withopen(file) asf: |
next(f) |
reader=csv.reader(f, delimiter=',', quotechar='') |
forsonginreader: |
title=song[1] |
author=song[2] |
ytb_name='{} - {}'.format(title, author) |
ytb_id=find_ytb_id(ytb_name) |
ifytb_idisnotNone: |
ids.append(ytb_id) |
returnids |
# takes as parameter a playlist in the format of the following project: |
# https://github.com/watsonbox/exportify |
ids=parse_csv(sys.argv[1]) |
index=1 |
foriinids: |
print ('[{}/{}] {}'.format(index, len(ids), i)) |
# now run youtube-dl / ffmped to get mp3 -> they must be in the host's PATH |
subprocess.run(['youtube-dl', '--audio-format', 'mp3', '-xi', 'http://www.youtube.com/watch?v={}'.format(i) |
]) |
index+=1 |