Skip to content

Instantly share code, notes, and snippets.

@20chan
Created November 13, 2019 01:53
Show Gist options
  • Save 20chan/cd6ef9f23784313f478152dbff36d4a5 to your computer and use it in GitHub Desktop.
Save 20chan/cd6ef9f23784313f478152dbff36d4a5 to your computer and use it in GitHub Desktop.
dotnet try jupyter notebook example for C# youtube music control with Puppeteersharp
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"Installing package PuppeteerSharp.done!"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Successfully added reference to package PuppeteerSharp, version "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget:PuppeteerSharp\"\n",
"using System;\n",
"using System.Threading.Tasks;\n",
"using PuppeteerSharp;"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"var browser = await Puppeteer.LaunchAsync(new LaunchOptions {\n",
" Headless = false,\n",
" DefaultViewport = null,\n",
" Args = new[] {\n",
" \"--window-size=400,800\",\n",
" },\n",
"});\n",
"var page = await browser.NewPageAsync();\n",
"var resp = await page.GoToAsync(\"https://music.youtube.com/watch?v=kg1BljLu9YY\");"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"var playBtn = await page.WaitForSelectorAsync(\"#play-pause-button\");\n",
"var prevBtn = await page.WaitForSelectorAsync(\".previous-button\");\n",
"var nextBtn = await page.WaitForSelectorAsync(\".next-button\");\n",
"var title = await page.WaitForSelectorAsync(\".title.ytmusic-player-bar\");\n",
"var progress = await page.WaitForSelectorAsync(\"#progress-bar\");"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"await playBtn.ClickAsync();"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"103"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await page.EvaluateFunctionAsync<int>(\"() => document.getElementById('progress-bar').getAttribute('aria-valuenow')\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment