Getting videoId from Youtube GET request

Questions and answers on how to get the most out of FFAStrans
Post Reply
flix
Posts: 4
Joined: Sat Mar 23, 2019 2:53 pm

Getting videoId from Youtube GET request

Post by flix »

Hi, first of of all thank you for this great software, so powerful. I'm not very experienced in parsing JSON files, thus I would need some help. The problem is getting the videoId from Youtube JSON reply. The goal is the get the videoid into a user variable, which is needed for a text file.

Here is the JSON:

Code: Select all

{
 "kind": "youtube#playlistItemListResponse",
 "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/2gSn6UOOSh73oNWwLJlWeSZgNUc\"",
 "nextPageToken": "CAEQAA",
 "pageInfo": {
  "totalResults": 499,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#playlistItem",
   "etag": "\"XpPGQXPnxQJhLgs6enD_n8JR4Qk/6aBe6zCGCUhOnJxsjUmWU6CRWBQ\"",
   "id": "VVUxTVNqbHhQS2RYVURRWmJSU05zbDVRLlhwUy1vS3B2V0pR",
   "contentDetails": {
    "videoId": "XpS-oKpfgJQ",
    "videoPublishedAt": "2019-03-17T19:51:08.000Z"
   }
  }
 ]
}
flix
Posts: 4
Joined: Sat Mar 23, 2019 2:53 pm

Re: Getting videoId from Youtube GET request

Post by flix »

Figured out the problem myself, here is the principle I used how to easily get all the information output in to file. I used curl GET on youtube API and then piped the json reply to jq. After that I output the information jq siphoned to a text file. Then using a user defined variable and $read in Populate variables to get the information into Ffastrans. Here one of the key things was to use the full path of curl and jq, otherwise it won't execute.

Code: Select all

%comspec% /c "C:\Users\felix\Documents\curl\curl-7.64.0-win64-mingw\bin\curl.exe "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=your_upload_playlist_id&maxResults=1&key=your_apikey" | C:\Users\felix\Documents\jq\jq.exe --raw-output .items[0].contentDetails.videoId > "D:\delivery\%s_original_name%_output.txt""
emcodem
Posts: 1753
Joined: Wed Sep 19, 2018 8:11 am

Re: Getting videoId from Youtube GET request

Post by emcodem »

Hi flix,

sorry for the late answer, happy that you already found a nice workaround.
Instead of writing to a text file, you could just set the reply json to a user variable in the http node and in another node like e.g. populate variables, you could use something like that to parse your value of interest

Code: Select all

$between("%s_rest_response%",'videoId": "','"')
explained:
$between is a function from ffastrans for string parsing, %s_rest_response% contains the rest value from the http node, 'videoId": "' (take care about the whitespace between the : and " here) is the start sequence and '"' the end sequence that you want to parse from your rest response :-)
emcodem, wrapping since 2009 you got the rhyme?
flix
Posts: 4
Joined: Sat Mar 23, 2019 2:53 pm

Re: Getting videoId from Youtube GET request

Post by flix »

emcodem wrote: Mon Mar 25, 2019 2:01 pm Hi flix,

sorry for the late answer, happy that you already found a nice workaround.
Instead of writing to a text file, you could just set the reply json to a user variable in the http node and in another node like e.g. populate variables, you could use something like that to parse your value of interest

Code: Select all

$between("%s_rest_response%",'videoId": "','"')
explained:
$between is a function from ffastrans for string parsing, %s_rest_response% contains the rest value from the http node, 'videoId": "' (take care about the whitespace between the : and " here) is the start sequence and '"' the end sequence that you want to parse from your rest response :-)
Thank you for your reply. I tried understanding $between before going the workaround route. The mix of single and double quotes are quite intimidating for the uninitiated.
I was already familiar with XPath, so jq so was an easy route for me. It also was really easy to debug to get the correct response using online tools.
https://jqplay.org/

Is there an easy way to debug the output of the functions built-in to FFAstrans?
emcodem
Posts: 1753
Joined: Wed Sep 19, 2018 8:11 am

Re: Getting videoId from Youtube GET request

Post by emcodem »

flix wrote: Tue Mar 26, 2019 2:19 pm Is there an easy way to debug the output of the functions built-in to FFAstrans?
Not sure if it is easy, but i always use the "write text file" node in order to investigate the current value of variables, like here:
viewtopic.php?f=5&t=849#p3550
emcodem, wrapping since 2009 you got the rhyme?
Post Reply