编程语言
首页 > 编程语言> > python – 使用Youtube播放限制检测视频

python – 使用Youtube播放限制检测视频

作者:互联网

我正在使用python开发一个Web服务,我想过滤掉在youtube页面之外无法播放的视频.

喜欢这个链接[https://www.youtube.com/v/SC3pupLn-_8?version=3\u0026amp;f=videos\u0026amp;app=youtube_gdata]
你必须在youtube页面上观看视频是否有任何方式过滤哪些视频属于同一类别.所以我只选择那些可以不受任何限制地播放的视频.

import gdata.youtube.service
#------------------------------------------------------------------------------ 

yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = 'YOUR API DEVELOPER KEY'

count=0

def PrintEntryDetails(entry):

    if entry.media.category[0].text == "Movies" :
        global count
        count = count + 1
        if  entry.noembed != None:
            print 'Video embedding not enable: %s' % entry.noembed.text
        else :
            print "entry embedable"

        print 'Video title: %s' % entry.media.title.text
        print 'Video category: %s' % entry.media.category[0].text
        print 'Video published on: %s ' % entry.published.text
        print 'Video description: %s' % entry.media.description.text
        if entry.media.private != None :
            print entry.media.private.text
        else :
            print "Right not found"          
        if entry.media.keywords :
            print 'Video tags: %s' % entry.media.keywords.text
        print 'Video watch page: %s' % entry.media.player.url
        print 'Video flash player URL: %s' % entry.GetSwfUrl()
        print 'Video duration: %s' % entry.media.duration.seconds        
        # For video statistics
        if entry.statistics :       
            print 'Video view count: %s' % entry.statistics.view_count
        # For video rating
        if entry.rating  :    
            print 'Video rating: %s' % entry.rating.average
        # show alternate formats
        for alternate_format in entry.media.content:
            if 'isDefault' not in alternate_format.extension_attributes:
                print 'Alternate format: %s | url: %s ' % (alternate_format.type,
                                                     alternate_format.url)
        # show thumbnails
        for thumbnail in entry.media.thumbnail:
            print 'Thumbnail url: %s' % thumbnail.url        
        print "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    else :
        pass

def PrintVideoFeed(feed):
    counter = 0    
    for entry in feed.entry:        
        PrintEntryDetails(entry)
        counter = counter+1
        #print counter   


def SearchAndPrint():
    max = 20    
    yt_service = gdata.youtube.service.YouTubeService()
    query = gdata.youtube.service.YouTubeVideoQuery()
    # OrderBy must be one of: published viewCount rating relevance 
    query.orderby = "relevance"
    query.racy = 'include'
    query.author = "tseries"
    query.max_results = 50
    index = 01
    for i in (range(max)):        
        query.start_index = index
        index = index + 50
        query.format = "5"
        feed = yt_service.YouTubeQuery(query)    
        PrintVideoFeed(feed)


SearchAndPrint()
print "**********************************************************"
print "Total Movies"
print count

解决方法:

如果我理解您的问题,那么您正在寻找app:control/yt:state标签.例如,如果视频仅限于在YouTube网站上播放,但您尝试通过嵌入式网址或非浏览器访问该视频,则会收到以下内容:

<app:control>
    <yt:state name="restricted" reasonCode="limitedSyndication">Syndication of this video was restricted.</yt:state>
</app:control>

您可以在输入对象中看到:

entry.control.FindExtensions('state')[0].attributes

这将是:

{'name': 'restricted', 'reasonCode': 'limitedSyndication'}

当然你需要使这个更强大 – 控制可能是无,它可能没有状态标签等.但你明白了.

我不认为您可以直接搜索状态的存在或不存在或特定值,但您可以使用fields参数在检索结果之前对结果进行后期过滤.文档实际上给出的示例是仅返回“不以任何方式限制回放的条目,这由< yt:state>元素的存在表示”:

entry[not(app:control/yt:state)]

我已经离开了(标题,媒体:组)部分,因为你想要默认标签,而不是有限集.

出于某种原因,并不总是发送fields参数.这可能是因为,正如docs所说,“字段参数目前仅用于实验性功能.”但无论如何,你可以自己检索所有内容并过滤控件.

标签:python,youtube-api,gdata-api
来源: https://codeday.me/bug/20190620/1246256.html