Browse Source

move playlist code to separate file

Tom Lee 9 years ago
parent
commit
fb10d6be44
2 changed files with 48 additions and 43 deletions
  1. 2 43
      youtube_upload/main.py
  2. 46 0
      youtube_upload/playlists.py

+ 2 - 43
youtube_upload/main.py

@@ -27,6 +27,7 @@ import youtube_upload.auth
 import youtube_upload.upload_video
 import youtube_upload.upload_video
 import youtube_upload.categories
 import youtube_upload.categories
 import youtube_upload.lib as lib
 import youtube_upload.lib as lib
+import playlists
 
 
 # http://code.google.com/p/python-progressbar (>= 2.3)
 # http://code.google.com/p/python-progressbar (>= 2.3)
 try:
 try:
@@ -154,49 +155,7 @@ def run_main(parser, options, args, output=sys.stdout):
                 youtube.thumbnails().set(videoId=video_id, media_body=options.thumb).execute()
                 youtube.thumbnails().set(videoId=video_id, media_body=options.thumb).execute()
 
 
             if options.playlist:
             if options.playlist:
-                # find playlist with given name
-                existing_playlist_id = None
-                playlists = youtube.playlists()
-                request = playlists.list(mine=True, part='id,snippet')
-                while request is not None:
-                    results = request.execute()
-                    for item in results['items']:
-                        if item.get('snippet', {}).get('title') == options.playlist:
-                            existing_playlist_id = item.get('id')
-
-                    # stop paginating playlists on first matching playlist title
-                    if existing_playlist_id is None:
-                        request = playlists.list_next(request, results)
-                    else:
-                        break
-
-                # create playlist, if necessary
-                if existing_playlist_id is None:
-                    playlists_insert_response = youtube.playlists().insert(part="snippet,status", body={
-                        "snippet": {
-                            "title": options.playlist
-                        },
-                        "status": {
-                            "privacyStatus": options.privacy
-                        }
-                    }).execute()
-                    existing_playlist_id = playlists_insert_response.get('id', None)
-
-                # something has gone wrong
-                if existing_playlist_id is None:
-                    debug('Error creating playlist')
-                else:
-                    # add video to playlist
-                    youtube.playlistItems().insert(part='snippet', body={
-                        "snippet": {
-                            "playlistId": existing_playlist_id,
-                            "resourceId": {
-                                "kind": "youtube#video",
-                                "videoId": video_id
-                            }
-                        }
-                    }).execute()
-                    debug("Added video to playlist '{0}'".format(options.playlist))
+                playlists.add_to_playlist(youtube, video_id, options)
 
 
             debug("Video URL: {0}".format(video_url))
             debug("Video URL: {0}".format(video_url))
             output.write(video_id + "\n")
             output.write(video_id + "\n")

+ 46 - 0
youtube_upload/playlists.py

@@ -0,0 +1,46 @@
+from lib import debug
+
+def add_to_playlist(youtube, video_id, options):
+    # find playlist with given name
+    existing_playlist_id = None
+    playlists = youtube.playlists()
+    request = playlists.list(mine=True, part='id,snippet')
+    while request is not None:
+        results = request.execute()
+        for item in results['items']:
+            if item.get('snippet', {}).get('title') == options.playlist:
+                existing_playlist_id = item.get('id')
+
+        # stop paginating playlists on first matching playlist title
+        if existing_playlist_id is None:
+            request = playlists.list_next(request, results)
+        else:
+            break
+
+    # create playlist, if necessary
+    if existing_playlist_id is None:
+        playlists_insert_response = youtube.playlists().insert(part="snippet,status", body={
+            "snippet": {
+                "title": options.playlist
+            },
+            "status": {
+                "privacyStatus": options.privacy
+            }
+        }).execute()
+        existing_playlist_id = playlists_insert_response.get('id', None)
+
+    # something has gone wrong
+    if existing_playlist_id is None:
+        debug('Error creating playlist')
+    else:
+        # add video to playlist
+        youtube.playlistItems().insert(part='snippet', body={
+            "snippet": {
+                "playlistId": existing_playlist_id,
+                "resourceId": {
+                    "kind": "youtube#video",
+                    "videoId": video_id
+                }
+            }
+        }).execute()
+        debug("Added video to playlist '{0}'".format(options.playlist))