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

Make url attribute customizable #8

Merged
merged 1 commit into from
Jan 4, 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: 29 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,32 @@ crawlers:
loc: ".commingupEventsList_block4"
layout: "15Uhr04"
location: "Europe/Berlin"
language: "de_DE"
language: "de_DE"

- name: Mascotte
type: concert
city: Zurich
url: "https://www.mascotte.ch/nu/events/event_list_type/2"
event: ".nu-e-concert"
fields:
title:
loc: ".screen-only h2"
comment:
loc: ".nu-e-subt"
url:
loc: ".nu-e-link-share"
attr: "data-a2a-url"
date:
day_month_year:
loc: ".screen-only .nu-e-date"
layout: "2.1.06"
regex:
exp: "([0-9]{1,2}\\.){2}[0-9]{2}"
index: 0
time:
loc: ".nu-e-time-age"
layout: "15.04"
regex:
exp: "[0-9]{2}\\.[0-9]{2}"
index: 1
location: "Europe/Berlin"
18 changes: 13 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,14 @@ func extractField(item string, s *goquery.Selection, crawler *Crawler, event *Ev
event.Comment = getFieldString(&crawler.Fields.Comment, s)
case "url":
var url string
attr := "href"
if crawler.Fields.URL.Attr != "" {
attr = crawler.Fields.URL.Attr
}
if crawler.Fields.URL.Loc == "" {
url = s.AttrOr("href", crawler.URL)
url = s.AttrOr(attr, crawler.URL)
} else {
url = s.Find(crawler.Fields.URL.Loc).AttrOr("href", crawler.URL)
url = s.Find(crawler.Fields.URL.Loc).AttrOr(attr, crawler.URL)
}

if crawler.Fields.URL.Relative {
Expand Down Expand Up @@ -257,9 +261,12 @@ func getFieldString(f *Field, s *goquery.Selection) string {
var fieldString string
fieldSelection := s.Find(f.Loc)
if len(fieldSelection.Nodes) > 0 {
fieldString = fieldSelection.Get(f.NodeIndex).FirstChild.Data
if f.MaxLength > 0 && f.MaxLength < len(fieldString) {
return fieldString[:f.MaxLength] + "..."
fieldNode := fieldSelection.Get(f.NodeIndex).FirstChild
if fieldNode.Type == html.TextNode {
fieldString = fieldSelection.Get(f.NodeIndex).FirstChild.Data
if f.MaxLength > 0 && f.MaxLength < len(fieldString) {
return fieldString[:f.MaxLength] + "..."
}
}
}
return fieldString
Expand Down Expand Up @@ -366,6 +373,7 @@ type Crawler struct {
Loc string `yaml:"loc"`
Relative bool `yaml:"relative"`
OnSubpage []string `yaml:"on_subpage"`
Attr string `yaml:"attr"`
} `yaml:"url"`
Date struct {
Day DateLocator `yaml:"day"`
Expand Down