endkb / SoundsDownloadScript

Download programs from BBC Sounds and publish them to a podcast feed
2 stars 0 forks source link

ConvertFrom-Json errors out on this page: https://www.bbc.co.uk/sounds/play/m001z3hv #5

Closed endkb closed 6 months ago

endkb commented 6 months ago

Errors from the log:

PS>TerminatingError(ConvertFrom-Json): "Conversion from JSON failed with error: After parsing a value an unexpected character was encountered: F. Path 'modules.data[0].data[0].synopses.long', line 1, position 1314."

ConvertFrom-Json: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:271
Line |
 271 |      $jsonData = $jsonResult | ConvertFrom-Json
     |                                ~~~~~~~~~~~~~~~~
     | Conversion from JSON failed with error: After parsing a value an unexpected character was encountered: F. Path 'modules.data[0].data[0].synopses.long', line 1, position 1314.

ConvertFrom-Json: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:271
Line |
 271 |      $jsonData = $jsonResult | ConvertFrom-Json
     |                                ~~~~~~~~~~~~~~~~
     | Conversion from JSON failed with error: After parsing a value an unexpected character was encountered:
     | F. Path 'modules.data[0].data[0].synopses.long', line 1, position 1314.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:274
Line |
 274 |      $TitleTable = $($jsonData.modules.data[0].data.titles)
     |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:274
Line |
 274 |      $TitleTable = $($jsonData.modules.data[0].data.titles)
     |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:284
Line |
 284 |      $SynopsesTable = $($jsonData.modules.data[0].data.synopses)
     |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:284
Line |
 284 |      $SynopsesTable = $($jsonData.modules.data[0].data.synopses)
     |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:298
Line |
 298 |      $Station = $($jsonData.modules.data[0].data.network.short_title)
     |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:298
Line |
 298 |      $Station = $($jsonData.modules.data[0].data.network.short_title)
     |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:301
Line |
 301 |  … eDate = [datetime]$($jsonData.modules.data[0].data.availability.from)
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:301
Line |
 301 |  … eDate = [datetime]$($jsonData.modules.data[0].data.availability.from)
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:318
Line |
 318 |      $CoverResult = $($jsonData.modules.data[0].data.image_url).replac …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:318
Line |
 318 |      $CoverResult = $($jsonData.modules.data[0].data.image_url).replac …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot index into a null array.


InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:318
Line |
 318 |      $CoverResult = $($jsonData.modules.data[0].data.image_url).replac …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.

InvalidOperation: C:\Program Files (x86)\VideoLAN\VLC\SoundsDownloadScript.ps1:318
Line |
 318 |      $CoverResult = $($jsonData.modules.data[0].data.image_url).replac …
     |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | You cannot call a method on a null-valued expression.


None of the metadata gets set correctly, of course.

endkb commented 6 months ago

This routine to replace fancy quotes is what is causing it to choke.

$smartSingleQuotes = '[\u2019\u2018]'
$smartDoubleQuotes = '[\u201C\u201D]'
$SoundsShowPage = $SoundsShowPage -replace $smartSingleQuotes,"'" -replace $smartDoubleQuotes,'"'

I hate fancy quotes because some programs don't display them properly, so I'm going to find a way to fix this code instead of just getting rid of it.

Edit to add: If you're a developer, STOP USING FANCY QUOTES!

endkb commented 6 months ago

Double quotes were not being escaped so the JSON parser was choking.

$jsonResult = "$jsonResult" -replace '[\u201C\u201D\u201E\u201F\u2033\u2036]', "$([char]92)$([char]34)" -replace "[\u2018\u2019\u201A\u201B\u2032\u2035]", "$([char]39)"

Adding the above code to the script with escapes the double quote with a backslash. It also catches more fancy quotes than the old routine.