How can I get the total youtube video view count for videos in a specific category for a date range?
I thought of using the videos/list [https://www.googleapis.com/youtube/v3/videos] and just iterating through that to get the total count but the filtering is only allowing to at best to get the most popular videos.
You can use the statistics part of the Youtube API which contains the viewCount. You can retrieve the number of times the video has been viewed using statistics.viewCount. For the date range, you can include the order parameter date to specifies the method that will be used to order resources. Resources are sorted in reverse chronological order based on the date they were created.
Here a sample request for statistics:
GET https://www.googleapis.com/youtube/v3/videos?part=statistics&id=Q5mHPo2yDG8&key={YOUR_API_KEY}
Sample response:
"statistics": {
"viewCount": unsigned long,
"likeCount": unsigned long,
"dislikeCount": unsigned long,
"favoriteCount": unsigned long,
"commentCount": unsigned long
},
Related
I have a simple timeseries table:
{
"n": "EXAMPLE", # Name, Hash Key
"t": 1640893628, # Unix Timestamp, Range Key
"v": 10 # Value being stored
}
Every 15 minutes I will poll data and insert into the table. If I want to query values between a 24-hour period, this works well - this would equate to a total of 96 records.
Now, say I want to query a larger timespan - 1 or 2 years. This is now tens of thousands of records, and (in my opinion) impractical to do regularly. This will require multiple queries to retrieve larger time ranges which would negatively impact response times as well as being much more costly.
I have thought of a couple of potential solutions to this problem:
1. Replicate data in another table, with larger increments. A table with a single record every 6 hours, for example.
2. Have another table to store common query results, such as records for "EXAMPLE" for the past week, month, and year (respectively). I would periodically update records in the new table to hold every N'th record in the main table (a total of 100). Something like:
{
"n": "EXAMPLE#WEEKLY",
"v": [
{
"t": 1640893628,
"v": 10
},
{
"t": 1640993628,
"v": 15
},
... 98 more.
]
}
I believe #2 is a solid approach. It seems to me like this would be a common enough problem, so I would love to hear about how other people have approached it.
More options present themselves if you can convert your unix timestamps into ISO 8601-type strings like 2021-12-31T09:27:58+00:00.
If so, DynamoDB's begins_with key condition expression lets us query for discrete calendar time buckets. December 2021, for example,
is queryable using n = id1 AND begins_with(t, "2021-12"). Same for days and hours. We can take this one step further by adding
other periods in indexes.
Some rolling windows are possible, too: n = id1 AND t > [24 hours ago] gives us last 24h.
n (PK) t (SK) hour_bucket (LSI1 SK) week (LSI2 SK)
id1 2021-12-31T10:45 2021-12-31T09-12 2021-52
id1 2021-12-31T13:00 2021-12-31T13-15 2021-52
id1 2022-06-01T22:00 2022-06-01T22-24 2022-01
If you are looking for arbitrary time-series queries, you might consider Athena, as the other answer suggested, or AWS's serverless
Timestream, which is a "purpose-built time series database that makes it easy to store and analyze trillions of time series data points per day. "
You could export the table to Amazon S3 and run Amazon Athena on the exported data. Here’s a blog post describing the process: https://aws.amazon.com/blogs/aws/new-export-amazon-dynamodb-table-data-to-data-lake-amazon-s3/
I am trying to get the number of views of every video in the channel but measured from the release date for 90 days. I would like something like this:
ID, Release date, 0, 1, 2, 3, 4, ...... 90
Video1, 2020-04-03, 100, 40, 20, 10, ....., 0
Video2, 2020-06-03, 100, 40, 20, 10, ....., 0
...
Is there a way to achive this?
You can achieve this by using a combination of YouTube Data API and YouTube Analytics API requests.
Firstly, query the Data API to retrieve all videos of a channel from the search endpoint.
Set the query parameters to:
part:snippet
channelId:CHANNEL_ID
You will get a list of videos with their details as a response, among others the videoId and publishedAt values. This is what you will need for the second query.
Secondly, query the Analytics API to retrieve view stats of the videos as a video report (differs for content owners and just channel owners).
Set the query parameters to:
dimensions:video,day
filters:video==comma separated list of video ids
metric:views
startDate:earliest published date of all videos
endDate:latest published date of all videos + 90
ids:refer to docs for your owner type
The scope you need for this query is:
yt-analytics.readonly
This query will return views for each video aggregated by day for the date span you provided. After you get the response, you can filter out just the first 90 days for each video.
Alternatively, you can write a separate query for each video with the specified start date and end date, getting a result that does not need to be filtered.
Using the Advanced Tab in YouTube Studio > Analytics you can download your statistics as a CVS-File or open in Google Tables.
That's the quickest way possible.
Hope it helps :)
I was struck with a problem which needs the total view time on a particular Youtube video.
I checked with the Youtube-data API and it seems to offer the following statistics:
"statistics": {
"viewCount": "xyz",
"likeCount": "a",
"dislikeCount": "b",
"favoriteCount": "c",
"commentCount": "d"
}
I expect the API to give me the statistic on the total view time of a particular video of interest.
Executing my code (using the Google Adwords Library to build an oauth user etc) works when using a query with Month as a segment
String query = "SELECT EffectiveFinalUrl, Cost, Clicks, Impressions,
Month FROM FINAL_URL_REPORT";
But it aggregates across all time, split into months. I am trying to pull in the data as daily aggregates. The documentation has both Month and Date (splitting data into yyyy-MM-dd) as a segment you can include in the query, but Date does not work.
String query = "SELECT EffectiveFinalUrl, Cost, Clicks, Impressions,
Date FROM FINAL_URL_REPORT";
Results in Error 400: Bad Request.
Am I missing something in the documentation that tells me how to resolve this?
Adwords Final_URL_Report Documentation
Try set a date range by adding something like DURING ... to your query.
I would like to use the Youtube data API search to retrieve the most viewed videos on Youtube. However, for some reason, my results are missing some videos.
Here is the API call: https://www.googleapis.com/youtube/v3/search?part=snippet&key={YOUR-API-KEY}&alt=json&type=video&order=viewCount&maxResults=50
The 13 first results returned by the API are:
9bZkp7q19f0
RgKAFK5djSk
fRh_vgS2dFE
OPf0YbXqDm0
e-ORhEE9VVg
KYniUCGPGLs
YQHsXMglC9A
nfWlot6h_JM
NUsoVlDFqZg
HP-MbfHFUqs
CevxZvSJLk8
7PCkvCPvDXk
0KSOMA3QBU0
These should be the 13 most viewed videos on Youtube at the time I queried it.
However, looking at this Youtube-made playlist:
https://www.youtube.com/playlist?list=PLirAqAtl_h2r5g8xGajEwdXd3x1sZh8hC
I can see that the video YqeW9_5kURI, which has 1.7 billion views, should arrive in 9th position in the list returned by the API, but it doesn't. Actually, it never appears among the 500 (max) videos returned by the API.
UPDATE
Since results change every day, I did more comprehensive tests with the search API.
Here is the result of the API call I mentioned above from yesterday dec. 13th (first 10 results):
9bZkp7q19f0
RgKAFK5djSk
fRh_vgS2dFE
OPf0YbXqDm0
e-ORhEE9VVg
KYniUCGPGLs
YQHsXMglC9A
nfWlot6h_JM
NUsoVlDFqZg
HP-MbfHFUqs
And here is the result obtained today, dec. 14th (again, first 10 results):
9bZkp7q19f0
RgKAFK5djSk
fRh_vgS2dFE
KYniUCGPGLs
nfWlot6h_JM
NUsoVlDFqZg
YqeW9_5kURI
HP-MbfHFUqs
CevxZvSJLk8
09R8_2nJtjg
I am absolutely sure that the API call did not change between these two dates, code is exactly the same.
First anomaly: video 7 in second call (YqeW9_5kURI) does not appear in first call (this was my original post example), although its 1.7 billion views were definitely not done overnight.
Second anomaly: videos 4, 5, 7 in first call (OPf0YbXqDm0, e-ORhEE9VVg, YQHsXMglC9A) do not appear in second call, although they are still available on Youtube and still more viewed than video 5 (nfWlot6h_JM), for instance.
These anomalies repeat very often over larger sets of results.
To sum up, the search API does not seem to yield deterministic results with no query string and viewCount order, is this expected behaviour?
Or can you help me figure out what could be the reason for this?
Thanks in advance for your help, any pointers will be greatly appreciated.
Try to include filtering parameters like chart. The chart parameter identifies the chart that you want to retrieve. mostPopular retruns the most popular videos for the specified content region and `video category.
If successful, this method returns a response body with the following structure:
{
"kind": "youtube#videoListResponse",
"etag": etag,
"nextPageToken": string,
"prevPageToken": string,
"pageInfo": {
"totalResults": integer,
"resultsPerPage": integer
},
"items": [
video Resource
]
}