Created
December 14, 2022 15:46
-
-
Save guifeliper/c8c79d312ea258c1ad776d2cbd919620 to your computer and use it in GitHub Desktop.
Youtube Heatmap in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import asyncio | |
from pyppeteer import launch | |
async def get_html(url): | |
browser = await launch() | |
page = await browser.newPage() | |
await page.goto(url) | |
html = await page.content() | |
await page.close() | |
await browser.close() | |
return html | |
async def get_yt_initial_data(html): | |
yt_duty_data = html.split('">var ytInitialData = ', 3) | |
yt_initial_data = json.loads(yt_duty_data[1].split(";</script>", 2)[0]) | |
with open("assets/ytInitialData.json", "w") as file: | |
json.dump(yt_initial_data, file, indent=4) | |
return yt_initial_data | |
async def get_json_from_html(url): | |
html = await get_html(url) | |
return await get_yt_initial_data(html) | |
async def getHeatMap(url): | |
try: | |
ytInitialData = await get_json_from_html(url) | |
markerMap = ytInitialData["playerOverlays"]["playerOverlayRenderer"]["decoratedPlayerBarRenderer"]["decoratedPlayerBarRenderer"]["playerBar"]["multiMarkersPlayerBarRenderer"]["markersMap"].pop() | |
mostReplayed = markerMap["value"]["heatmap"]["heatmapRenderer"]["heatMarkers"] | |
return mostReplayed | |
except Exception as error: | |
print("I am sorry, I could not find the data \n", error) | |
videoUrl = "https://www.youtube.com/watch?v=95PnFRjh-IE" | |
test = asyncio.run(getHeatMap(videoUrl)) | |
print(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment