Ekaterina Premudraya
Ekaterina Premudraya

Reputation: 139

Getting titles from playlist in the youtube api(python)

Problem is to get titles of each item in video list and print it, but print function doesn't work in function and doesn't see variable out of function.

A little more edited text

from __future__ import unicode_literals
import requests
import json

url = "https://www.youtube.com/chanel/UC1ctnuRGUUXe04EgbsYXwPQ"
PLAYLIST_ID = 'PLg5SS_4L6LYueOJFm-IdWSuLiYwcASkH-'
APIKEY = '***********'


def get_items():

        YOUTUBE_URI = 'https://www.googleapis.com/youtube/v3/playlistItems'
        FORMAT = {'key': APIKEY, 'part': 'snippet', 'playlistId': PLAYLIST_ID, 'maxResults': 50}
        FORMAT_YOUTUBE_URI = (YOUTUBE_URI, FORMAT)
        content = requests.get(FORMAT_YOUTUBE_URI).json()

        video_list = []
        keys = 'id', 'title', 'description'

        for item in content('items'):
            id = item.get('id').get('videoId')
            title = item.get('snippet').get('title')
            description = item.get('snippet').get('description')
            values = id, title, description

            if id:
                video_item = dict(zip(keys, values))
                video_list.append(video_item)
                print('\n'.join(video_list))
                return video_list

Upvotes: 1

Views: 667

Answers (1)

user1785721
user1785721

Reputation:

content variable is never used and from what I can see is where the data is. Besides vide_list is used but never filled with useful data, just initialized with an empty list []

I highly recommend you use some sort of editor to help you to easily spot such things.

After your EDIT:

content = requests.get(FORMAT_YOUTUBE_URI).json()

is the problematic thing, not the logic that come, so you should concentrate in be able to make a Youtube API request right and then see how to process the result.

Upvotes: 1

Related Questions