omansak / libvideo

A lightweight .NET library to download YouTube videos.
BSD 2-Clause "Simplified" License
538 stars 163 forks source link

Newtonsoft.Json.JsonReaderException: Unterminated string. Expected delimiter: " #239

Closed sepp89117 closed 2 years ago

sepp89117 commented 2 years ago

Newtonsoft.Json.JsonReaderException: Unterminated string. Expected delimiter: ". Path 'videoDetails.shortDescription', line 1, position 56218. YouTube.cs:Line 72 try to downloading URL https://www.youtube.com/watch?v=U2XK_TJZ3PI

sepp89117 commented 2 years ago

Solved!

The shortDescription of this Video contains two '}'. The Json extraction function misinterprets the character because it does not recognize that it is within a quoted string.

Fixed function: 'Helpers/Json.cs' -> 'public static string Extract(string source)'

public static string Extract(string source)
{
    StringBuilder sb = new StringBuilder();
    int depth = 0;
    char lastChar = '\u0000';
    bool isString = false;
    foreach (var ch in source)
    {
        sb.Append(ch);
        //First, set bool if we are in a quoted String
        if(ch == '"' && lastChar != '\\')
        {
            isString = !isString;
        }

        //Don't change depth if we are in String
        if (!isString)
        {
            if (ch == '{' && lastChar != '\\')
                depth++;
            else if (ch == '}' && lastChar != '\\')
                depth--;
        }

        if (depth == 0)
            break;
        lastChar = ch;
    }
    return sb.ToString();
}
omansak commented 2 years ago

@sepp89117 can you do pull request ?

omansak commented 2 years ago

any update ?

sepp89117 commented 2 years ago

I'm really sorry! I hardly find time for the hobby... You can simply copy the code if you want.

sepp89117 commented 2 years ago

I opened a pull request

omansak commented 2 years ago

fixed in 3.2.1