Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using HTTP, JSON, DataFrames # Julia v1.1
function makedataframe()
return DataFrame(title=String[], url=String[], videoid=String[], favcount=Int64[], dislikecount=Int64[],likecount=Int64[],viewcount=Int64[])
end
function _getpage(apikey, channelurl;
nextpagetoken="")
if nextpagetoken != ""
page1 = HTTP.request("GET", "https://www.googleapis.com/youtube/v3/search?key=$(apikey)&channelId=$(channelurl)&pageToken=$(nextpagetoken)&part=snippet,id&order=date&maxResults=50")
else
page1 = HTTP.request("GET", "https://www.googleapis.com/youtube/v3/search?key=$(apikey)&channelId=$(channelurl)&part=snippet,id&order=date&maxResults=50")
end
linkspage1 = JSON.parse(String(page1.body))
counter = 0
df = makedataframe()
for i in linkspage1["items"]
kind = i["id"]["kind"]
if kind == "youtube#video"
title = i["snippet"]["title"]
videoId = i["id"]["videoId"]
push!(df, (title, "https://www.youtube.com/watch?v=$(videoId)", videoId, 0, 0, 0, 0))
counter += 1
end
end
nextpage = linkspage1["nextPageToken"]
return (linkspage1, nextpage, counter, df)
end
function getall(df::DataFrame ; limit=100)
counter = 0
np = ""
while counter < limit
(lp1, np, c, resultsdf) = _getpage(ENV["API_KEY"], ENV["CHANNEL_URL"], nextpagetoken=np)
counter += c
df = vcat(df, resultsdf)
sleep(1) # might be unnecessary
end
return df
end
function addvideostats!(df::DataFrame)
apikey = ENV["API_KEY"]
for row in eachrow(df)
videoid = row[3]
rawvideostats = HTTP.get("https://www.googleapis.com/youtube/v3/videos?key=$(apikey)&id=$(videoid)&part=snippet,id,statistics")
jvs = JSON.parse(String(rawvideostats.body))
videostats = jvs["items"][1]["statistics"]
row[:favcount] = parse(Int, videostats["favoriteCount"])
row[:dislikecount] = parse(Int, videostats["dislikeCount"])
row[:likecount] = parse(Int, videostats["likeCount"])
row[:viewcount] = parse(Int, videostats["viewCount"])
end
end
ENV["API_KEY"] = "add your key here"
ENV["CHANNEL_URL"] = "UC9IuUwwE2xdjQUT_LMLONoA" # JuliaLang
df = makedataframe()
isempty(df) && (resultsDF = getall(df, limit=200))
addvideostats!(resultsDF)
@cormullion
Copy link
Author

cormullion commented Apr 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment