Jump to content

API Youtube Extracting Messages


Go to solution Solved by Danp2,

Recommended Posts

#include <MsgBoxConstants.au3>
$video_id = '7HkUFRFnTJw';    Parliament - Night of The Thumpasorus Peoples
$apikey =  'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ;  // browser key for desktop apps.
;$apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' ;  // server key for web apps.
$ytquery = 'https://www.youtube.com/watch' & '?id=' & $video_id & '&key=' & $apikey ;
$ytquery = $ytquery & '&part=snippet';
$ytquery = $ytquery & '&fields=displayMessage';
$ytdata = InetRead($ytquery,1);
$ytjson = BinaryToString($ytdata);
;MsgBox($MB_OK, $video_id, $ytjson)
ConsoleWrite( @CRLF &  $ytjson & @CRLF)
Exit

I wanted to write into console the message from my livechat.
When I provide the APIKey and Run the script, it give me  
<!DOCTYPE html><html style="font-size:......... and I hardly see the message chat because its too much output.
 

Link to comment
Share on other sites

Can I get A link that has a Valid API call. I couldnt find it.

image.thumb.png.5c4e702328d933ee526315f9107ad4dc.png

I have This API.
And I get my API Key Here

as1.thumb.png.a4fd1e63f7924d6043a5ddc583522867.png

 

$ytquery = 'https://www.youtube.com/watch' & '?id=' & $video_id & '&key=' & $apikey ;
$ytquery = $ytquery & '&part=snippet';
$ytquery = $ytquery & '&fields=displayMessage';]

Isnt This command accessing the fields of named displayMessage?
I wanted to get the content inside DisplayMessage.

Edited by senatin
Link to comment
Share on other sites

{
  "kind": "youtube#liveChatMessageListResponse",
  "etag": "B3IwHnXE3CBJTe4eCB7LNGYRA1Q",
  "pollingIntervalMillis": 1031,
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "nextPageToken": "GKzm9OCms_ICIPXGq-Oms_IC",
  "items": [
    {
      "kind": "youtube#liveChatMessage",
      "etag": "RnyZlS3ovUKQONFy0zNjOUodbKE",
      "id": "LCC.CikqJwoYVUNsbWVNcm1CUEQ2dC0wbDhnOG5CTGV3Egt2WU11aTd6OVRhdxI5ChpDUHZsOU9DbXNfSUNGZTRrclFZZE9MSUpiQRIbQ0xuMjVlaUlzX0lDRlZraEtnb2RsUW9MWlEx",
      "snippet": {
        "type": "textMessageEvent",
        "liveChatId": "KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILdllNdWk3ejlUYXc",
        "authorChannelId": "UClmeMrmBPD6t-0l8g8nBLew",
        "publishedAt": "2021-08-15T15:05:59.552812+00:00",
        "hasDisplayContent": true,
        "displayMessage": "test",
        "textMessageDetails": {
          "messageText": "test"
        }
      }
    }
  ]
}

This is from Live streaming API. I Think This is what Im trying to pull out.

And Here is what I Did follow in some other tutorial on making Client ID for web application including the API Key.

image.thumb.png.c67917462ca592c4b2f509f20532ba4d.png

I Did Try to search for API call And I bump to other programming language and I could not find one with autoit.

Is this the Right one using API CALL?

 

let getYoutubeVideoData = async (youtubeVideoId) => {
  const response = await request
    .get('https://www.googleapis.com/youtube/v3/videos')
    .query({id: youtubeVideoId})
    .query({key: process.env.YOUTUBE_API_KEY || "change-me-with-a-valid-youtube-key-if-you-need-me"}) //used only when saving youtube videos
    .query({part: 'snippet,contentDetails,statistics,status'});

  let title = response.body.items[0].snippet.title;
  const tags = response.body.items[0].snippet.tags;
  const description = response.body.items[0].snippet.description;
  const publishedAt = response.body.items[0].snippet.publishedAt;
  const publishedOn = publishedAt.substring(0, publishedAt.indexOf('T'));
  const videoDuration = formatDuration(response.body.items[0].contentDetails.duration);
  if (title.endsWith('- YouTube')) {
    title = title.replace('- YouTube', ' - ' + videoDuration);
  } else {
    title = title + ' - ' + videoDuration;
  }

  let webpageData = {
    title: title,
    metaDescription: description.substring(0, 500),
    publishedOn: publishedOn,
    videoDuration: videoDuration
  }

  if(tags) { //some youtube videos might not have tags defined
    webpageData.tags = tags.slice(0,8).map(tag => tag.trim().replace(/\s+/g, '-'));
  }

  return webpageData;
}

And if so Then I must start searching for the request type in autoit.

Edited by senatin
Link to comment
Share on other sites

#include "YTAPI.au3"
$linkadd = 'https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILdllNdWk3ejlUYXc&part=snippet&pageToken=GNyJvcjrs_ICIJWP_e7rs_IC'
$apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxx'

$Video_ID = _YTApi_GetVideoID($linkadd & "&key=" & $apikey)
If @error Then
    MsgBox(0, "VideoID", "The Video Failed:")
EndIf

$Video_Thumbnail = _YTApi_GetThumbnail($Video_ID)
If Not @error Then
    MsgBox(0, "Thumbnail", "You can see the video thumbnail at this address:" & @CRLF & $Video_Thumbnail)
EndIf

$Video_Author = _YTApi_GetAuthor($Video_ID)
If Not @error Then
    MsgBox(0, "Author", "The video author is: " & $Video_Author)
EndIf

I Got it Working With the right Link and API Key Thank you.

After I check YTAPI.au3 script

Func _YTApi_GetAuthor($s_ID)
    Local $sSource = __YTApi_GetSource($s_ID)
    If @error Then Return SetError(1, 0, "")

    Local $aRegex = StringRegExp($sSource, "<author><name>(.*?)</name>", 3)
    If @error Then Return SetError(1, 0, "")

    Return $aRegex[0]
EndFunc   ;==>_YTApi_GetAuthor

I don't know how to change the StringRegExp($sSource, "<author><name>(.*?)</name>", 3)
because The Output of the link provided by the API is Like this.
 

{
  "kind": "youtube#liveChatMessageListResponse",
  "etag": "EnTTv_ybu6Pc5_CF1KDOuasvP6w",
  "pollingIntervalMillis": 5102,
  "pageInfo": {
    "totalResults": 1,
    "resultsPerPage": 1
  },
  "nextPageToken": "GMvi_pjvs_ICIM3P8Zzvs_IC",
  "items": [
    {
      "kind": "youtube#liveChatMessage",
      "etag": "RCNOoo635-AvgA44NXkk0l_RlIY",
      "id": "LCC.CikqJwoYVUNsbWVNcm1CUEQ2dC0wbDhnOG5CTGV3Egt2WU11aTd6OVRhdxI5ChpDSnZpX3BqdnNfSUNGVUlHclFZZGwxZ0RiURIbQ0xuMjVlaUlzX0lDRlZraEtnb2RsUW9MWlEz",
      "snippet": {
        "type": "textMessageEvent",
        "liveChatId": "KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILdllNdWk3ejlUYXc",
        "authorChannelId": "UClmeMrmBPD6t-0l8g8nBLew",
        "publishedAt": "2021-08-15T20:30:04.509515+00:00",
        "hasDisplayContent": true,
        "displayMessage": "new item here",
        "textMessageDetails": {
          "messageText": "new item here"
        }
      }
    }
  ]
}

And I wanted to access nextPageToken and also the authorchannelID along with displayMessage.

Link to comment
Share on other sites

#include "Json.au3"

HotKeySet( "{ESC}", GoOut)

#Region API
$video_id = 'https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILRXFqdURWaFp4cnc&part=snippet'
$apikey = 'xxxxxxxxxxx'
#EndRegion
$Json = $video_id & "&key=" & $apikey
$Json = BinaryToString(InetRead($Json), 4)
$Obj = Json_Decode($Json)

While 1
    $totalresults = Json_Get($Obj, '["pageInfo"]["totalResults"]')
    If @error Then ErrorSwitch(@error)

    If $totalresults = 0 Then
        Sleep(500)
        ContinueLoop
    EndIf

    For $i = 0 to $totalresults - 1  Step + 1
        $displaymessage = Json_Get($Obj, '["items"][' & $i & ']["snippet"]["displayMessage"]')
        If @error Then ErrorSwitch(@error)
        ConsoleWrite($displaymessage & @LF)

    Next

    $nextpagetoken = Json_Get($Obj, '["nextPageToken"]')

    SetNewObj($nextpagetoken)

WEnd

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    ConsoleWrite($Json & @LF)
    $Json = BinaryToString(InetRead($Json), 4)
    ConsoleWrite($Json & @LF)
    $Obj = Json_Decode($Json)

EndFunc

Func ErrorSwitch($error)
    Switch $error
        Case 1
            ConsoleWrite("Error 1: key not exists" & @LF)
        Case 2
            ConsoleWrite("Error 2: syntax error" & @LF)
    EndSwitch
    Exit
EndFunc

Func GoOut()
    Exit
EndFunc

This Json.au3 works really well.
But I Bumb to an error That make me confuse.
 

$nextpagetoken = Json_Get($Obj, '["nextPageToken"]')
    ConsoleWrite(VarGetType($nextpagetoken) & " : " & $nextpagetoken & @LF)
    SetNewObj($nextpagetoken)

WEnd

$nextpagetoken has a String Value
And adding it in the right place to the Link Address
 

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    ConsoleWrite($Json & @LF)
    $Json = BinaryToString(InetRead($Json), 4)
    $Obj = Json_Decode($Json)

EndFunc

It shows The right Link as shown bellow the console. But Somehow It Doesn't Return Anything after Its been BinaryToString.
I Tried To Copy the link and manually Open and Its working perfectly. If its the $Json = $vieo_id & "&pageToken=" & $token made it error then is there other way of combining 2 strings?

Quote

String : GJ3GgIeatvICIMH6jb2itvIC
https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILRXFqdURWaFp4cnc&part=snippet&pageToken=GJ3GgIeatvICIMH6jb2itvIC&key=AIz....................yM
Error 1: key not exists
+>03:24:44 AutoIt3.exe ended.rc:0
+>03:24:44 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 2.487

 

Edited by senatin
Link to comment
Share on other sites

{
  "kind": "youtube#liveChatMessageListResponse",
  "etag": "WB7JTJxr7gcgNItxGX_-jIhfG3E",
  "pollingIntervalMillis": 4770,
  "pageInfo": {
    "totalResults": 0,
    "resultsPerPage": 0
  },
  "nextPageToken": "GJ3GgIeatvICIPKr8_SctvIC",
  "items": []
}

This is The Content With The New $Json.

If I Try to do this

 

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    $Json = BinaryToString(InetRead($Json), 4)
    ConsoleWrite($Json & @LF)
    $Obj = Json_Decode($Json)

EndFunc

CosoleWrite Return Nothing. Its Totally Empty.

Edited by senatin
Link to comment
Share on other sites

I ReMade the Post Because I notice The Error Is found Here.

 

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey ;"----- This Link is correct-----"
    ConsoleWrite($Json & @LF)
    $Json = BinaryToString(InetRead($Json), 4) ;"--------Here it return empty, unable to convert the Address Link------- "
    ConsoleWrite($Json & @LF)    ;"-----Return Empty---------"
    $Obj = Json_Decode($Json)    ;"------Give error Becuase Json is empty -----------"

EndFunc

This Script Return Console Like this

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
test on me
ano yarn
hindi pa kasi puno yung odins mo nyan
nagrerecover pa dun sa video
yung s video mo kasi, hindi pa puno yung odins
kapag hindi pa puno yung odins, saka lang nadadagdagan yung odins every 12 at 8
100 na din weapon ko sa wakas
accessories naman
parang medyo mas madali sa accessories kasi level 25 lang
bakit may mga scipt kana?
test float
kala ko script para sa ROX hahahah!
test Baphomet
String : GNDCqtKXuPICIJaHpfSZuPIC
https://youtube.googleapis.com/youtube/v3/liveChat/messages?liveChatId=KicKGFVDbG1lTXJtQlBENnQtMGw4ZzhuQkxldxILdzFTbGVrSUZmOWc&part=snippet&pageToken=GNDCqtKXuPICIJaHpfSZuPIC&key=**********

Error
Error 1: key not exists
+>21:51:42 AutoIt3.exe ended.rc:0
+>21:51:42 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 2.902

