In this article we will see how we can get the youtube playlist items in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A playlist in YouTube is a list, or group, of videos that plays in order, one video after the other. When one video finishes playing, the next starts automatically so you don’t have to click or search to start playing a new video. Playlist items are individual videos total items are the total videos present in the playlist.
We can get the playlist from youtube in pafy with the help of get_playlist
method, below is the command given to do this
pafy.get_playlist(url)
The playlist url should exist on youtube as it get the information of those videos which are present on the youtube. YouTube is an American online video-sharing platform.
Steps to get the playlist ID
1. Import the pafy module
2. Get the playlist with the help of URL of playlist
3. Return play list work as dictionary so use ‘items’ as key with the return playlist
4. Store the result in variable and print it
Below is the implementation
# importing pafy import pafy # url of playlist # getting playlist playlist = pafy.get_playlist(url) # getting playlist items items = playlist[ "items" ] # printing single item print (items[ 1 ]) |
Output :
{'pafy': Pafy object: AfxHGNRtFac [Write a program to print all permutations of ..], 'playlist_meta': {'added': '06/01/2016', 'is_cc': False, 'is_hd': True, 'likes': 1111, 'title': 'Write a program to print all permutations of a given string | neveropen', 'views': '334, 905', 'rating': 4.0, 'author': 'neveropen', 'user_id': '0RhatS1pyxInC00YKjjBqQ', 'privacy': 'public', 'start': 0.0, 'dislikes': 227, 'duration': '11:52', 'comments': '116', 'keywords': '', 'thumbnail': 'https://i.ytimg.com/vi/AfxHGNRtFac/default.jpg', 'cc_license': False, 'category_id': 22, 'description': 'Explanation for the Article: https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/\n\nThis video is contributed by Sephiri.\n#neveropen', 'encrypted_id': 'AfxHGNRtFac', 'time_created': 1490874673, 'time_updated': None, 'length_seconds': 712, 'end': 712}}
Another example
# importing pafy import pafy # url of playlist # getting playlist playlist = pafy.get_playlist(url) # getting playlist items items = playlist[ "items" ] # printing single item print (items[ 2 ]) |
Output :
{'pafy': Pafy object: GY-KULykGaw [Find subarray with given sum | Set 1 (Non-neg..], 'playlist_meta': {'added': '27/09/2017', 'is_cc': True, 'is_hd': True, 'likes': 192, 'title': 'Find subarray with given sum | Set 1 (Non-negative Numbers) | neveropen', 'views': '32, 716', 'rating': 3.0, 'author': 'neveropen', 'user_id': '0RhatS1pyxInC00YKjjBqQ', 'privacy': 'public', 'start': 0.0, 'dislikes': 78, 'duration': '2:50', 'comments': '24', 'keywords': 'neveropen Programming Algorithms "Data Structures" Coding Tutorial array "non-negative numbers"', 'thumbnail': 'https://i.ytimg.com/vi/GY-KULykGaw/default.jpg', 'cc_license': False, 'category_id': 27, 'description': "Find Complete Code at neveropen Article: https://www.geeksforgeeks.org/find-subarray-with-given-sum/\n\nPractice Problem Online Judge: http://practice.geeksforgeeks.org/problems/subarray-with-given-sum/0\n\nThis video is contributed by Shubham Kumar\n\nPlease Like, Comment and Share the Video among your friends.\n\nAlso, Subscribe if you haven't already! :)", 'encrypted_id': 'GY-KULykGaw', 'time_created': 1506577926, 'time_updated': None, 'length_seconds': 170, 'end': 170}}