在Python中使用gdata Youtube API时出现套接字错误 [英] Socket error when using gdata Youtube API in Python

查看:71
本文介绍了在Python中使用gdata Youtube API时出现套接字错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gdata使用以下代码将YouTube URL映射到视频标题:

I'm using gdata to map YouTube URLs to video titles, using the following code:

import gdata.youtube.service as youtube
import re
import queue
import urlparse

ytservice = youtube.YouTubeService()
ytservice.ssl = True
ytservice.developer_key = '' # snip

class youtube(mediaplugin):
  def __init__(self, parsed_url):
    self.url = parsed_url
    self.video_id = urlparse.parse_qs(parsed_url.query)['v'][0]
    self.ytdata = ytservice.GetYouTubeVideoEntry(self.video_id)
    print self.ytdata

调用service.GetYouTubeVideoEntry()时收到以下套接字异常:

I get the following socket exception when calling service.GetYouTubeVideoEntry():

  File "/Users/haldean/Documents/qpi/qpi/media.py", line 21, in __init__
    self.ytdata = ytservice.GetYouTubeVideoEntry(self.video_id)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/gdata/youtube/service.py", line 210, in GetYouTubeVideoEntry
    return self.Get(uri, converter=gdata.youtube.YouTubeVideoEntryFromString)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/gdata/service.py", line 1069, in Get
    headers=extra_headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/__init__.py", line 93, in optional_warn_function
    return f(*args, **kwargs)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/service.py", line 186, in request
    data=data, headers=all_headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/http_interface.py", line 148, in perform_request
    return http_client.request(operation, url, data=data, headers=headers)
  File "/Users/haldean/Documents/qpi/lib/python2.7/site-packages/atom/http.py", line 163, in request
    connection.endheaders()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
    self._send_output(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output
    self.send(msg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send
    self.connect()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1140, in connect
    self.timeout, self.source_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 8] nodename nor servname provided, or not known  

我不知道如何开始调试它.任何想法表示赞赏.谢谢!

I'm at a loss as to how to even begin debugging this. Any ideas appreciated. Thanks!


对于评论中提出的问题,video_idqh-mwjF-OMo,而parsed_url是:


In response to a question asked in comments, video_id is qh-mwjF-OMo and parsed_url is:

ParseResult(scheme=u'http', netloc=u'www.youtube.com', path=u'/watch', params='', query=u'v=qh-mwjF-OMo&feature=g-user-u', fragment='')

推荐答案

我的错误是video_id应该作为关键字参数传递,就像这样:

My mistake was that the video_id should be passed as a keyword parameter, like so:

    self.ytdata = ytservice.GetYouTubeVideoEntry(video_id=self.video_id)

似乎套接字异常是gdata唯一会抛出异常的层.它会尝试根据参数盲目获取URL,并且只有在URL提取失败时才会失败.

It seems that the socket exception is the only layer of gdata that will throw an exception; it tries to get a URL blindly based on the arguments and it only fails when the URL fetch fails.

这篇关于在Python中使用gdata Youtube API时出现套接字错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