CashCow
CashCow

Reputation: 31445

How to get Channel Name for Video, youtubeAPI, Python

Firstly the previous "answer" to a similar question used the deprecated Youtube Api V2. Also none of the answers showed how you can do it with Python.

My code is:

def youtube_search(item):


  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)

  # Call the search.list method to retrieve results matching the specified
  # query term.
  search_response = youtube.search().list(
    q=item,
    part="id,snippet",
    maxResults=6
  ).execute()


  videoCount = 0;
  title = ""
  channelId = ""
  channelName = ""
  # Add each result to the appropriate list, and then display the lists of
  # matching videos, channels, and playlists.
  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videoCount += 1
      if videoCount == 1:
         snippet = search_result["snippet"]
         title = snippet["title"]
         channelId = snippet["channelId"]
         channelName = snippet["channelTitle"] 

#  print "Videos:\n", "\n".join(videos), "\n"
  print( item, videoCount, title, channelId, channelName )

So essentially from the first returned video I want to know the title of the video and the name of the channel. It is the name in particular I am looking for. For this search string:

r'''"Joe Keegan" "Pick Myself Up"'''

My result is:

('"Joe Keegan" "Pick Myself Up"', 1, u'Pick Myself Up', u'UCThUpINg-JADDOHdkz_4-jw', u'')

The Channel Name I am looking for here is "Various Artists - Topic". In particular I have a huge list and am trying to eliminate any with that channel name (which is a lot). There are however a lot of channel ids that use that name. channelTitle doesn't give me what I am looking for.

Upvotes: 2

Views: 2554

Answers (1)

CashCow
CashCow

Reputation: 31445

While all this was going on I was trying to solve it myself and have merged a few other answers around the place to come up with a solution that works. It might not be the best one so please if you have a better one let me know. But it may be useful here for others who stumble across trying to do the same:

Essentially I did a URL "Get" request on the video id and parsed the JSON response. Oh, Python is such an easy language to use sometimes...

  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videoCount += 1
      if videoCount == 1:
         snippet = search_result["snippet"]
         title = snippet["title"]
         videoId = search_result["id"]["videoId"] 
         channelId = snippet["channelId"]

  urlBase = "https://gdata.youtube.com/feeds/api/videos/"
  if videoCount > 0:
     url = urlBase + videoId + "?v=2&alt=json"
     r = requests.get(url)
     metadata = r.json()
     channelName = metadata["entry"]["author"][0]["name"]["$t"]

print( item, videoCount, title, channelId, channelName )

And of course I don't really need channelId...

Upvotes: 1

Related Questions