Skip to content

Instantly share code, notes, and snippets.

@timhberry
Created January 17, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timhberry/fd984770997ed76630600b42174c8500 to your computer and use it in GitHub Desktop.
Save timhberry/fd984770997ed76630600b42174c8500 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"os"
)
type Dialog struct {
Episodename string `json:"episodename"`
Act string `json:"act"`
Scenenumber string `json:"scenenumber"`
Texttype string `json:"texttype"`
Who string `json:"who"`
Text string `json:"text"`
Speech string `json:"speech"`
Released string `json:"released"`
Episode string `json:"episode"`
Imdbrating string `json:"imdbrating"`
Imdbid string `json:"imdbid"`
Season string `json:"season"`
}
type Results struct {
SearchKey string
Lines []Dialog
Images map[string]string
}
type TMDBQuery struct {
MovieResults []string `json:"-"`
PersonResults []string `json:"-"`
TvResults []string `json:"-"`
TvEpisodeResults []struct {
AirDate string `json:"air_date"`
EpisodeNumber int `json:"episode_number"`
ID int `json:"id"`
Name string `json:"name"`
Overview string `json:"overview"`
ProductionCode string `json:"production_code"`
SeasonNumber int `json:"season_number"`
ShowID int `json:"show_id"`
StillPath string `json:"still_path"`
VoteAverage float64 `json:"vote_average"`
VoteCount int `json:"vote_count"`
} `json:"tv_episode_results"`
TvSeasonResults string `json:"-"`
}
var API_KEY string
var tpl = template.Must(template.ParseFiles("index.html"))
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(r.URL.String())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal server error"))
return
}
params := u.Query()
searchKey := params.Get("q")
results := &Results{}
results.Images = make(map[string]string)
results.SearchKey = searchKey
endpoint := fmt.Sprintf("https://tngapi-awicwils6q-ew.a.run.app/?q=%s", url.QueryEscape(searchKey))
resp, err := http.Get(endpoint)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewDecoder(resp.Body).Decode(&results.Lines)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, d := range results.Lines {
tmdbquery := &TMDBQuery{}
imdbid := d.Imdbid
_, ok := results.Images[imdbid]
if !ok {
tmdbep := fmt.Sprintf("https://api.themoviedb.org/3/find/%s?api_key=%s&language=en-US&external_source=imdb_id", imdbid, API_KEY)
tmdbresp, err := http.Get(tmdbep)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer tmdbresp.Body.Close()
if tmdbresp.StatusCode != 200 {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = json.NewDecoder(tmdbresp.Body).Decode(&tmdbquery)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(tmdbquery.TvEpisodeResults) > 0 {
results.Images[imdbid] = tmdbquery.TvEpisodeResults[0].StillPath
} else {
results.Images[imdbid] = "8do6gZErem4wfdPwALiT8agtJfb.jpg"
}
}
}
err = tpl.Execute(w, results)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
func main() {
API_KEY = os.Getenv("API_KEY")
if API_KEY == "" {
fmt.Println("No API_KEY in environment")
os.Exit(1)
}
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("assets"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fs))
mux.HandleFunc("/search", searchHandler)
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":8080", mux)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment