Thursday, 15 January 2015

python - Retrieving number of subscriptions and subscribers of specific set of users -



python - Retrieving number of subscriptions and subscribers of specific set of users -

i'm trying retrieve number of subscriptions , subscribers of specific set of users. i'm using youtube api python.

i wrote next code number of subscriptions. code reads users' ids list 1 one, counts number of subscriptions , writes id , number in csv file. doesn't work properly. after few first user's stops writing numbers in file, , numbers not right anyhow. think there should simpler mess.

thanks,i appreciate suggestions , 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): = 0 entry in subscription_feed.entry: = +1 homecoming usersub = open ('usersubscribtions.csv','w') users=[] userlist = open("user_ids_noduplicates1.txt","r") text1 = userlist.readlines() 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()

if trying total number of subscribers, don't need count items in feed - supplied value in v3 of info api.

you need create phone call channels resource channelid of user 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 can see, subscribercount included in response.

python youtube-api

No comments:

Post a Comment