Reputation: 69
From the following code, I want to query each line which contains a song name (a line from a file "playlist.txt") from the google search engine, then based on the results I am taking the youtube link and using the youtube-dl, I am extracting the audio.
When i run the link which the results hold,GOOGLE blocks that claiming a unusual traffic from my computer system.Any method to counter that
I have used sleep to keep a time lapse between the requests.
I want to know how to rectify that error
I think changing the user agents or using proxies might be useful to avoid automated search detection.So, I wanted to know how to change my code to achieve that
import urllib
import json as m_json
import re
import time
import subprocess
from random import randint
import getpass
playlist=open('playlist.txt','r')
songs = playlist.readlines()
song_num = 1
for song in songs:
query = song
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
title = result['title']
url = result['url']
if re.search(r'www.youtube.com',url):
print ( title + '; ' + url )
print "DOWNLOADING",title
decoded_url=urllib.unquote(url).decode('utf8')
print decoded_url
subprocess.call(['youtube-dl','-o','/home/'+getpass.getuser()+'/Videos/playlist%('+title+").(ext)s","--extract-audio","--audio-format","mp3",decoded_url])
break;
print song_num
time.sleep(randint(10,15))
song_num+=1
output
DOWNLOADING <b>Black Sabbath Iron Man</b> - YouTube
http://www.youtube.com/watch?v=rT4KpfiFcNc
[youtube] rT4KpfiFcNc: Downloading webpage
[youtube] rT4KpfiFcNc: Extracting video information
[youtube] rT4KpfiFcNc: Downloading DASH manifest
ERROR: Error in output template: unsupported format character '(' (0x28) at index 73 (encoding: 'UTF-8')
Upvotes: 1
Views: 2967
Reputation: 8624
Your url
is malformed. If you look closely, you have:
http://www.youtube.com/watch%3Fv%3DrT4KpfiFcNc
but the proper Youtube URL format is:
https://www.youtube.com/watch?v=rT4KpfiFcNc
You should try to decode the URL before calling the youtube-dl
. Something like this:
url=urllib.unquote(result['url']).decode('utf8')
Additionally, your output format is invalid; you want simply
'-o', '~/Videos/playlist%(title)s.(ext)s"
%('+title+").
is adivising youtube-dl to look for a property named after the title, and to boot, missing an s
after )
.
Upvotes: 3