GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
944 B

  1. package mirclike
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "git.aiterp.net/rpdata/api/models"
  7. )
  8. // ErrEmptyLog is returned by ParseLog if there are no (valid) posts in the log.
  9. var ErrEmptyLog = errors.New("No valid posts found in log")
  10. // ParseLog parses the log and returns the things that can be gleamed from them.
  11. func ParseLog(data string, date time.Time, strict bool) (models.Log, []models.Post, error) {
  12. lines := strings.Split(data, "\n")
  13. posts := make([]models.Post, 0, len(lines))
  14. prev := models.Post{}
  15. for _, line := range lines {
  16. line = strings.Trim(line, "\r\t  ")
  17. if len(line) < 1 {
  18. continue
  19. }
  20. post, err := ParsePost(line, date, prev)
  21. if err != nil {
  22. if strict {
  23. return models.Log{}, nil, err
  24. }
  25. continue
  26. }
  27. posts = append(posts, post)
  28. prev = post
  29. }
  30. if len(posts) == 0 {
  31. return models.Log{}, nil, ErrEmptyLog
  32. }
  33. log := models.Log{
  34. Date: posts[0].Time,
  35. }
  36. return log, posts, nil
  37. }