-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* first (intal) draft * fixed the testcases * added a cronjob * changed the cronjob accordingly * reformated the test-html * formatting * fixed the movie title * made sure that even legacy feeds are saved * fixed linting issues * refactored the canals to be cleaner * documented `OMDB_API_KEY` * rebase
- Loading branch information
1 parent
a2f04e1
commit c304432
Showing
17 changed files
with
1,939 additions
and
638 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,60 @@ | ||
package movie_parsers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type OmdbResults struct { | ||
ReleaseYear string `json:"Year"` | ||
Runtime string | ||
Genre string | ||
Director string | ||
Actors string | ||
Plot string | ||
ImdbRating string `json:"imdbRating"` | ||
} | ||
|
||
func GetOmdbMovie(id string) (*OmdbResults, error) { | ||
url := fmt.Sprintf("https://www.omdbapi.com/?r=json&v=1&i=%s&apikey=%s", id, os.Getenv("OMDB_API_KEY")) | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.WithField("url", url).WithError(err).Error("Error while getting response for request") | ||
return nil, err | ||
} | ||
// check if the api key is valid | ||
if resp.StatusCode == http.StatusUnauthorized { | ||
return nil, errors.New("missing or invalid api key for omdb (environment variable OMDB_API_KEY)") | ||
} | ||
// other errors | ||
if resp.StatusCode != http.StatusOK { | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.WithError(err).Warn("Unable to read http body") | ||
return nil, err | ||
} else { | ||
log.WithField("status", resp.StatusCode).WithField("status", resp.Status).WithField("body", string(body)).Error("error while getting omdb movie") | ||
return nil, errors.New("error while getting omdb movie") | ||
} | ||
} | ||
defer func(Body io.ReadCloser) { | ||
err := Body.Close() | ||
if err != nil { | ||
log.WithField("url", url).WithError(err).Error("Error while closing body") | ||
} | ||
}(resp.Body) | ||
// parse the response body | ||
var res OmdbResults | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
log.WithField("url", url).WithError(err).Error("Error while unmarshalling omdbResults") | ||
return nil, err | ||
} | ||
return &res, nil | ||
} |
Oops, something went wrong.