Skip to content

Commit

Permalink
Fix Issue Detected by Linters
Browse files Browse the repository at this point in the history
Add errorchecks
Fix typos
Fix formatting
  • Loading branch information
Jisin0 committed Jun 4, 2024
1 parent 33be770 commit 58d686f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
12 changes: 9 additions & 3 deletions imdb/advancedsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ func (*ImdbClient) AdvancedSearchTitle(opts *AdvancedSearchTitleOpts) ([]*Advanc

var data a

json.Unmarshal([]byte(htmlquery.InnerText(dataNode)), &data)
err = json.Unmarshal([]byte(htmlquery.InnerText(dataNode)), &data)
if err != nil {
return nil, errors.New("failed to unmarshal results data")
}

results := data.Props.PropsPage.SearchResults.TitleResults.Items

Expand Down Expand Up @@ -240,7 +243,7 @@ type AdvancedSearchNameResult struct {
KnownFor struct {
// Indicates wether the title can have episodes.
CanHaveEpisodes bool `json:"canHaveEpisodes"`
// Orginal or full title of the movie or show.
// Original or full title of the movie or show.
OriginalTitle string `json:"originalTitle"`
// Imdb ID of the title.
ID string `json:"titleId"`
Expand Down Expand Up @@ -297,7 +300,10 @@ func (*ImdbClient) AdvancedSearchName(opts *AdvancedSearchNameOpts) ([]*Advanced

var data a

json.Unmarshal([]byte(htmlquery.InnerText(dataNode)), &data)
err = json.Unmarshal([]byte(htmlquery.InnerText(dataNode)), &data)
if err != nil {
return nil, errors.New("failed to unmarshal results data")
}

results := data.Props.PropsPage.SearchResults.NameResults.Items

Expand Down
6 changes: 2 additions & 4 deletions imdb/advancedsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ func TestAdvancedSearchTitle(t *testing.T) {
if i == lastIndex {
t.Errorf("error expected for item %v but results found", i)
} else {
t.Logf("item %v succesfully returned", i)
t.Logf("item %v successfully returned", i)
}
}
})

}
}

Expand All @@ -62,10 +61,9 @@ func TestAdvancedSearchName(t *testing.T) {
if i == lastIndex {
t.Errorf("error expected for item %v but results found", i)
} else {
t.Logf("item %v succesfully returned", i)
t.Logf("item %v successfully returned", i)
}
}
})

}
}
29 changes: 14 additions & 15 deletions imdb/getmovie.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const (
)

type Movie struct {
//Imdb id of the movie .
// Imdb id of the movie .
ID string
// Years of release of the movie, A range for shows over multiple years.
ReleaseYear string
MovieJsonContent
MovieJSONContent
MovieDetailsSection
}

Expand All @@ -48,8 +48,8 @@ type MovieDetailsSection struct {
}

