boxd/main.go
x 5a31b4899c
Logging Movies + Listed Logged Movies
search movie using tmdb.org and log them on the database with the
current date. simple as that
2024-03-11 20:07:59 +05:30

152 lines
4.5 KiB
Go

package main
import (
"fmt"
"log"
"time"
"strconv"
"database/sql"
"github.com/charmbracelet/huh"
"github.com/imroc/req/v3"
"github.com/tidwall/gjson"
"github.com/spf13/cobra"
_ "github.com/mattn/go-sqlite3"
)
func entryExists(db *sql.DB, name string) (bool, error) {
var count int
row := db.QueryRow("SELECT COUNT(*) FROM movies WHERE imdb = ?", name)
err := row.Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
func logMovie() {
// get the watched movie
var inputstring string
huh.NewInput().Title("what did you watch today ?").Value(&inputstring).Run()
// search it on tmdb
url := "https://api.themoviedb.org/3/search/movie?query=" + inputstring + "&page=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 {
title := gjson.Get(value.String(), "title")
id := gjson.Get(value.String(), "id")
releaseDate := gjson.Get(value.String(), "release_date")
comb := title.String() + " - " + releaseDate.String()
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 movie
movid := MovieInfo[watchedMovie]
url = "https://api.themoviedb.org/3/movie/" + strconv.FormatInt(movid, 10)
resp, err = client.R().Get(url)
if err != nil {
log.Fatal(err)
}
ogTitle := gjson.Get(resp.String(), "original_title")
plot := gjson.Get(resp.String(), "overview")
imdbid := gjson.Get(resp.String(), "imdb_id")
releasedday := gjson.Get(resp.String(), "release_date")
var ratingvalue string
ratetitle := "how would you rate " + ogTitle.String() + "?"
huh.NewInput().Title(ratetitle).Value(&ratingvalue).Run()
var movopinion string
huh.NewText().Title("what did you think of this movie ?").Value(&movopinion).Run()
var recomend bool
huh.NewConfirm().Title("do you recomend this to others ?").Affirmative("Yes!").Negative("No.").Value(&recomend).Run()
currentdata := time.Now()
watchedDate := currentdata.Format("2006-01-02")
// log info to the database
db, err := sql.Open("sqlite3", "./movies.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
createTableSQL := `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
);`
_, err = db.Exec(createTableSQL)
if err != nil {
log.Fatal(err)
}
entryExists, err := entryExists(db, imdbid.String())
if err != nil {
log.Fatal(err)
}
if entryExists {
fmt.Println("movie already exist in the database")
} 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)
}
fmt.Println("movie logged successfully")
}
}
func main() {
cmdLog := &cobra.Command{
Use: "a",
Short: "log movie",
Long: `kind of like a movie diary with required
information about the watched movies`,
Run: func(cmd *cobra.Command, args []string) {
logMovie()
},
}
cmdList := &cobra.Command{
Use: "l",
Short: "list watched movies",
Long: `kind of like a movie diary with required
information about the watched movies`,
Run: func(cmd *cobra.Command, args []string) {
Listmovies()
},
}
rootCmd := &cobra.Command{Use: "boxd"}
rootCmd.AddCommand(cmdLog)
rootCmd.AddCommand(cmdList)
rootCmd.Execute()
}