Get YouTube Page Title

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
denbartman
Posts: 4
Joined: 12 Apr 2021, 18:02

Get YouTube Page Title

12 Apr 2021, 18:35

Hi,

I'm trying to get the page title of a list of YouTube pages in AHK.

I'm using this function to get the webpage

Code: Select all

		whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
		whr.Open("GET", url, true)
		whr.Send()
		whr.WaitForResponse()
		_webPage := whr.ResponseText
This has been working for a long time, but now every page has the title "Before you continue to YouTube".
This is because youtube now shows a cookie page before showing the requested page.
Apparently blocking all cookies should make this page go away and open the requested page immediatly.

How could this be done with WinHttp?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Get YouTube Page Title

12 Apr 2021, 19:04

Perhaps this will work:

Code: Select all

url := "https://www.youtube.com/watch?v=fzlT80jQ3lo"

Whr := ComObjCreate("Msxml2.XMLHTTP.6.0")
Whr.Open("GET", url, false)
Whr.SetRequestHeader( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36"
                    . " (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63" )
Whr.Send()
status := Whr.status
if (status != 200)
   throw "HttpRequest error, status: " . status

Arr := Whr.responseBody
pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize)
length := arr.MaxIndex() + 1
html := StrGet(pData, length, "UTF-8")

Doc := ComObjCreate("htmlfile")
Doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
Doc.write(html)
title := Doc.querySelector("title").innerText

MsgBox, % title
denbartman
Posts: 4
Joined: 12 Apr 2021, 18:02

Re: Get YouTube Page Title

13 Apr 2021, 02:43

Hi,

With this code I get an error "msxml6.dll - Access denied".
When i use:

Code: Select all

Whr := ComObjCreate("Msxml2.ServerXMLHTTP")
The error is gone, but I get the same result.
All pages i check have the title "Before you continue to YouTube"

This is the title of the page YouTube shows to get your permission to store cookies.

This might be only a european issue, since in europe all websites have to show such (an annoying) page when they use cookies.
(Google search shows the same page).
In the browsers I only get this page the first time since after that the cookie is stored.
I only see this page when I use a private window, or when i load a page via AHK)
denbartman
Posts: 4
Joined: 12 Apr 2021, 18:02

Re: Get YouTube Page Title

13 Apr 2021, 03:47

I also tried this in Edge: https www.ghacks.net /2021/04/01/here-is-the-easiest-way-to-get-rid-of-googles-before-you-continue-to-youtube-prompt/ Broken Link for safety

After adding "consent.youtube.com" to the cookies edge has to block the page does not appear anymore.

Is there a way I can block this cookie in AHK?
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Get YouTube Page Title

13 Apr 2021, 11:15

I do not see any sense to use Msxml2 instead of WinHTTPRequest.
To bypass google check for agreement, add cookie CONSENT=YES+EN.
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: Get YouTube Page Title

13 Jun 2021, 15:25

get title with youtube-dl

Code: Select all

=========================== START ===================================
youtube-dl https://www.youtube.com/watch?v=iR0w0mWB3uA --encoding UTF-8 -e --get-duration --get-description =
李香蘭  Li Xiang Lan   1920 - 2014
An unforgettable and irreplaceable Li Xiang Lan.  A legendary Mandarin songs singer from the Shanghai era and later HK in the 1950s.
I am truly saddened by her passing at an old age of 94 years in Japan.
I grew up with her songs as far as I can remembered when still a very young child in Malaysia, my original country. Her songs are part of me !
May her soul rest in peace and her songs be eternally treasured in the Chinese peoples heart !
3:12
================================ ENDXX ================================

Code: Select all

;- test for youtube: get TITLE DURATION DESCRIPTION
;- needs : youtube-dl.exe   https://youtube-dl.org/latest/youtube-dl.exe
;-
;- modified=20210613
;- created =20210613

;--------- this command start for test ------------------------------------
url:="https://www.youtube.com/watch?v=iR0w0mWB3uA"
cmdx=youtube-dl %url% --encoding UTF-8 -e --get-duration --get-description
;cmdx=youtube-dl -U  ;- update youtube-dl.exe
;cmdx=youtube-dl -h  ;- HELP   youtube-dl.exe
;------------------------------------------------
;-
youtubedl:=a_scriptdir . "\youtube-dl.exe"         ;<<< youtube-dl.exe in a_scriptdir
ifexist,%youtubedl%
 goto,guitest
else
 {
 run,https://youtube-dl.org/latest/youtube-dl.exe  ;- downloads youtube-dl.exe
 exitapp
 }
;-
;-----------------------------------------------------------------
GUITEST:
#warn
#noenv
name1:="YOUTUBE_TEST"
FileEncoding, UTF-8
global name1
e:=""
;CPX:="cp" . DllCall("GetOEMCP", "UInt")        ;- get CODEPAGE e.g. CP850
;cpx:="cp65001"