// Data scraped from the json attached in the script tag.
type MovieJsonContent struct {
//Type of the title possibble values are Movie, TVSeries etc.
type MovieJSONContent struct {
// Type of the title possibble values are Movie, TVSeries etc.
Type string `json:"@type"`
// ID of the movie
ID string
Expand All @@ -59,11 +59,11 @@ type MovieJsonContent struct {
Title string `json:"name"`
// Url of the full size poster image.
PosterURL string `json:"image"`
// Content rating class (currenly undocumented).
// Content rating class (currently undocumented).
ContentRating string `json:"contentRating"`
// Date the movie was released on in yyyy-mm-dd format.
ReleaseDate string `json:"datePublished"`
// Keywords associated with the movie in a comma seperated list.
// Keywords associated with the movie in a comma separated list.
Keywords string `json:"keywords"`
// Ratings for the movie.
Rating Rating `json:"aggregateRating"`
Expand Down Expand Up @@ -181,23 +181,22 @@ func (c *ImdbClient) GetMovie(id string) (*Movie, error) {
ID: id,
}

// var ok bool

// movie, ok = encode.Xpath(doc, movie).(Movie)
// if !ok {
// return nil, errors.New("unknown type returned from encode.Xpath")
// }

jsonDataNode := htmlquery.FindOne(doc, "//script[@type='application/ld+json']")
if jsonDataNode == nil {
return nil, errors.New("json data node not found")
}

json.Unmarshal([]byte(htmlquery.InnerText(jsonDataNode)), &movie.MovieJsonContent)
err = json.Unmarshal([]byte(htmlquery.InnerText(jsonDataNode)), &movie.MovieJSONContent)
if err != nil {
return nil, errors.New("failed to unmarshal results data")
}

detailsNode, err := htmlquery.Query(doc, "//section[@data-testid='Details']/div[@data-testid='title-details-section']")
if detailsNode != nil && err == nil {
encode.Xpath(detailsNode, &movie.MovieDetailsSection)
err = encode.Xpath(detailsNode, &movie.MovieDetailsSection)
if err != nil {
return nil, errors.Wrap(err, "error while scraping data with xpath")
}
}

releaseYearNode, err := htmlquery.Query(doc, "//h1[@data-testid='hero__pageTitle']/..//a[contains(@href, 'releaseinfo')]")
Expand Down
2 changes: 1 addition & 1 deletion imdb/getmovie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestGetMovie(t *testing.T) {
res, err := c.GetMovie(id)
if err != nil {
t.Logf("Expected error for ID %s: %v", id, err)

if id != invalidMovieID {
t.Errorf("Unexpected error for valid ID %s: %v", id, err)
}
Expand All @@ -34,5 +35,4 @@ func TestGetMovie(t *testing.T) {
}
})
}

}
21 changes: 15 additions & 6 deletions imdb/getperson.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Person struct {
ID string
// Links to movies/show the person is known for.
KnownFor types.Links `xpath:"|linklist"`
PersonJsonContent
PersonJSONContent
PersonDetailsSection
PersonDYKSection
}
Expand Down Expand Up @@ -55,8 +55,8 @@ type PersonDetailsSection struct {
OtherWorks string `xpath:"/li[@data-testid='nm_pd_wrk']/div"`
}

// Json data available for the person the page.
type PersonJsonContent struct {
// JSON data available for the person the page.
type PersonJSONContent struct {
// URL of the imdb page.
URL string `json:"url"`
// Name of the person.
Expand Down Expand Up @@ -114,16 +114,25 @@ func (c *ImdbClient) GetPerson(id string) (*Person, error) {
return nil, errors.New("json data node not found")
}

json.Unmarshal([]byte(htmlquery.InnerText(jsonDataNode)), &person.PersonJsonContent)
err = json.Unmarshal([]byte(htmlquery.InnerText(jsonDataNode)), &person.PersonJSONContent)
if err != nil {
return nil, errors.New("failed to unmarshal results data")
}

detailsNode, err := htmlquery.Query(doc, "//section[@data-testid='PersonalDetails']/div[2]/ul")
if detailsNode != nil && err == nil {
encode.Xpath(detailsNode, &person.PersonDetailsSection)
err = encode.Xpath(detailsNode, &person.PersonDetailsSection)
if err != nil {
return nil, errors.Wrap(err, "error while scraping data with xpath")
}
}

dykNode, err := htmlquery.Query(doc, "//section[@data-testid='DidYouKnow']")
if dykNode != nil && err == nil {
encode.Xpath(dykNode, &person.PersonDYKSection)
err = encode.Xpath(dykNode, &person.PersonDYKSection)
if err != nil {
return nil, errors.Wrap(err, "error while scraping data with xpath")
}
}

knownForNode, err := htmlquery.Query(doc, "//div[@data-testid='Filmography']//div[@data-testid='nm_flmg_kwn_for']//div[ends-with(@data-testid, 'container')]")
Expand Down
1 change: 1 addition & 0 deletions imdb/getperson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestGetPersons(t *testing.T) {
res, err := c.GetPerson(id)
if err != nil {
t.Logf("Expected error for ID %s: %v", id, err)

if id != invalidPersonID {
t.Errorf("Unexpected error for valid ID %s: %v", id, err)
}
Expand Down

0 comments on commit 58d686f

Please sign in to comment.