Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jakopako/issue29 #44

Merged
merged 2 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,33 @@ crawlers:
exp: "[0-9]{2}(\\.|:)[0-9]{2}"
index: -1
location: "Europe/Berlin"

- name: Muffatwerk
type: concert
city: Munich
url: "https://www.muffatwerk.de/de/events/concert"
event: ".row .event"
fields:
title:
loc: ".hover-in .center span"
url:
loc: ".hover-in .right a"
relative: true
date:
day_month:
loc: ".hover-in .center"
layout: "02.01. "
regex_extract:
exp: "([0-9]{2}\\.){2}\\s"
year:
loc: ".date"
layout: "06 " # the whitespace in this string is a tab.
regex_extract:
exp: "[0-9]{2}\\s"
time:
loc: ".hover-in .center"
layout: "15:04"
regex_extract:
exp: "[0-9]{2}:[0-9]{2}"
location: "Europe/Berlin"

23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -212,7 +213,16 @@ func (c Crawler) ignoreEvent(event *Event) (bool, error) {
func extractField(item string, s *goquery.Selection, crawler *Crawler, event *Event, events []Event, loc *time.Location, mLocale string, res *http.Response) error {
switch item {
case "date":
year := time.Now().Year()
currentYear := time.Now().Year()
yearString := strconv.Itoa(currentYear)
yearLayout := "2006"

if crawler.Fields.Date.Year.Loc != "" {
yearStringTmp, yearLayoutTmp := getDateStringAndLayout(&crawler.Fields.Date.Year, s)
if yearStringTmp != "" {
yearString, yearLayout = yearStringTmp, yearLayoutTmp
}
}

var timeString, timeStringLayout string
if crawler.Fields.Date.Time.Loc == "" {
Expand Down Expand Up @@ -240,8 +250,8 @@ func extractField(item string, s *goquery.Selection, crawler *Crawler, event *Ev
dayMonthLayout = dayLayout + " " + monthLayout
}

dateTimeLayout = fmt.Sprintf("%s 2006 %s", dayMonthLayout, timeStringLayout)
dateTimeString = fmt.Sprintf("%s %d %s", dayMonthString, year, timeString)
dateTimeLayout = fmt.Sprintf("%s %s %s", dayMonthLayout, yearLayout, timeStringLayout)
dateTimeString = fmt.Sprintf("%s %s %s", dayMonthString, yearString, timeString)
}

if dateTimeString == "" {
Expand All @@ -255,8 +265,10 @@ func extractField(item string, s *goquery.Selection, crawler *Crawler, event *Ev
// actually this is only necessary if we have to guess the date but currently for ease of implementation
// this check is done always.
if len(events) > 0 {
if events[len(events)-1].Date.After(t) {
t = time.Date(int(year+1), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
correctYear := currentYear
for events[len(events)-1].Date.After(t) {
correctYear += 1
t = time.Date(int(correctYear), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}
}
event.Date = t
Expand Down Expand Up @@ -486,6 +498,7 @@ type Crawler struct {
Date struct {
Day DateField `yaml:"day"`
Month DateField `yaml:"month"`
Year DateField `yaml:"year"`
DayMonth DateField `yaml:"day_month"`
DayMonthYear DateField `yaml:"day_month_year"`
DayMonthYearTime DateField `yaml:"day_month_year_time"`
Expand Down