Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jakopako committed Jun 17, 2024
1 parent ef22982 commit 6e05b29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
15 changes: 6 additions & 9 deletions scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ func (c Scraper) GetItems(globalConfig *GlobalConfig, rawDyn bool) ([]map[string
}
}

c.GuessYear(items)
c.guessYear(items, time.Now())

return items, nil
}

func (c *Scraper) GuessYear(items []map[string]interface{}) {
func (c *Scraper) guessYear(items []map[string]interface{}, ref time.Time) {
// get date field names where we need to adapt the year
dateFieldsGuessYear := map[string]bool{}
for _, f := range c.Fields {
Expand Down Expand Up @@ -405,17 +405,14 @@ func (c *Scraper) GuessYear(items []map[string]interface{}) {
// for the remaining items we do the same as with the first item except
// that we compare this item's date to the previous item's date instead
// of 'now'.
var prev time.Time
if i == 0 {
prev = time.Now()
} else {
prev, _ = items[i-1][name].(time.Time)
if i > 0 {
ref, _ = items[i-1][name].(time.Time)
}
diff := time.Since(time.Unix(0, 0))
newDate := t
for y := prev.Year() - 1; y <= prev.Year()+1; y++ {
for y := ref.Year() - 1; y <= ref.Year()+1; y++ {
tmpT := time.Date(y, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
if newDiff := tmpT.Sub(prev).Abs(); newDiff < diff {
if newDiff := tmpT.Sub(ref).Abs(); newDiff < diff {
diff = newDiff
newDate = time.Date(y, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
Expand Down
13 changes: 13 additions & 0 deletions scraper/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,16 @@ func TestExtractFieldDate29Feb(t *testing.T) {
t.Fatalf("expected '2024' as year of date but got '%d'", dt.Year())
}
}

func TestGuessYearSimple(t *testing.T) {
// events span period around change of year

}

func TestGuessYearUnordered(t *testing.T) {
// events are not perfectly ordered
}

func TestGuessYear2Years(t *testing.T) {
// events span more than 2 years
}

0 comments on commit 6e05b29

Please sign in to comment.