Anime "Duration" issue

Issue #119 resolved
sugar created an issue

Hello, wanna report another bug.

Some anime title causes a Notice: Undefined offset: 0 in \src\Atarashii\APIBundle\Parser\AnimeParser.php

Occurred when the anime duration is only hours. i.e; https://myanimelist.net/anime/30346/Doukyuusei_Movie (1 hr.) https://myanimelist.net/anime/18617/Girls_und_Panzer_der_Film (2 hr.)

Anime with duration in hr. min. (i.e. 2 hr. 10 min.) or min. (i.e. 45 min.) are not affected.

Thanks.

Note: i'm using develop branch & API 2.1

Comments (15)

  1. sugar reporter

    then, im trying to fix this issue myself.

    # Duration:
    $extracted = $leftcolumn->filterXPath('//span[text()="Duration:"]');
    if ($extracted->count() > 0) {
        $duration = trim(str_replace($extracted->text(), '', $extracted->parents()->text()));
        # match: # hr. # min
        if (strpos($duration, 'hr.') !== false && strpos($duration, 'min.') !== false) {
            preg_match('/(\d+) hr. (\d+) min/', $duration, $matches);
            $animerecord->setDuration((int) $matches[0] * 60 + $matches[2]);
        } 
        # match: # hr.
        else if (strpos($duration, 'hr.') !== false) {
            preg_match('/(\d+) hr./', $duration, $matches);
            $animerecord->setDuration((int) $matches[0] * 60);
        } 
        # match: # min
        else if (strpos($duration, 'min') !== false) {
            preg_match('/(\d+) min/', $duration, $matches);
            $animerecord->setDuration((int) $matches[0]);
        } 
        # fallback (i.e. unknown)
        else {
            $animerecord->setDuration(0);
        }
    }
    

    using !== false due strpos() has mixed return values and return false (boolean) if the needle was not found.

  2. sugar reporter

    yes.

    • 1st, i change the comparasion operator to not identical to prevent strpos() return value issue (0 treated as false, 1 as true)
    • 2nd, adding strpos($duration, 'min') to match duration in min
    • 3rd, adding fallback by using else and set the duration with 0 (zero, integer)
  3. Michael Johnson

    If we don't know the length, we shouldn't be returning a value. Returning 0 means that the length is 0, not "unknown" or something we couldn't parse.

    I'm going to be committing a better fix that will handle hr, hr+min, and min situations.

  4. Michael Johnson

    Please add the seconds request as a new issue. The current API defines duration as an integer in minutes, so the API would need to be changed, which means an incompatible (breaking) change. For now, we won't return a value if the duration is under one minute.

    You are welcome to modify the API for your needs. That's why the source is open. We won't normally accept a change where we return data when there isn't an existing value, however. The only exception is when we're already doing that because of legacy behavior.

  5. Michael Johnson

    Fix Duration Parsing

    Adjust the duration parser to handle all major styles including hour/minute, hour, and minute durations. Don’t break on “unknown” and unrecognized formats.

    Fixes #119

    → <<cset c45ee61c1676>>

  6. Michael Johnson

    Fix Duration Parsing

    Adjust the duration parser to handle all major styles including hour/minute, hour, and minute durations. Don’t break on “unknown” and unrecognized formats.

    Fixes #119

    → <<cset 13aed2576400>>

  7. Log in to comment