Skip to content

Instantly share code, notes, and snippets.

@lyuboraykov
Created August 25, 2014 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyuboraykov/eeb5134fcf53d730fdcf to your computer and use it in GitHub Desktop.
Save lyuboraykov/eeb5134fcf53d730fdcf to your computer and use it in GitHub Desktop.
Download songs from youtube
# -*- coding: utf-8 -*-
import pafy
import requests
from multiprocessing import Pool
import pyglet
import os
import sys
def get_url_from_name(name):
words = name.split()
query = '+'.join(words)
url = 'https://www.youtube.com/results?search_query={}'.format(query)
response_html = requests.get(url).content
index = response_html.find('href="/watch?v=')
print('index: {}'.format(index))
watch_url = response_html[index:index+26]
print('watch_url: {}'.format(watch_url))
watch_url = watch_url.replace('href="', '')
watch_url = 'http://youtube.com' + watch_url
return watch_url
if __name__ == '__main__':
playlist_file = sys.argv[1]
lines = []
with open('citroen.playlist', 'r') as reader:
lines = reader.readlines()
for line in lines:
url = get_url_from_name(line)
video = pafy.new(url)
bestaudio = video.getbestaudio()
bestaudio.download()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment