Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ go.work.sum
/.omdbcache

# Vscode workspace configs
/.vscode
/.vscode

# Env file
.env
3 changes: 1 addition & 2 deletions api/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"path"
"strings"

Expand All @@ -14,7 +13,7 @@ import (
)

var (
allowedTokens = strings.Split(os.Getenv("BOT_TOKEN"), " ")
allowedTokens = strings.Split(plugins.BotToken, " ")
lenAllowedTokens = len(allowedTokens)
)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Jisin0/filmigo v0.2.3
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.29
github.com/fogleman/gg v1.3.0
github.com/joho/godotenv v1.5.1
golang.org/x/image v0.20.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo=
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main
import (
"fmt"
"net/http"
"os"
"time"

"github.com/Jisin0/filmigobot/plugins"
Expand All @@ -21,8 +20,7 @@ const (
func main() {
// Run a useless http server to get a healthy build on koyeb/render
go func() {
port := os.Getenv("PORT")

port := plugins.Port
if port == "" {
port = "8080"
}
Expand All @@ -38,7 +36,7 @@ func main() {
}
}()

token := os.Getenv("BOT_TOKEN")
token := plugins.BotToken
if token == "" {
panic("exiting because no BOT_TOKEN provided")
}
Expand Down
31 changes: 31 additions & 0 deletions plugins/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Setup and initialize environment variables.

package plugins

import (
"fmt"
"os"

"github.com/joho/godotenv"
)

var (
BotToken string // bot token from @botfather
Port string // port to run webapp
DefaultMethod string // default search method
OmdbApiKey string // omdb api key
)

const stringTrue = true

func init() {
err := godotenv.Load()
if err != nil {
fmt.Println("load environment variables failed")
}

BotToken = os.Getenv("BOT_TOKEN")
Port = os.Getenv("PORT")
DefaultMethod = os.Getenv("DEFAULT_SEARCH_METHOD")
OmdbApiKey = os.Getenv("OMDB_API_KEY")
}
20 changes: 13 additions & 7 deletions plugins/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,35 @@ const (

// ImdbInlineSearch searches for query on imdb and returns results to be used in inline queries.
func IMDbInlineSearch(query string) []gotgbot.InlineQueryResult {
var results []gotgbot.InlineQueryResult

rawResults, err := imdbClient.SearchTitles(query)
if err != nil {
return results
return nil
}

results := make([]gotgbot.InlineQueryResult, 0, len(rawResults.Results))

for _, item := range rawResults.Results {
posterURL := item.Image.URL
if posterURL == "" {
posterURL = imdbLogo
}

title := fmt.Sprintf("%s (%v)", item.Title, item.Year)
url := fmt.Sprintf("https://imdb.com/title/%s", item.ID)

results = append(results, gotgbot.InlineQueryResultPhoto{
results = append(results, gotgbot.InlineQueryResultArticle{
Id: searchMethodIMDb + "_" + item.ID,
PhotoUrl: posterURL,
Url: url,
ThumbnailUrl: posterURL,
Title: title,
Description: item.Description,
Caption: fmt.Sprintf("<b><a href='https://imdb.com/title/%s'>%s</a></b>", item.ID, title),
ParseMode: gotgbot.ParseModeHTML,
InputMessageContent: gotgbot.InputTextMessageContent{
MessageText: fmt.Sprintf("<b><a href='%s'>%s</a></b>", url, title),
ParseMode: gotgbot.ParseModeHTML,
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{
PreferSmallMedia: true,
},
},
ReplyMarkup: &gotgbot.InlineKeyboardMarkup{InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{{Text: "Open IMDb", CallbackData: fmt.Sprintf("open_%s_%s", searchMethodIMDb, item.ID)}},
}},
Expand Down
19 changes: 9 additions & 10 deletions plugins/inlinesearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ package plugins

import (
"fmt"
"os"
"strings"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
)

var (
defaulSearchMethod = searchMethodJW
allSearchMethods = []string{searchMethodIMDb, searchMethodOMDb, searchMethodJW}
defaultSearchMethod = searchMethodJW
allSearchMethods = []string{searchMethodIMDb, searchMethodOMDb, searchMethodJW}
)

// Search methods with a whitespace added after for a seamless search.
Expand Down Expand Up @@ -53,12 +52,12 @@ const (
)

func init() {
if defaultMethod := os.Getenv("DEFAULT_SEARCH_METHOD"); defaultMethod != "" {
if defaultMethod == searchMethodIMDb || defaultMethod == searchMethodOMDb || defaulSearchMethod == searchMethodJW {
defaulSearchMethod = defaultMethod
} else {
fmt.Printf("error: unknown search method \"%s\", using default method \"%s\"\n", defaultMethod, defaulSearchMethod)
}
switch DefaultMethod {
case "":
DefaultMethod = defaultSearchMethod
case searchMethodIMDb, searchMethodJW, searchMethodOMDb:
default:
fmt.Printf("error: unknown search method \"%s\", using default method \"%s\"\n", DefaultMethod, defaultSearchMethod)
}
}

Expand Down Expand Up @@ -120,6 +119,6 @@ func getInlineResults(method, query, fullQuery string) []gotgbot.InlineQueryResu
case searchMethodOMDb:
return OMDbInlineSearch(query)
default:
return getInlineResults(defaulSearchMethod, fullQuery, fullQuery)
return getInlineResults(DefaultMethod, fullQuery, fullQuery)
}
}
6 changes: 3 additions & 3 deletions plugins/justwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ var jWClient = justwatch.NewClient(&justwatch.JustwatchClientOpts{Country: jWCou

// JWInlineSearch searches for query on justwatch and returns results to be used in inline queries.
func JWInlineSearch(query string) []gotgbot.InlineQueryResult {
var results []gotgbot.InlineQueryResult

rawResults, err := jWClient.SearchTitle(query)
if err != nil {
return results
return nil
}

results := make([]gotgbot.InlineQueryResult, 0, len(rawResults.Results))

for _, item := range rawResults.Results {
posterURL := item.Poster.FullURL()
if posterURL == "" {
Expand Down
13 changes: 6 additions & 7 deletions plugins/omdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package plugins

import (
"fmt"
"os"
"strings"

"github.com/Jisin0/filmigo/omdb"
Expand All @@ -24,26 +23,26 @@ var (
)

func init() {
if key := os.Getenv("OMDB_API_KEY"); key != notAvailable {
omdbClient = omdb.NewClient(key)
if OmdbApiKey != "" {
omdbClient = omdb.NewClient(OmdbApiKey)

inlineSearchButtons = append(inlineSearchButtons, []gotgbot.InlineKeyboardButton{{Text: "🔍 Search OMDb", SwitchInlineQueryCurrentChat: &inlineOMDbSwitch}})
}
}

// OmdbInlineSearch searches for query on omdb and returns results to be used in inline queries.
func OMDbInlineSearch(query string) []gotgbot.InlineQueryResult {
var results []gotgbot.InlineQueryResult

if omdbClient == nil {
return results
return nil
}

rawResults, err := omdbClient.Search(query)
if err != nil {
return results
return nil
}

results := make([]gotgbot.InlineQueryResult, 0, len(rawResults.Results))

for _, item := range rawResults.Results {
posterURL := item.Poster
if posterURL == notAvailable {
Expand Down