Skip to content

Commit

Permalink
Movie search (#7)
Browse files Browse the repository at this point in the history
#6 add movie search


---------

Co-authored-by: kubegu <[email protected]>
  • Loading branch information
gunni1 and kubegu authored Mar 27, 2024
1 parent 4ee3221 commit 8d5cee2
Show file tree
Hide file tree
Showing 21 changed files with 700 additions and 218 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.22'

- name: Go Mod
run: go mod download
Expand Down
8 changes: 1 addition & 7 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ import (
func main() {
branchPtr := flag.Int("branch", 20, "Branch code of the library")
platformPtr := flag.String("platform", "Nintendo Switch", "Console platform to list games")
allBranchesPtr := flag.Bool("all", false, "Search in all Branches")

flag.Parse()

client := libClient.Client{}

var games []domain.Game
if *allBranchesPtr {
fmt.Printf("Searching all games for %s \n", *platformPtr)
games = client.GetAllAvailableGamesPlatform(*platformPtr)
} else {
games = client.FindAvailabelGames(*branchPtr, *platformPtr)
}
games = client.FindAvailabelGames(*branchPtr, *platformPtr)

for _, game := range games {
fmt.Printf("%s (%s)\n", game.Title, game.Branch)
Expand Down
7 changes: 7 additions & 0 deletions domain/movie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package domain

type Movie struct {
Title string `json:"title"`
Branch string `json:"branch"`
IsAvailable bool `json:"isAvailable"`
}
13 changes: 7 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module github.com/gunni1/leipzig-library-game-stock-api

go 1.21
go 1.22

require github.com/stretchr/testify v1.8.3

require github.com/PuerkitoBio/goquery v1.8.0
require (
github.com/PuerkitoBio/goquery v1.9.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/net v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
50 changes: 39 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
94 changes: 38 additions & 56 deletions library-le/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"sync"

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

const (
LIB_BASE_URL = "https://webopac.stadtbibliothek-leipzig.de"
)

// Deprecated?
var BranchCodes = map[int]string{
0: "Stadtbibliothek",
20: "Bibliothek Plagwitz",
Expand All @@ -31,6 +34,7 @@ var BranchCodes = map[int]string{
90: "Fahrbibliothek",
}

// Deprecated?
func BranchCodeKeys() []int {
keys := make([]int, 0, len(BranchCodes))
for key := range BranchCodes {
Expand All @@ -54,84 +58,62 @@ func (libClient Client) FindAvailabelGames(branchCode int, platform string) []do
fmt.Println(sessionErr)
return nil
}
request := createSearchRequest(branchCode, platform, libClient.session.jSessionId, libClient.session.userSessionId)
request := createGameSearchRequest(branchCode, platform, libClient.session)
httpClient := http.Client{}
response, err := httpClient.Do(request)
if err != nil {
log.Fatal("error during search")
log.Println("error during search")
return nil
}
defer response.Body.Close()

games, parseResultErr := parseSearchResult(response.Body)
//Add branchCode to games?
games, parseResultErr := parseGameSearchResult(response.Body)
if parseResultErr != nil {
log.Fatalln(parseResultErr)
return nil
}
return games
}

func (libClient Client) GetAllAvailableGamesPlatform(platform string) []domain.Game {
searchResults := make(chan domain.Game)

wg := &sync.WaitGroup{}
for _, code := range BranchCodeKeys() {
wg.Add(1)
go getAvailableGames(code, platform, searchResults, wg, libClient)
// Search for a specific movie title in all library branches
func (libClient Client) FindMovies(title string) []domain.Movie {
sessionErr := libClient.openSession()
if sessionErr != nil {
fmt.Println(sessionErr)
return nil
}
go func() {
wg.Wait()
close(searchResults)
}()
games := make([]domain.Game, 0)
for game := range searchResults {
games = append(games, game)
searchRequest := createMovieSearchRequest(title, libClient.session)
httpClient := http.Client{}
searchResponse, err := httpClient.Do(searchRequest)
if err != nil {
log.Println("error during search")
return nil
}
return games
}
resultTitles := parseMovieSearch(searchResponse.Body)

func getAvailableGames(branchCode int, platform string, results chan domain.Game, wg *sync.WaitGroup, client Client) {
defer wg.Done()
games := client.FindAvailabelGames(branchCode, platform)
for _, game := range games {
results <- game
movies := make([]domain.Movie, 0)
for _, resultTitle := range resultTitles {
movies = append(movies, resultTitle.loadMovieCopies(libClient.session)...)
}
//Parallel Ergebnislinks folgen und Details über Zweigstelle und Verfpgbarkeit sammeln
return movies
}

func createSearchRequest(branchCode int, searchString string, jSessionId string, userSessionId string) *http.Request {
request, _ := http.NewRequest("GET", "https://webopac.stadtbibliothek-leipzig.de/webOPACClient/search.do", nil)
jSessionCookie := &http.Cookie{
Name: "JSESSIONID",
Value: jSessionId,
}
userSessionCookie := &http.Cookie{
Name: "USERSESSIONID",
Value: userSessionId,
}
request.AddCookie(jSessionCookie)
request.AddCookie(userSessionCookie)
// Load all existing copys of a result title over all library branches
func (result searchResult) loadMovieCopies(libSession webOpacSession) []domain.Movie {
request := createRequest(libSession, result.resultUrl)

query := request.URL.Query()
//Fix Query Params to make the search working
query.Add("methodToCall", "submit")
query.Add("methodToCallParameter", "submitSearch")
query.Add("searchCategories[0]", "902")
query.Add("submitSearch", "Suchen")
query.Add("callingPage", "searchPreferences")
query.Add("numberOfHits", "500")
query.Add("timeOut", "20")
//Query Params dependend on user input / session
query.Add("CSId", userSessionId)
query.Add("searchString[0]", searchString)
query.Add("selectedSearchBranchlib", strconv.FormatInt(int64(branchCode), 10))
query.Add("selectedViewBranchlib", strconv.FormatInt(int64(branchCode), 10))
request.URL.RawQuery = query.Encode()
return request
httpClient := http.Client{}
movieResponse, err := httpClient.Do(request)
if err != nil {
log.Println("error during search")
return nil
}
return parseMovieCopiesPage(result.title, movieResponse.Body)
}

func (client *Client) openSession() error {
resp, err := http.Get("https://webopac.stadtbibliothek-leipzig.de/webOPACClient")
resp, err := http.Get(LIB_BASE_URL + "/webOPACClient")
if err != nil {
return err
}
Expand Down
38 changes: 30 additions & 8 deletions library-le/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package libraryle

import (
"net/http"
"testing"

. "github.com/stretchr/testify/assert"
Expand All @@ -11,10 +12,10 @@ const (
userSessionId string = "2267N112S85e7645be446dd6c4e2e4bc558a206f3c4a88788"
)

func TestRequestHasSearchParameters(t *testing.T) {

func TestGameRequestHasSearchParameters(t *testing.T) {
session := webOpacSession{jSessionId: jSessionId, userSessionId: userSessionId}
searchString := "Nintendo Switch"
result := createSearchRequest(40, searchString, jSessionId, userSessionId)
result := createGameSearchRequest(40, searchString, session)

Equal(t, "submit", result.URL.Query().Get("methodToCall"))
Equal(t, "submitSearch", result.URL.Query().Get("methodToCallParameter"))
Expand All @@ -29,14 +30,17 @@ func TestRequestHasSearchParameters(t *testing.T) {
Equal(t, "40", result.URL.Query().Get("selectedSearchBranchlib"))
}

func TestRequestHasCookiesSet(t *testing.T) {
result := createSearchRequest(40, "Nintendo Switch", jSessionId, userSessionId)

Equal(t, 2, len(result.Cookies()))
func TestGameRequestHasCookiesSet(t *testing.T) {
session := webOpacSession{jSessionId: jSessionId, userSessionId: userSessionId}
request := createGameSearchRequest(40, "Nintendo Switch", session)
assertSessionCookiesExists(request, t)
}

func assertSessionCookiesExists(request *http.Request, t *testing.T) {
Equal(t, 2, len(request.Cookies()))
foundJSessionId := false
foundUserSessionId := false
for _, cookie := range result.Cookies() {
for _, cookie := range request.Cookies() {
switch cookie.Name {
case "JSESSIONID":
foundJSessionId = true
Expand All @@ -47,3 +51,21 @@ func TestRequestHasCookiesSet(t *testing.T) {
True(t, foundJSessionId)
True(t, foundUserSessionId)
}

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"))
}
File renamed without changes.
Loading

0 comments on commit 8d5cee2

Please sign in to comment.