Skip to content

Commit

Permalink
fix lib api update, adapt goquery selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
kubegu committed Feb 22, 2024
1 parent 8806c2a commit efe1c1b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
15 changes: 7 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ ENV CGO_ENABLED=0

WORKDIR /app

COPY go.mod go.sum ./
COPY cmd/server/main.go ./
COPY pkg ./pkg
COPY . ./

RUN go mod download
RUN go build -o lib-api
RUN go build -o web

# Stage Run
FROM alpine:latest
WORKDIR /app
COPY --from=build /app/lib-api .
RUN chmod +x lib-api
CMD ["./lib-api"]
EXPOSE 8080
COPY --from=build /app/web .
RUN chmod +x web
CMD ["./web"]
EXPOSE 3000
24 changes: 10 additions & 14 deletions library-le/searchresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package libraryle
import (
"log"
"net/http"
"strings"

"github.com/PuerkitoBio/goquery"
"github.com/gunni1/leipzig-library-game-stock-api/domain"
)

const (
resultItemSelector string = "td[style*='width:100%']"
resultDataSelector string = "table.data"
gameTitleSelector string = "a[href^='/webOPACClient/singleHit']"
resultItemSelector string = "h2[class^=recordtitle]"
gameTitleSelector string = "a[href^='/webOPACClient/singleHit']"
availabilitySelector string = "span[class^=textgruen]"
)

// Takes a http.Response from a webopac search and
Expand All @@ -24,18 +23,15 @@ func parseSearchResult(searchResult *http.Response, branchCode int) ([]domain.Ga
return nil, docErr
}
games := make([]domain.Game, 0)
doc.Find(resultDataSelector).Each(func(i int, data *goquery.Selection) {
data.Find(resultItemSelector).Each(func(i int, resultItem *goquery.Selection) {
title := resultItem.Find(gameTitleSelector).Text()
if isAvailable(resultItem.Find("span").Text()) {
games = append(games, domain.Game{Title: title, Branch: BranchCodes[branchCode]})
}
})
doc.Find(resultItemSelector).Each(func(i int, resultItem *goquery.Selection) {
title := resultItem.Find(gameTitleSelector).Text()
if isAvailable(resultItem.Parent()) {
games = append(games, domain.Game{Title: title, Branch: BranchCodes[branchCode]})
}
})
return games, nil
}

// See tests for all possible response codes
func isAvailable(responseCode string) bool {
return strings.Contains(responseCode, "ausleihbar") || strings.Contains(responseCode, "heute zurückgebucht")
func isAvailable(searchHitNode *goquery.Selection) bool {
return len(searchHitNode.Find(availabilitySelector).Nodes) > 0
}
18 changes: 18 additions & 0 deletions library-le/searchresult_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="container-fluid">
<div class="row border-bottom py-2 pt-md-3">
<div class="col-10 col-md-11">
<h2 class="recordtitle">
<a href="/webOPACClient/singleHit.do?methodToCall=showHit&amp;curPos=1&amp;identifier=-1_FT_887324501">Spiel1</a>
</h2>
<span class="textrot">Der gewählte Titel ist in der aktuellen Zweigstelle entliehen.</span>
</div>
</div>
<div class="row border-bottom py-2 pt-md-3">
<div class="col-10 col-md-11">
<h2 class="recordtitle">
<a href="/webOPACClient/singleHit.do?methodToCall=showHit&amp;curPos=2&amp;identifier=-1_FT_887324501">Spiel2</a>
</h2>
<span class="textgruen" >Ein oder mehrere Exemplare dieses Titels sind in der aktuellen Zweigstelle ausleihbar.</span>
</div>
</div>
</div>
7 changes: 0 additions & 7 deletions library-le/searchresult_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ package libraryle

import (
"testing"

. "github.com/stretchr/testify/assert"
)

func TestAvailability(t *testing.T) {
False(t, isAvailable("Der gewählte Titel ist in der aktuellen Zweigstelle entliehen."))
False(t, isAvailable("Alle Exemplare des gewählten Titels sind entliehen."))
False(t, isAvailable("Ein Exemplar finden Sie in einer anderen Zweigstelle."))

True(t, isAvailable("Ein Exemplar dieses Titels wurde heute zurückgebucht."))
True(t, isAvailable("Ein oder mehrere Exemplare dieses Titels sind in der aktuellen Zweigstelle ausleihbar."))
}
10 changes: 4 additions & 6 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
</head>
<body>
<div class="container">
<form hx-post="/game-index/" hx-target="#display-games" hx-indicator="#spinner">
<div class="mb-3">
<label for="branchSelect" class="form-label">Bibliothek wählen:</label>
<form hx-post="/game-index/" hx-target="#display-games" hx-indicator="#spinner" class="d-flex flex-wrap justify-content-center">
<div class="p-2">
<select id="branchSelect" name="branch" class="form-select" style="width:auto;">
<option>Stadtbibliothek</option>
<option>Gohlis</option>
Expand All @@ -27,15 +26,14 @@
<option>Grünau-Süd</option>
</select>
</div>
<div class="mb-3">
<label for="platformSelect" class="form-label">Plattform wählen:</label>
<div class="p-2">
<select id="platformSelect" name="platform" class="form-select" style="width:auto;">
<option>Switch</option>
<option>XBox</option>
<option>Playstation</option>
</select>
</div>
<div class="mb-3 ">
<div class="p-2">
<button type="submit" class="btn btn-primary">Los</button>
</div>
</form>
Expand Down

0 comments on commit efe1c1b

Please sign in to comment.