I didn't know
%%~nxA would also work for urls. Nice!
I've further worked out my script:
Code: Select all
@ECHO off
CLS
:: NPO JSON-extractor geschreven door Reino Wijnsma, 2015 (reino@degeelebosch.nl)
SET batchname=NPO JSON-extractor
SET version=1.1
TITLE %batchname% %version%
SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"
:Check
IF EXIST %curl% (
IF EXIST %jq% (
GOTO Input
) ELSE (
ECHO 'jq.exe' niet gevonden.
ECHO.
PAUSE
GOTO :eof
)
GOTO Input
) ELSE (
ECHO 'curl.exe' niet gevonden.
ECHO.
PAUSE
GOTO :eof
)
:Input
ECHO Voer npo.nl programmalink in :
SET url=
SET /P url=
:: http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 bijvoorbeeld
IF "%url%"=="" GOTO :eof
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('curl.exe -s http://e.omroep.nl/metadata/aflevering/%%~nxA ^| jq.exe -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"') DO (
ECHO.
ECHO JSON:
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% .') DO ECHO %%C
ECHO.
FOR /F "tokens=1-3" %%C IN ('ECHO %%B ^| %jq% -r "[.tijdsduur,.start,.eind] | \"\(.[0]^) \(.[1]^) \(.[2]^)\""') DO (
ECHO Tijdsduur: %%C
IF NOT "%%D"=="null" (
SET ss=%%D
SET /A "_ss=((1!ss:~0,2!-100)*3600)+((1!ss:~3,2!-100)*60)+(1!ss:~6,2!-100)"
ECHO Start: %%D (!_ss!s^)
)
IF NOT "%%E"=="null" (
SET to=%%E
SET /A "_to=((1!to:~0,2!-100)*3600)+((1!to:~3,2!-100)*60)+(1!to:~6,2!-100)"
ECHO Einde: %%E (!_to!s^)
)
)
)
)
ECHO.
ENDLOCAL
GOTO Input
Okay, from the beginning. Npo.nl ("Dutch Public Broadcast") is a website where you, or at least Dutch citizens, can freely watch television broadcasts.
http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766 is examplary for a typical program-url for a full-length tvshow.
http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040 on the other hand is a program-url for a fragment of the same tvshow.
Each has a different JSON and I'm trying to obtain certain metadata from within these JSONs.
Let's process http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040. The url for the JSON is http://e.omroep.nl/metadata/aflevering/POMS_VPRO_850040. By simply opening this url in your browser you'd get...
Code: Select all
parseMetadata({"STATUS":"OK","VERSION":"1.11.12","prid":"VPWON_1232766","titel":"Schuim & As","aflev
ering_titel":"","info":"Schuim & As met Jelle Brandt Corstius","ratio":"16:9","medium":"tv","gidsdat
um":"2015-05-03","tijdsduur":"00:05:27","start":"00:23:13","eind":"00:28:40","url":"","webcast":1,"i
mages":[{"size":"640x480","ratio":"4:3","url":"http:\/\/images.poms.omroep.nl\/image\/sx480\/c640x48
0\/606030.jpg"},{"size":"720x405","ratio":"16:9","url":"http:\/\/images.poms.omroep.nl\/image\/sx405
\/c720x405\/606030.jpg"}],"omroepen":[{"naam":"VPRO"}],"pubopties":["adaptive","h264_bb","h264_sb","
h264_std"],"tt888":"ja","serie":{"srid":"VPWON_1232748","serie_titel":"Buitenhof","serie_url":null},
"sitestat":{"baseurl":"http:\/\/b.scorecardresearch.com\/p?c1=2&c2=17827132&ns_site=po-totaal","prog
ramurl":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","programurlpost":"category=
uitzendinggemist&thema=informatief&po_source=video","baseurl_subtitle":"http:\/\/nl.sitestat
.com\/klo\/po\/s","subtitleurl":"uitzendinggemist.publiekeomroep.ondemand.tv.player.tt888.buitenhof"
,"subtitleurlpost":"category=uitzendinggemist&po_source=video&po_sitetype=webonly"},"reclame
":"http:\/\/pubads.g.doubleclick.net\/gampad\/ads?_cookie_&impl=s&gdfp_req=1&env=vp&output=xml_vast2
&unviewed_position_start=1&sz=_sz_&correlator=_correlator_&iu=\/9233\/_site_\/buitenhof&url=_url_&cu
st_params=genre%3Dinformatief%2Cnieuws%2Factualiteiten%26dur%3D3284%26prid%3DVPWON_1232766%26srid%3D
VPWON_1232748%26player%3D_player_","streamSense":{"episode":"buitenhof","program":"buitenhof","stati
on":"nederland_1","sitestatname":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","s
ko":"TRUE","sko_dt":"20150503","sko_pr":"buitenhof","sko_stid":"1","sko_ty":"tv.seg","sko_prid":"vpw
on1232766","sko_t":"1210","sko_cl":"3284"}})
//epc
...which is the same as
curl.exe's output in the first for-loop. Everything is on a single line, except "//epc". So to prevent the for-loop from starting over, this 2nd line has to go. This is where
jq (a command-line JSON processor) comes in. The jq-command after the pipe first of all puts everything on one single line and then removes
parseMetadata( and
) //epc so that we end up with:
Code: Select all
{"STATUS":"OK","VERSION":"1.11.12","prid":"VPWON_1232766","titel":"Schuim & As","aflevering_titel":"
","info":"Schuim & As met Jelle Brandt Corstius","ratio":"16:9","medium":"tv","gidsdatum":"2015-05-0
3","tijdsduur":"00:05:27","start":"00:23:13","eind":"00:28:40","url":"","webcast":1,"images":[{"size
":"640x480","ratio":"4:3","url":"http:\/\/images.poms.omroep.nl\/image\/sx480\/c640x480\/606030.jpg"
},{"size":"720x405","ratio":"16:9","url":"http:\/\/images.poms.omroep.nl\/image\/sx405\/c720x405\/60
6030.jpg"}],"omroepen":[{"naam":"VPRO"}],"pubopties":["adaptive","h264_bb","h264_sb","h264_std"],"tt
888":"ja","serie":{"srid":"VPWON_1232748","serie_titel":"Buitenhof","serie_url":null},"sitestat":{"b
aseurl":"http:\/\/b.scorecardresearch.com\/p?c1=2&c2=17827132&ns_site=po-totaal","programurl":"uitze
ndinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","programurlpost":"category=uitzendinggemi
st&thema=informatief&po_source=video","baseurl_subtitle":"http:\/\/nl.sitestat.com\/klo\/po\
/s","subtitleurl":"uitzendinggemist.publiekeomroep.ondemand.tv.player.tt888.buitenhof","subtitleurlp
ost":"category=uitzendinggemist&po_source=video&po_sitetype=webonly"},"reclame":"http:\/\/pu
bads.g.doubleclick.net\/gampad\/ads?_cookie_&impl=s&gdfp_req=1&env=vp&output=xml_vast2&unviewed_posi
tion_start=1&sz=_sz_&correlator=_correlator_&iu=\/9233\/_site_\/buitenhof&url=_url_&cust_params=genr
e%3Dinformatief%2Cnieuws%2Factualiteiten%26dur%3D3284%26prid%3DVPWON_1232766%26srid%3DVPWON_1232748%
26player%3D_player_","streamSense":{"episode":"buitenhof","program":"buitenhof","station":"nederland
_1","sitestatname":"uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503","sko":"TRUE","sk
o_dt":"20150503","sko_pr":"buitenhof","sko_stid":"1","sko_ty":"tv.seg","sko_prid":"vpwon1232766","sk
o_t":"1210","sko_cl":"3284"}}
In the next for-loop this line is fed to
jq.exe to make the JSON more readible:
Code: Select all
{
"STATUS": "OK",
"VERSION": "1.11.12",
"prid": "VPWON_1232766",
"titel": "Schuim & As",
"aflevering_titel": "",
"info": "Schuim & As met Jelle Brandt Corstius",
"ratio": "16:9",
"medium": "tv",
"gidsdatum": "2015-05-03",
"tijdsduur": "00:05:27",
"start": "00:23:13",
"eind": "00:28:40",
"url": "",
"webcast": 1,
"images": [
{
"size": "640x480",
"ratio": "4:3",
"url": "http://images.poms.omroep.nl/image/sx480/c640x480/606030.jpg"
},
{
"size": "720x405",
"ratio": "16:9",
"url": "http://images.poms.omroep.nl/image/sx405/c720x405/606030.jpg"
}
],
"omroepen": [
{
"naam": "VPRO"
}
],
"pubopties": [
"adaptive",
"h264_bb",
"h264_sb",
"h264_std"
],
"tt888": "ja",
"serie": {
"srid": "VPWON_1232748",
"serie_titel": "Buitenhof",
"serie_url": null
},
"sitestat": {
"baseurl": "http://b.scorecardresearch.com/p?c1=2&c2=17827132&ns_site=po-totaal",
"programurl": "uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503",
"programurlpost": "category=uitzendinggemist&thema=informatief&po_source=video",
"baseurl_subtitle": "http://nl.sitestat.com/klo/po/s",
"subtitleurl": "uitzendinggemist.publiekeomroep.ondemand.tv.player.tt888.buitenhof",
"subtitleurlpost": "category=uitzendinggemist&po_source=video&po_sitetype=webonly"
},
"reclame": "http://pubads.g.doubleclick.net/gampad/ads?_cookie_&impl=s&gdfp_req=1&env=vp&output=xm
l_vast2&unviewed_position_start=1&sz=_sz_&correlator=_correlator_&iu=/9233/_site_/buitenhof&url=_url
_&cust_params=genre%3Dinformatief%2Cnieuws%2Factualiteiten%26dur%3D3284%26prid%3DVPWON_1232766%26sri
d%3DVPWON_1232748%26player%3D_player_",
"streamSense": {
"episode": "buitenhof",
"program": "buitenhof",
"station": "nederland_1",
"sitestatname": "uitzendinggemist.publiekeomroep.ondemand.tv.buitenhof.20150503",
"sko": "TRUE",
"sko_dt": "20150503",
"sko_pr": "buitenhof",
"sko_stid": "1",
"sko_ty": "tv.seg",
"sko_prid": "vpwon1232766",
"sko_t": "1210",
"sko_cl": "3284"
}
}
In the next for-loop the same single-line input is again being fed to
jq.exe, but this time to retrieve the values for "tijdsduur", "start" and "eind". In this JSON all 3 objects are present, so the value for "tijdsduur" is echoed, and since the values for "start" and "eind" are not "null" these are echoed as well, with the time in seconds between brackets as a bonus:
Code: Select all
Tijdsduur: 00:05:27
Start: 00:23:13 (1393s)
Einde: 00:28:40 (1720s)
In the JSON of a full-length tvshow only "tijdsduur" is present, so only the value for "tijdsduur" is echoed.
With the path to
curl.exe and
jq.exe in variables this is working fine, except for the first for-loop where curl's output is piped to jq.