Error Parsing Episode Number from String "One Piece Film: Z - Filme" in Go

75 Views Asked by At

I am developing a Go application that interacts with an anime website to download and play episodes. The application should handle both regular series with numbered episodes and movies/OVAs that do not follow the same numbering format.

The issue arises when trying to extract and convert the episode number from strings representing movies/OVAs, such as "One Piece Film: Z - Filme". Since these titles do not contain episode numbers in the expected format, the strconv.Atoi function fails to convert an empty string, resulting in the error:


Error parsing episode number 'One Piece Film: Z - Filme': strconv.Atoi: parsing "": invalid syntax

The relevant code snippet is as follows:


if series {
        fmt.Printf("O anime selecionado é uma série com %d episódios.\n", totalEpisodes)
        selectedEpisodeURL, episodeNumberStr := selectEpisode(episodes)
    
        // Check if the episode title contains any digits
        var selectedEpisodeNum int
        if regexp.MustCompile(`\d`).MatchString(episodeNumberStr) {
            selectedEpisodeNumStr := extractEpisodeNumber(episodeNumberStr)
            var err error
            selectedEpisodeNum, err = strconv.Atoi(selectedEpisodeNumStr)
            if err != nil {
                log.Fatalf("Error parsing episode number: %v", err)
            }
        } else {
            // For movies/OVAs, set a default episode number (e.g., 1)
            selectedEpisodeNum = 1
        }
    
        videoURL, err := getVideoURLForEpisode(selectedEpisodeURL)
        if err != nil {
            log.Fatalf("Failed to extract video URL: %v", err)
        }
    
        handleDownloadAndPlay(videoURL, episodes, selectedEpisodeNum, animeURL, episodeNumberStr)
    } else {
        // Handle movies/OVAs similarly as before
        fmt.Println("O anime selecionado é um filme/OVA. Iniciando a reprodução direta...")
        videoURL, err := getVideoURLForEpisode(episodes[0].URL)
        if err != nil {
            log.Fatalf("Failed to extract video URL: %v", err)
        }
    
        handleDownloadAndPlay(videoURL, episodes, 1, animeURL, episodes[0].Number)
    }

The extractEpisodeNumber function attempts to extract a number from a string, returning "1" if no number is found:


func extractEpisodeNumber(episodeStr string) string {
    numRe := regexp.MustCompile(`\d+`)
    numStr := numRe.FindString(episodeStr)
    if numStr == "" {
        fmt.Println("No numeric episode number found, returning default value '1'")
        return "1" // Retorna "1" para filmes/OVAs
    }
    return numStr
}

How can I adjust my episode number extraction logic to properly handle strings for movies/OVAs, avoiding the conversion error with strconv.Atoi when the string does not contain numbers?

I am looking for a solution that allows me to reliably differentiate between series episodes and movies/OVAs and handle each case appropriately in my application.

How can I adjust my episode number extraction logic to properly handle strings for movies/OVAs, avoiding the conversion error with strconv.Atoi when the string does not contain numbers?

Minimal Reproducible Example

package main

import (
    "fmt"
    "log"
    "regexp"
    "strconv"
)

func extractEpisodeNumber(episodeStr string) string {
    numRe := regexp.MustCompile(`\d+`)
    numStr := numRe.FindString(episodeStr)
    if numStr == "" {
        return "1" // Return "1" for movies/OVAs
    }
    return numStr
}

func main() {
    episodeTitles := []string{"One Piece Episode 1", "One Piece Film: Z - Filme"}

    for _, title := range episodeTitles {
        episodeNumStr := extractEpisodeNumber(title)
        episodeNum, err := strconv.Atoi(episodeNumStr)
        if err != nil {
            log.Fatalf("Error parsing episode number from '%s': %v", title, err)
        }
        fmt.Printf("Parsed episode number for '%s': %d\n", title, episodeNum)
    }
}

Expected Behavior

The program should parse the episode number from the string and print it. For strings without a numeric episode number, it should default to 1.

Actual Behavior

The program crashes with the following error when processing a title without a numeric episode number:

Error parsing episode number from 'One Piece Film: Z - Filme': strconv.Atoi: parsing "": invalid syntax

Steps to Reproduce

  • Run the provided Go program.
  • Observe the error output when processing the second title in the episodeTitles slice.

full code

0

There are 0 best solutions below