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()