2010-06-19 3 views
1

아래 코드를 통해 YouTube의 재생 목록에 항목을 추가하려고합니다. 내가 재생 목록 URI (http://gdata.youtube.com/feeds/api/users/nashrafeeg/playlists/0F4EF4B14F514476?client=Reddit+playlist+maker)를 AddPlaylistVideoEntryToPlaylist 메서드에 전달할 때 재생 목록 가져 오기 방법에서 얻을 때 오류가 Invalid request URI이라고 표시됩니다. 이 문제를 해결하는 가장 좋은 방법은 무엇입니까?youtube python api를 통해 재생 목록 항목 추가

import urllib,re 
import gdata.youtube 
import gdata.youtube.service 

class reddit(): 

    def __init__(self, rssurl ='http://www.reddit.com/r/chillmusic.rss'): 
     self.URL = rssurl 
     self._downloadrss() 

    def _downloadrss(self): 
     if self.URL.endswith('.rss'): 
      # Downloadd the RSS feed of the subreddit - save as "feed.rss" 
      try: 
       print "Downloading rss from reddit..." 
       urllib.urlretrieve (URL, "feed.rss") 
      except Exception as e: 
       print e 

    def clean(self): 
     playList = open("feed.rss").read() 
     links = re.findall(r'(http?://www.youtube.com\S+)', playList) 
     for link in links: 
      firstPass = link.replace('">[link]</a>', '') 
      secondPass = firstPass.replace('&fmt=18', '') 
      thirdpass = secondPass.replace('&feature=related', '') 
      finalPass = thirdpass.replace('http://www.youtube.com/watch?v=', '') 
      print thirdpass, "\t Extracted: ", finalPass 
      return finalPass 


class google(): 
    def __init__(self, username, password): 
     self.Username = username 
     self.password = password 
     #do not change any of the following 
     self.key = 'AI39si5DDjGYhG_1W-8n_amjgEjbOU27sa0aw2RQI5gOaoK5KqCD2Fzffbkh8oqGu7CqFQLLQ7N7wK0gz7lrTQbd70srC72Niw' 
     self.appname = 'Reddit playlist maker' 
     self.service = gdata.youtube.service.YouTubeService() 

    def authenticate(self): 
     self.service.email = self.Username 
     self.service.password = self.password 
     self.service.developer_key = self.key 
     self.service.client_id = self.appname 
     self.service.source = self.appname 
     self.service.ssl = False 
     self.service.ProgrammaticLogin() 

    def get_playlists(self): 
     y_playlist = self.service.GetYouTubePlaylistFeed(username='default') 
     l = [] 
     k = [] 
     for p in y_playlist.entry:  
      k=[] 
      k=[p.link[1].href, p.title.text] 
      l.append(k) 
     return l 

    def get_playlist_id_from_url(self, href): 
      #quick and dirty method to get the playList id's 
      return href.replace('http://www.youtube.com/view_play_list?p=','') 

    def creat_playlist(self, name="Reddit list", disc ="videos from reddit"): 
     playlistentry = self.service.AddPlaylist(name, disc) 
     if isinstance(playlistentry, gdata.youtube.YouTubePlaylistEntry): 
      print 'New playlist added' 
      return playlistentry.link[1].href 

    def add_video_to_playlist(self,playlist_uri,video): 
     video_entry = self.service.AddPlaylistVideoEntryToPlaylist(
     playlist_uri, video) 
     if isinstance(video_entry, gdata.youtube.YouTubePlaylistVideoEntry): 
      print 'Video added' 
URL = "http://www.reddit.com/r/chillmusic.rss" 

r = reddit(URL) 
g = google('[email protected]', 'xxxx') 
g.authenticate() 

def search_playlist(playlist="Reddit list3"): 
    pl_id = None 
    for pl in g.get_playlists(): 
     if pl[1] == playlist: 
      pl_id = pl[0] 
      print pl_id 
      break 
    if pl_id == None: 
     pl_id = g.creat_playlist(name=playlist) 
    return pl_id  
pls = search_playlist() 
for video_id in r.clean(): 
    g.add_video_to_playlist(pls, video_id) 
+0

나는이 일을 할 수 있었고, 목표는 아이폰에서 동일한 개념을 적용하려고 시도했지만 성공하지 못했습니다. – Swati

답변

0

어떻게 얻는 지 모르지만 '/ users/[username]'의 playlist_uri를 제거하면 작동합니다.

예 : playlist_uri

http://gdata.youtube.com/feeds/api/users/[username]/playlists/[long_id]

될해야

HTTP ://gdata.youtube.com/feeds/api/playlists/[long_id]