First before Getting the $nextpageToken The script along with the link is working fine. But after it include the TOken into the Link address The 
$Json = BinaryToString(InetRead($Json), 4)Return me Empty.

as we can see to console it live a blank Tab.

Thats Why When it call to Json_Decode The Scripts Give Error Saying that the $Index inside Json is Meant to be an Object.

I Dont Understand Why The Link address When Provided to the  BinaryToString become empty. I see That The variable with the link address is Arranged correctly.

I Also Tried to access the $Json after using InetRead

 

If InetRead($Json) = "" Then
        ConsoleWrite("Error Me"& @lf)
    Else
        ConsoleWrite("Im Zero" & @lf)
    EndIf

And It Return me WIth Empty = "Error Me"

Edited by senatin
Link to comment
Share on other sites

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    ConsoleWrite($Json & @LF)
    $inetread = InetRead($Json)
    If @error Then ConsoleWrite(@error & @LF)
    ConsoleWrite($inetread & @LF)
    $Json = BinaryToString(InetRead($Json), 4)
    ConsoleWrite($Json & @LF)
    $Obj = Json_Decode($Json)

EndFunc

and the error for the inetread is 13
I looked from other forum and I Find it in inetget?
 

Does the Link address with Token Need a credentials?

Do I need To have like this scripts to access with credentials the Link?

Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET","http://" & $sDomain & ":2086/xml-api/loadavg",False)
$oHTTP.SetRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5")
$oHTTP.SetCredentials($sUser,$sPass,0)
$oHTTP.Send("")
$sXML = $oHTTP.ResponseText()


Answer:
I finally Found A way how to Make it work
here it is.
 

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    ConsoleWrite($Json & @LF)

    Sleep(5000) ;"--------It Looks Like Its Loading ------------"

    $inetread = InetRead($Json)
    If @error Then ConsoleWrite(@error & @LF)
    ConsoleWrite($inetread & @LF)
    $Json = BinaryToString(InetRead($Json), 4)
    ConsoleWrite($Json & @LF)
    $Obj = Json_Decode($Json)

EndFunc

Now I'm going to Find A way to Know If the Page Is Ready To use.

Edited by senatin
Found Solution
Link to comment
Share on other sites

I don't have any idea why that occur. First I added the Http Credentials, and I notice something odd if I Delayed accepting the messagebox Bug DOnt appear. So I ended Up Putting sleep and Voila Its now Working Fine.
 

Func SetNewObj($token)
    $Json = $video_id & "&pageToken=" & $token & "&key=" & $apikey
    ConsoleWrite($Json & @LF)

;~  #Region Credential
;~  Local $REMOTE_URL = "http://localhost:***"
;~  Local $REMOTE_USER = "*****", $REMOTE_PASS = "r******TW0Pc"
;~  #EndRegion
;~  Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
;~  $oHTTP.Open("GET", $Json, False)
;~  $oHTTP.SetRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5")
;~  $oHTTP.SetCredentials($REMOTE_USER,$REMOTE_PASS,0)
;~  $oHTTP.Send("")
;~  $sXML = $oHTTP.ResponseText()
;~  $oStatusCode = $oHTTP.Status

    GetObjLink($Json)



EndFunc

Func GetObjLink($link)

    While 1
        Sleep(100)
        InetRead($link)
        If Not @error Then ExitLoop
    WEnd

    $Json = BinaryToString(InetRead($link), 4)
    ;ConsoleWrite($Json & @LF)
    $Obj = Json_Decode($Json)
EndFunc

 

Edited by senatin
Forgot To *** username and pass
Link to comment
Share on other sites

  • 2 weeks later...

@Earthshineaw Im sorry for long time reply.
I never got notified theres new reply on my Topic. After Reviewing my Topic I now see you.
Anyway Yes I did Loop it using while.

On 8/18/2021 at 1:05 AM, Danp2 said:

I don't see why that would be required unless they have a limit on the number of transactions they will process per user.

And Yes, It Does have Limit for a day for minute and so on. And Now Im Blending together Imagesearch to prevent exceeding limit.
But Now Im Wondering if this will be the same case to Facebook Livestreaming API. But I'll leave it for now, Im still making my way to Completion on my own project. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...