1

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

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() 时出现以下套接字异常:

  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  

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

编辑:
针对评论中提出的问题,video_idqh-mwjF-OMoparsed_url是:

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

1 回答 1

0

我的错误是video_id应该作为关键字参数传递,如下所示:

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

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

于 2012-08-05T07:28:22.447 回答