3

I'm trying to retrieve the number of subscriptions and subscribers of specific set of users. I'm using YouTube API for Python.

I wrote the following code for number of subscriptions. This code reads the users' IDs from a list one by one, counts number of their subscriptions and writes the ID and the number in a CSV file. But it doesn't work properly. after the few first user's it stops writing the numbers in the file, and the numbers are not all correct anyhow. I think there should be something simpler than this mess.

Thanks,I appreciate your suggestions and comments.

import os
import gdata.youtube
import gdata.youtube.service
import time


def GetUserUrl (username):

    yt_service = gdata.youtube.service.YouTubeService()
    uri = 'https://gdata.youtube.com/feeds/api/users/%s/subscriptions?max-results=50&start-index=1' % username
    subscription_feed = yt_service.GetYouTubeSubscriptionFeed(uri)
    T1 = GetUserSub(subscription_feed)
    final = 0
    j = 1
    total = 0
    while j<800:
      j = j + 50
      sj = str(j)
      uri = 'https://gdata.youtube.com/feeds/api/users/%s/subscriptions?max-results=50&start-index=' % username+sj
      subscription_feed = yt_service.GetYouTubeSubscriptionFeed(uri)
      T2 = GetUserSub(subscription_feed)
      total = total + T2

    final = total + T1
    usersub.writelines([str(username),',',str(final),'\n'])

def GetUserSub (subscription_feed):

  i = 0
  for entry in subscription_feed.entry:
    i = i +1
  return i

usersub = open ('usersubscribtions.csv','w')
users=[]
userlist = open("user_ids_noduplicates1.txt","r")
text1 = userlist.readlines()

for l in text1:
        users.append(l.strip().split()[0])
x = 0
while (x<len(users)):

 try:
    GetUserUrl(users[x])
    time.sleep(0.4)
    x = x+1
 except:
    usersub.writelines([str(users[x]),'\n'])
    x = x+1
    pass

usersub.close()
DMD
  • 103
  • 1
  • 8

1 Answers1

4

If you are just trying to get the total number of subscribers, you don't need to count items in a feed - it is a supplied value in v3 of the Data API.

You just need to make a call to the Channels resource with the channelId of the user you are looking up: https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCDsO-0Yo5zpJk575nKXgMVA&key={YOUR_API_KEY}

Response:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"O7gZuruiUnq-GRpzm3HckV3Vx7o/wC5OTbvm5Z2-sKAqmTfH4YDQ-Gw\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "id": "UCDsO-0Yo5zpJk575nKXgMVA",
   "kind": "youtube#channel",
   "etag": "\"O7gZuruiUnq-GRpzm3HckV3Vx7o/xRjATA5YtH9wRO8Uq6Vq4D45vfQ\"",
   "statistics": {
    "viewCount": "80667849",
    "commentCount": "122605",
    "subscriberCount": "4716360",
    "videoCount": "163"
   }
  }
 ]
}

As you can see, the subscriberCount is included in the response.

Matt Koskela
  • 5,269
  • 3
  • 26
  • 29
  • Thanks Matt for your answer. It sounds straightforward but I didn't exactly understand how it works. I went through the documentation: https://developers.google.com/youtube/v3/docs/channels. But I didn't understand how I should set the parameters (I think I should assign the users'IDs to 'id') for https://www.googleapis.com/youtube/v3/channels and make the call. – DMD Feb 19 '13 at 12:33
  • Check out my answer here to see how to get a channel's id when you only have their username: http://stackoverflow.com/questions/14760497/search-youtube-videos-by-author/14761305#14761305. The user's id is in this format: UCDsO-0Yo5zpJk575nKXgMVA – Matt Koskela Feb 19 '13 at 18:15