2011-12-29 59 views
1

我已經用我想要使用YouTube數據API的方式撞牆了。我有一個試圖充當「聚合器」的用戶帳戶,根據類別將各種其他頻道的視頻添加到大約15個播放列表中的一箇中。我的問題是,我無法將所有這些視頻合併到一個Feed中,因爲它們屬於各種YouTube用戶。我想把它們都放到一個列表中,這樣我就可以按照最新和最流行的方式對這個主列表進行排序,以便在我的web應用中填充不同的視圖。檢索YouTube用戶添加到所有播放列表的所有視頻

我怎樣才能得到所有用戶已添加到他們的任何播放列表的視頻列表?

YouTube必須跟蹤這類內容,因爲如果您進入任何用戶頁面的「Feed」部分,它都會爲您提供一系列活動,其中包括視頻添加到播放列表中。

要清楚,我不希望獲取的由眼前這個用戶上傳的視頻列表,以便http://gdata.../<user>/uploads將無法​​正常工作。由於有許多不同的播放列表,http://gdata.../<user>/playlists也不起作用,因爲每次我想要檢查新視頻時,我都需要提出約15個請求。

似乎有沒有辦法恢復的用戶已添加到他們所有的播放列表所有視頻列表。有人可以想出一種可能會忽略的方法嗎?

回答

0

就像這樣從檢索播放列表中的YouTube鏈接。它仍然需要改進。

import urllib2 
import xml.etree.ElementTree as et 
import re 
import os 

more = 1 
id_playlist = raw_input("Enter youtube playlist id: ") 
number_of_iteration = input("How much video links: ") 
number = number_of_iteration/50 
number2 = number_of_iteration % 50 
if (number2 != 0): 
    number3 = number + 1 
else: 
    number3 = number 
start_index = 1 

while more <= number3: 
    #reading youtube playlist page 
    if (more != 1): 
      start_index+=50 

    str_start_index = str(start_index) 
    req = urllib2.Request('http://gdata.youtube.com/feeds/api/playlists/'+ id_playlist  + '?v=2&&start-index=' + str_start_index + '&max-results=50') 
    response = urllib2.urlopen(req) 
    the_page = response.read() 

    #writing page in .xml 
    dat = open("web_content.xml","w") 
    dat.write(the_page) 
    dat.close() 

    #searching page for links 
    tree = et.parse('web_content.xml') 
    all_links = tree.findall('*/{http://www.w3.org/2005/Atom}link[@rel="alternate"]') 

    #writing links + attributes to .txt 
    if (more == 1): 
      till_links = 50 
    else: 
      till_links = start_index + 50 

    str_till_links = str(till_links) 
    dat2 = open ("links-"+ str_start_index +"to"+ str_till_links +".txt","w") 
    for links in all_links: 
      str1 = (str(links.attrib) + "\n") 
      dat2.write(str1)  
    dat2.close() 

    #getting only links 
    f = open ("links-"+ str_start_index +"to"+ str_till_links +".txt","r") 
    link_all = f.read() 
    new_string = link_all.replace("{'href': '","") 
    new_string2 = new_string.replace("', 'type': 'text/html', 'rel': 'alternate'}","") 
    f.close() 

    #writing links to .txt 
    f = open ("links-"+ str_start_index +"to"+ str_till_links +".txt","w") 
    f.write(new_string2) 
    f.close() 

    more+=1 

os.remove('web_content.xml') 
print "Finished!" 
相關問題