package main import ( "fmt" "log" "os" "strconv" "strings" "time" "github.com/charmbracelet/huh" "github.com/charmbracelet/huh/spinner" "github.com/imroc/req/v3" _ "github.com/mattn/go-sqlite3" "github.com/tidwall/gjson" ) func LOGMedia(isMovie bool, isSeries bool, isEpisode bool) { db := OpenDatabase() defer db.Close() if isEpisode == true { LogEpisode(db) } else { // get the watched media var inputstring string huh.NewForm(huh.NewGroup(huh.NewInput().Title("what did you watch today ?").Value(&inputstring)).WithShowHelp(true)).Run() replacedinput := strings.Replace(inputstring, " ", "+", -1) // search it on tmdb var url string if isMovie == true && isSeries == false { url = "https://api.themoviedb.org/3/search/movie?query=" + replacedinput + "&page=1" } else if isMovie == false && isSeries == true { url = "https://api.themoviedb.org/3/search/tv?query=" + replacedinput + "&page=1" } else { fmt.Println("oops there is some confusion") os.Exit(1) } client := req.C() client.SetCommonHeader("accept", "application/json") client.SetCommonHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIzZDllYmUxOTNhMGU4OWFjMjM0MzZjNGJhNDg1NTY5ZCIsInN1YiI6IjY1ZWQ3ZTFjYmRjMzRjMDE0NzMyMDgxYiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.cJ5Aws6Um2VEcyqjhii5IjEihYyGmw5ICbh8ldHVqIs") resp, err := client.R().Get(url) if err != nil { log.Fatal(err) } // parsing found json data MovieInfo := make(map[string]int64) var MovieList []string result := gjson.Get(resp.String(), "results") result.ForEach(func(key, value gjson.Result) bool { var title gjson.Result var id gjson.Result var releaseDate gjson.Result if isMovie == true && isSeries == false { title = gjson.Get(value.String(), "title") id = gjson.Get(value.String(), "id") releaseDate = gjson.Get(value.String(), "release_date") } else if isMovie == false && isSeries == true { title = gjson.Get(value.String(), "name") id = gjson.Get(value.String(), "id") releaseDate = gjson.Get(value.String(), "first_air_date") } else { fmt.Println("oops there is some confusion") os.Exit(1) } datesplit := strings.Split(releaseDate.String(), "-") var releaseYear string if datesplit[0] == "" { releaseYear = "unknown" } else { releaseYear = datesplit[0] } comb := title.String() + " (" + releaseYear + ")" MovieInfo[comb] = id.Int() return true }) for key := range MovieInfo { MovieList = append(MovieList, key) } var watchedMovie string huh.NewSelect[string]().Title("so which one ?").Options(huh.NewOptions(MovieList[:]...)...).Value(&watchedMovie).Run() // fetch info about the watched media movid := MovieInfo[watchedMovie] if isMovie == true && isSeries == false { url = "https://api.themoviedb.org/3/movie/" + strconv.FormatInt(movid, 10) } else if isMovie == false && isSeries == true { url = "https://api.themoviedb.org/3/tv/" + strconv.FormatInt(movid, 10) } else { fmt.Println("oops there is some confusion") os.Exit(1) } resp, err = client.R().Get(url) if err != nil { log.Fatal(err) } var ogTitle gjson.Result var plot gjson.Result var imdbid gjson.Result var releasedday gjson.Result if isMovie == true && isSeries == false { ogTitle = gjson.Get(resp.String(), "title") plot = gjson.Get(resp.String(), "overview") imdbid = gjson.Get(resp.String(), "imdb_id") releasedday = gjson.Get(resp.String(), "release_date") } else if isMovie == false && isSeries == true { ogTitle = gjson.Get(resp.String(), "name") plot = gjson.Get(resp.String(), "overview") imdbid = gjson.Get(resp.String(), "id") releasedday = gjson.Get(resp.String(), "first_air_date") } else { fmt.Println("oops there is some confusion") os.Exit(1) } var ratingvalue string var movopinion string var recomend bool var isTVFinished bool if isMovie == true && isSeries == false { ratetitle := "how would you rate " + ogTitle.String() + "?" huh.NewForm(huh.NewGroup( huh.NewInput().Title(ratetitle).Value(&ratingvalue), huh.NewText().Title("what did you think of this movie ?").Value(&movopinion), huh.NewConfirm().Title("do you recomend this to others ?").Affirmative("absolutely").Negative("nah").Value(&recomend), )).Run() } else if isMovie == false && isSeries == true { isTVFinishedTitle := "did you finish watching " + ogTitle.String() + "?" huh.NewConfirm().Title(isTVFinishedTitle).Affirmative("yes").Negative("nope").Value(&isTVFinished).Run() } else { fmt.Println("oops there is some confusion") os.Exit(1) } if isMovie == false && isSeries == true && isTVFinished == true { ratetitle := "how would you rate " + ogTitle.String() + "?" huh.NewForm(huh.NewGroup( huh.NewInput().Title(ratetitle).Value(&ratingvalue), huh.NewText().Title("what did you think of this show ?").Value(&movopinion), huh.NewConfirm().Title("do you recomend this to others ?").Affirmative("absolutely").Negative("nah").Value(&recomend), )).Run() } else if isMovie == false && isSeries == true && isTVFinished == false { fmt.Println("show added to watching section try logging your episodes now.") } else { fmt.Println("oops there is some confusion") os.Exit(1) } currentdata := time.Now() watchedDate := currentdata.Format("2006-01-02") var dbTableSquema string if isMovie == true && isSeries == false { dbTableSquema = `CREATE TABLE IF NOT EXISTS movies ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, plot TEXT, releasedDate DATETIME, imdb TEXT UNIQUE, watchedOn DATETIME, rating INTEGER, opinion TEXT, recommended INTEGER );` } else if isMovie == false && isSeries == true { dbTableSquema = `CREATE TABLE IF NOT EXISTS series ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, plot TEXT, releasedDate DATETIME, tmdb TEXT UNIQUE, finished INTEGER, watchedOn DATETIME, rating INTEGER, opinion TEXT, recommended INTEGER );` } else { fmt.Println("oops there is some confusion") os.Exit(1) } _, err = db.Exec(dbTableSquema) if err != nil { log.Fatal(err) } action := func() { time.Sleep(1 * time.Second) } _ = spinner.New().Title("processing").Action(action).Run() entryExists, err := DBEntryExist(db, imdbid.String(), isMovie, isSeries) if err != nil { log.Fatal(err) } if entryExists { fmt.Println("you already logged this media.") } else { if isMovie == true && isSeries == false { if ogTitle.String() == "" || plot.String() == "" || releasedday.String() == "" || imdbid.String() == "" || watchedDate == "" || ratingvalue == "" || movopinion == "" { fmt.Println("you need to enter all the details") } else { insertSQL := `INSERT INTO movies (title, plot, releasedDate, imdb, watchedOn, rating, opinion, recommended) VALUES (?, ?, ?, ?, ?, ?, ?, ?);` _, err = db.Exec(insertSQL, ogTitle.String(), plot.String(), releasedday.String(), imdbid.String(), watchedDate, ratingvalue, movopinion, recomend) if err != nil { log.Fatal(err) } } } else if isMovie == false && isSeries == true && isTVFinished == true { if ogTitle.String() == "" || plot.String() == "" || releasedday.String() == "" || imdbid.String() == "" || watchedDate == "" || ratingvalue == "" || movopinion == "" { fmt.Println("you need to enter all the details") } else { insertSQL := `INSERT INTO series (title, plot, releasedDate, tmdb, finished, watchedOn, rating, opinion, recommended) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);` _, err = db.Exec(insertSQL, ogTitle.String(), plot.String(), releasedday.String(), imdbid.String(), isTVFinished, watchedDate, ratingvalue, movopinion, recomend) if err != nil { log.Fatal(err) } } } else if isMovie == false && isSeries == true && isTVFinished == false { if ogTitle.String() == "" || plot.String() == "" || releasedday.String() == "" || imdbid.String() == "" { fmt.Println("something went wrong while fetching details") } else { insertSQL := `INSERT INTO series (title, plot, releasedDate, tmdb, finished) VALUES (?, ?, ?, ?, ?);` _, err = db.Exec(insertSQL, ogTitle.String(), plot.String(), releasedday.String(), imdbid.String(), isTVFinished) if err != nil { log.Fatal(err) } } } else { fmt.Println("oops there is some confusion") os.Exit(1) } textOnSuccess := ogTitle.String() + " was logged to your diary." fmt.Println(textOnSuccess) } } }