0
votes

I use a python script to search for video information with youtube v3 api. On one computer the script works perfectly, but on another it receive the following error:

File "script.py", line 105, in youtube_search(options) File "script.py", line 16, in youtube_search developerKey = DEVELOPER_KEY ..... File "C:\Python27\lib\httplib.py", line 1013, in getresponse raise ResponseNotReady() httplib.ResponseNotReady.

The youtube_search() function that I'm using is:

def youtube_search(options):
 youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
 developerKey=DEVELOPER_KEY)

 search_response = youtube.search().list(
  q = options.q,
  part = "id, snippet",
  maxResults=options.maxResults
 ).execute()

 videos = []
 channels = []
 playlists = []
 videoInfo = []

 t = datetime.datetime.now()
 ff_name = '[' + options.q + '].txt'
 f = open(ff_name, 'w')

 no_results = search_response.get("pageInfo")["totalResults"]
 page = 1
 while (page <= (no_results / 50)):
   nextPage = search_response.get("nextPageToken")
   for search_result in search_response.get("items", []):
   if search_result["id"]["kind"] == "youtube#video":
   info = youtube.videos().list(
     part = "statistics,contentDetails"
     ,id = search_result["id"]["videoId"]).execute()
   for info_result in info.get("items", []):
     videos.append("%s<|>%s<|>%s" % (
                 time.strftime("%x")
                 ,nextPage
                         ,search_result["snippet"]["title"]
                                      )
   f.write(str(videos))
   f.write('\n')
   videos = []
   page = page + 1
   search_response = youtube.search().list(
     q = options.q,
     part = "id,snippet",
     maxResults = options.maxResults,
     pageToken = nextPage
   ).execute()

Do you have any hints on why I encounter this behavior?

Thanks.

1
The only possible way for us to help you would be for you to show us your youtube_search(options) function. Edit your answer to include it please. - VooDooNOFX

1 Answers

0
votes

That specific exception is explained at Python httplib ResponseNotReady

One thing to point out, though, is that you don't need to perform a separate youtube.videos.list() call for each video id. You can pass up to 50 comma-separate video ids as the id= parameter to a single youtube.videos.list() call. Cutting down on the number of HTTP requests you're making will lead to better performance and may work around the exception.