setworkingdir,%a_scriptdir%
Gui,1:default
Gui,1: -DPIScale 
Gui,1:Color,Black,Black                        ; GUI black   / EDIT is BLACK
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
 Gui,1:Font,s12 cYellow,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
 Gui,1:Font,s10 cYellow,Lucida Console
else
 Gui,1:Font,s8 cYellow,Lucida Console
;=============================================================
x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*72)/xx,h:=(ha*60)/xx
Gui, Add, Edit, x%x%   y%y%  w%w% h%h% readonly vED1                  ;- here the output
x:=(wa*1)/xx,y:=(ha*63)/xx,w:=(wa*65)/xx,h:=(ha*6)/xx
Gui, Add, Edit, x%x%   y%y% w%w%  h%h% vED2,%cmdx%                    ;- add CMDx command above
x:=(wa*67)/xx,y:=(ha*63)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui, add, Edit, x%x%  y%y% w%w%  h%h% vED3,
x:=(wa*67)/xx,y:=(ha*66)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x%  y%y% w%w%  h%h% gPrintx, PRINT
x:=(wa*2)/xx,y:=(ha*2)/xx,w:=(wa*75)/xx,h:=(ha*75)/xx
Gui, Show,x%x% y%y% w%w% h%h%,%name1%
gosub,a0
GuiControl,1: Focus,ED2
;send,^{end}
return
;-------------------------
Guiclose:
Exitapp
esc::exitapp
;-------------------------

A0:
Gui,1:submit,nohide
Guicontrol,1:,ED3,running...
cpx:="cp65001"
ed2a:="cmd /c " . cmdx
xxc:=cmdret(ED2a,,CPX)
sleep,400
e .="`r`n=========================== START ===================================`r`n" . cmdx . " =`r`n-------------------------------`r`n" . xxc . "`r`n================================ ENDXX ================================`r`n"
sleep,100
Guicontrol,1:,ED1,%e%
Guicontrol,1:,ED3,ENDED
return

;--------------------------
printx:
Gui,1:submit,nohide
File1 =%a_now%_Youtube-TEST_cmdret.txt
if e<>
 {
 e:=Trim(RegExReplace(e, "`am)^(?!.*[[:alnum:]]).*\R+"), "`r`n")
 fileappend,%e%,%File1%,UTF-8
 try
  run,%File1%
 e:=""
 }
return
;==========================
;================= function from user teadrinker ========================================
;- https://www.autohotkey.com/boards/viewtopic.php?p=369467#p369467
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
   static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
        , STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
   hPipeRead:=""
   hPipeWrite:=""
   sOutput:=""
   DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
   DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
   
   VarSetCapacity(STARTUPINFO , siSize :=    A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
   NumPut(siSize              , STARTUPINFO)
   NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
   NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
   NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
   
   VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
   if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
                              , "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
   {
      DllCall("CloseHandle", "Ptr", hPipeRead)
      DllCall("CloseHandle", "Ptr", hPipeWrite)
      throw Exception("CreateProcess is failed")
   }
   DllCall("CloseHandle", "Ptr", hPipeWrite)
   VarSetCapacity(sTemp, 4096), nSize := 0
   while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
      sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
      ;sOutput .= stdOut := StrGet(&sTemp, nSize)
      ;sOutput .= stdOut := StrGet(&sTemp, nSize, CPX)
      ( callBackFuncObj && callBackFuncObj.Call(stdOut) )
   }
   DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
   DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
   DllCall("CloseHandle", "Ptr", hPipeRead)
   Return sOutput
}
;================= END SCRIPT ========================================================================

/*
 Verbosity / Simulation Options:
    -q, --quiet                          Activate quiet mode
    --no-warnings                        Ignore warnings
    -s, --simulate                       Do not download the video and do not
                                         write anything to disk
    --skip-download                      Do not download the video
    -g, --get-url                        Simulate, quiet but print URL
    -e, --get-title                      Simulate, quiet but print title
    --get-id                             Simulate, quiet but print id
    --get-thumbnail                      Simulate, quiet but print thumbnail URL
    --get-description                    Simulate, quiet but print video
                                         description
    --get-duration                       Simulate, quiet but print video length
    --get-filename                       Simulate, quiet but print output
                                         filename
    --get-format                         Simulate, quiet but print output format
    -j, --dump-json                      Simulate, quiet but print JSON
                                         information. See the "OUTPUT TEMPLATE"
                                         for a description of available keys.
    -J, --dump-single-json               Simulate, quiet but print JSON
                                         information for each command-line
                                         argument. If the URL refers to a
                                         playlist, dump the whole playlist
                                         information in a single line.
*/
;=======================================================================================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, mikeyww and 329 guests