-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kubegu
committed
Apr 16, 2024
1 parent
6a9a60f
commit 33fff6e
Showing
7 changed files
with
151 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package libraryle | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/gunni1/leipzig-library-game-stock-api/domain" | ||
) | ||
|
||
const ( | ||
resultItemSelector string = "h2[class^=recordtitle]" | ||
titleSelector string = "a[href^='/webOPACClient/singleHit']" | ||
availabilitySelector string = "span[class^=textgruen]" | ||
) | ||
|
||
func (libClient Client) FindAvailabelGames(branchCode int, platform string) []domain.Game { | ||
sessionErr := libClient.openSession() | ||
if sessionErr != nil { | ||
fmt.Println(sessionErr) | ||
return nil | ||
} | ||
request := createGameSearchRequest(branchCode, platform, libClient.session) | ||
httpClient := http.Client{} | ||
response, err := httpClient.Do(request) | ||
if err != nil { | ||
log.Println("error during search") | ||
return nil | ||
} | ||
defer response.Body.Close() | ||
|
||
games, parseResultErr := parseGameSearchResult(response.Body) | ||
if parseResultErr != nil { | ||
log.Fatalln(parseResultErr) | ||
return nil | ||
} | ||
return games | ||
} | ||
|
||
// Takes a html as reader from a webopac search and | ||
// try to parse it to an array of games that are listed as available. | ||
func parseGameSearchResult(searchResult io.Reader) ([]domain.Game, error) { | ||
doc, docErr := goquery.NewDocumentFromReader(searchResult) | ||
if docErr != nil { | ||
log.Println("Could not create document from response.") | ||
return nil, docErr | ||
} | ||
games := make([]domain.Game, 0) | ||
doc.Find(resultItemSelector).Each(func(i int, resultItem *goquery.Selection) { | ||
title := resultItem.Find(titleSelector).Text() | ||
if isGameAvailable(resultItem.Parent()) { | ||
games = append(games, domain.Game{Title: title}) | ||
} | ||
}) | ||
return games, nil | ||
} | ||
|
||
func isGameAvailable(searchHitNode *goquery.Selection) bool { | ||
return len(searchHitNode.Find(availabilitySelector).Nodes) > 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package libraryle | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParseMovieCopiesResult(t *testing.T) { | ||
testResponse := loadTestData("movie_copies_example.html") | ||
movies := parseMovieCopiesPage("Terminator - Genesis", testResponse) | ||
Equal(t, 6, len(movies)) | ||
|
||
available := 0 | ||
for _, movie := range movies { | ||
if movie.IsAvailable { | ||
available++ | ||
} | ||
} | ||
Equal(t, 2, available) | ||
} | ||
|
||
func TestMovieSearchRequestHasCookiesSet(t *testing.T) { | ||
session := webOpacSession{jSessionId: jSessionId, userSessionId: userSessionId} | ||
request := createMovieSearchRequest("Terminator", session) | ||
assertSessionCookiesExists(request, t) | ||
} | ||
|
||
func TestMovieSearchRequestHasQueryParamsSet(t *testing.T) { | ||
session := webOpacSession{jSessionId: jSessionId, userSessionId: userSessionId} | ||
request := createMovieSearchRequest("Terminator", session) | ||
Equal(t, "submit", request.URL.Query().Get("methodToCall")) | ||
Equal(t, "331", request.URL.Query().Get("searchCategories[0]")) | ||
Equal(t, "500", request.URL.Query().Get("numberOfHits")) | ||
Equal(t, "3", request.URL.Query().Get("searchRestrictionID[2]")) | ||
Equal(t, "29", request.URL.Query().Get("searchRestrictionValue1[2]")) | ||
Equal(t, "0", request.URL.Query().Get("selectedViewBranchlib")) | ||
Empty(t, request.URL.Query().Get("selectedSearchBranchlib")) | ||
} |