rrr*_*360 0 python google-api youtube-api youtube-data-api raspberry-pi3
我正在尝试获得一个基于RaspberryPi3 Python的鸟舍,以阅读其自己上传的视频的受欢迎程度(这使它能够推断出应该删除的视频,并避免数百个上传的文件)。我认为阅读#views /#likes的最好方法是使用yt_analytics_report.py
当我输入时,它总是返回0值:
当我输入:
$python yt_analytics_report.py --filters="video==bb_o--nP1mg"
要么
$python yt_analytics_report.py --filters="channel==UCGXIq7h2UeFh9RhsSEsFsiA"
输出为:
$ python yt_analytics_report.py --filters =“ video == bb_o--nP1mg” {'metrics':'views,estimatedMinutesWatched,averageViewDuration','filters':'video == bb_o--nP1mg','ids:' channel == MINE','end_date':'2018-01-12','start_date':'2018-01-06'}
请访问此URL以授权此应用程序:[注意:此处是带有其他序列的url等。结果]:
观看次数估计数MinutesWatched averageViewDuration 0.0 0.0 0.0
我是新来的;最近3天,我一直在测试各种过滤器,但结果始终相同。我想我做错了严重的事情。(自动传感器触发的)视频上传效果非常好,因此我认为根本原因与我使用yt-analytics示例的方式有关。如有任何关于rootcause的建议或替代方法,以检索自己上传的youtube的#views /#likes。
经过几天的尝试,我找到了一种解决方案,该方法如何使用Python和Youtube API v3生成一个列表,其中包含我自己的YouTube频道的上传视频的观看次数,喜欢次数等。我想分享完整的代码,以防万一有人遇到同样的挑战。该代码包含备注和对其他信息的引用。请注意,使用API会消耗API信用...这意味着(当您连续或频繁运行此脚本时)您可能会用完Google设置的每日最大API信用数量。
# This python 2.7.14 example shows how to retrieve with Youtube API v3 a list of uploaded Youtube videos in a channel and also
# shows additional statistics of each individual youtube video such as number of views, likes etc.
# Please notice that YOU HAVE TO change API_KEY and Youtube channelID
# Have a look at the referred examples to get to understand how the API works
#
# The code consists of two parts:
# - The first part queries the videos in a channel and stores it in a list
# - The second part queries in detail each individual video
#
# Credits to the Coding 101 team, the guy previously guiding me to a query and Google API explorer who/which got me on track.
#
# RESULTING EXAMPLE OUTPUT: The output of the will look a bit like this:
#
# https://www.youtube.com/watch?v=T3U2oz_Y8T0
# Upload date:        2018-01-13T09:43:27.000Z
# Number of views:    8
# Number of likes:    2
# Number of dislikes: 0
# Number of favorites:0
# Number of comments: 0
#
# https://www.youtube.com/watch?v=EFyC8nhusR8
# Upload date:        2018-01-06T14:24:34.000Z
# Number of views:    6
# Number of likes:    2
# Number of dislikes: 0
# Number of favorites:0
# Number of comments: 0
#
#
import urllib #importing to use its urlencode function
import urllib2 #for making http requests
import json #for decoding a JSON response
#
API_KEY = 'PLACE YOUR OWN YOUTUBE API HERE'                 # What? How? Learn here: https://www.youtube.com/watch?v=JbWnRhHfTDA 
ChannelIdentifier = 'PLACE YOUR OWN YOUTUBE channelID HERE' # What? How? Learn here: https://www.youtube.com/watch?v=tf42K4pPWkM
#
# This first part will query the list of videos uploaded of a specific channel
# The identification is done through the ChannelIdentifier hwich you have defined as a variable
# The results from this first part will be stored in the list videoMetadata. This will be used in the second part of the code below.
#
# This code is based on the a very good example from Coding 101 which you can find here:https://www.youtube.com/watch?v=_M_wle0Iq9M
#
url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='+ChannelIdentifier+'&maxResults=50&type=video&key='+API_KEY
response = urllib2.urlopen(url) #makes the call to YouTube
videos = json.load(response) #decodes the response so we can work with it
videoMetadata = [] #declaring our list
for video in videos['items']:
  if video['id']['kind'] == 'youtube#video':
      videoMetadata.append(video['id']['videoId']) #Appends each videoID and link to our list
#
# In this second part, a loop will run through the listvideoMetadata
# During each step the details a specific video are retrieved and displayed
# The structure of the API-return can be tested with the API explorer (which you can excecute without OAuth):
# https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=snippet%252CcontentDetails%252Cstatistics&id=Ks-_Mh1QhMc&_h=1&
#
for metadata in videoMetadata:
  print "https://www.youtube.com/watch?v="+metadata  # Here the videoID is printed
  SpecificVideoID = metadata
  SpecificVideoUrl = 'https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id='+SpecificVideoID+'&key='+API_KEY
  response = urllib2.urlopen(SpecificVideoUrl) #makes the call to a specific YouTube
  videos = json.load(response) #decodes the response so we can work with it
  videoMetadata = [] #declaring our list
  for video in videos['items']: 
    if video['kind'] == 'youtube#video':
        print "Upload date:        "+video['snippet']['publishedAt']    # Here the upload date of the specific video is listed
        print "Number of views:    "+video['statistics']['viewCount']   # Here the number of views of the specific video is listed
        print "Number of likes:    "+video['statistics']['likeCount']   # etc
        print "Number of dislikes: "+video['statistics']['dislikeCount']
        print "Number of favorites:"+video['statistics']['favoriteCount']
        print "Number of comments: "+video['statistics']['commentCount']
        print "\n"