package forumlog import ( "bufio" "fmt" "strings" "time" "git.aiterp.net/rpdata/api/internal/importers/mirclike" "git.aiterp.net/rpdata/api/models" ) // A ParsedLog contains the parsed log header and its posts. type ParsedLog struct { Log models.Log Posts []models.Post } // ParseLogs parses the logs from the data. func ParseLogs(data string, tz *time.Location) ([]ParsedLog, error) { metadata := ParseMetadata(data) results := make([]ParsedLog, 0, len(metadata["Date"])) scanner := bufio.NewScanner(strings.NewReader(data)) for i, dateStr := range metadata["Date"] { // Parse date date, err := time.ParseInLocation("January 2, 2006", dateStr, tz) if err != nil { return nil, fmt.Errorf("Failed to parse date #%d: %#+v is not the in the correct format of \"January 2, 2006\"", i+1, dateStr) } // Parse posts posts := make([]models.Post, 0, 128) parsing := false prev := "" prevPost := models.Post{} for scanner.Scan() { line := strings.Trim(scanner.Text(), "\r\t  ") // Skip lines with links to other logs using the --> and <-- notation. if strings.HasPrefix(line, "-") || strings.HasPrefix(line, "<") { prev = line continue } // If parsing and reaching a double empty-line, the session is done. if parsing && len(line) < 2 && len(prev) < 2 { break } // Skip empty lines, but record them as thep revious for the above check. if len(line) < 2 { prev = line continue } // If not parsing, skip until the first mirclike post. if !parsing { if strings.HasPrefix(line, "[") { parsing = true } else { prev = line continue } } // Parse the post. post, err := mirclike.ParsePost(line, date, prevPost) if err != nil { summary := "" for _, ru := range line { summary += string(ru) if len(summary) > 60 { summary += "..." break } } return nil, fmt.Errorf("Failed to parse post: %s", summary) } posts = append(posts, post) prevPost = post prev = line } // No posts means there's a problem. if len(posts) == 0 { return nil, fmt.Errorf("Session %d (%s) has no posts (too many dates?)", i+1, dateStr) } // Add it. results = append(results, ParsedLog{ Log: models.Log{Date: posts[0].Time}, Posts: posts, }) } return results, nil }