Skip to content

Commit

Permalink
feat: permanent cache
Browse files Browse the repository at this point in the history
Cache goes brrrr
  • Loading branch information
TheTipo01 committed Sep 12, 2023
1 parent 547ad9e commit ba4b41c
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ $RECYCLE.BIN/

config.yml
videoDownloader
temp/
temp/
cache.db
Empty file added data/.gitkeep
Empty file.
53 changes: 53 additions & 0 deletions database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"encoding/json"
"github.com/bwmarrin/lit"
tele "gopkg.in/telebot.v3"
)

const cacheTable = "CREATE TABLE IF NOT EXISTS `cache` ( `filename` VARCHAR(15) NOT NULL, `video` TEXT NOT NULL, PRIMARY KEY (`filename`) )"

func execQuery(query string) {
_, err := db.Exec(query)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}

func load() {
var (
filename string
bytes []byte
)

rows, err := db.Query("SELECT filename, video FROM cache")
if err != nil {
lit.Error("Error executing query, %s", err)
return
}

for rows.Next() {
var video tele.Video

err = rows.Scan(&filename, &bytes)
if err != nil {
lit.Error("Error scanning row, %s", err)
continue
}

_ = json.Unmarshal(bytes, &video)
cache[filename] = &video
}
}

func save(video *tele.Video) {
bytes, _ := json.Marshal(video)

_, err := db.Exec("INSERT INTO cache (filename, video) VALUES (?, ?)", video.FileName, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ services:
image: thetipo01/videodownloader
restart: always
volumes:
- ${PWD}/config.yml:/config.yml
- ./data:/data
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
12 changes: 10 additions & 2 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ func videoDownload(c tele.Context) error {
url := cleanURL(c.Message().EntityText(e))

if contains(url, cfg.URLs) {
filename := checkAndDownload(url)
filename, hit := checkAndDownload(url)

err := c.Reply(cache[filename], tele.Silent)
if err != nil {
lit.Error(err.Error())
}

if !hit {
save(cache[filename])
}
} else {
// For twitter, we send the same url with only fx appended to it
if strings.HasPrefix(url, "https://twitter.com") {
Expand All @@ -41,11 +45,15 @@ func inlineQuery(c tele.Context) error {

if isValidURL(text) && contains(text, cfg.URLs) {
text = cleanURL(text)
filename := checkAndDownload(text)
filename, hit := checkAndDownload(text)

// Upload video to channel, so we can send it even in inline mode
_, _ = c.Bot().Send(tele.ChatID(cfg.Channel), cache[filename])

if !hit {
go save(cache[filename])
}

// Create result
results[0] = &tele.VideoResult{
Cache: cache[filename].FileID,
Expand Down
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@ require (
github.com/bwmarrin/lit v0.0.0-20190813132558-fd4b44871312
github.com/kkyr/fig v0.3.2
gopkg.in/telebot.v3 v3.1.3
modernc.org/sqlite v1.25.0
)

require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.1 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
120 changes: 120 additions & 0 deletions go.sum

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"database/sql"
"github.com/bwmarrin/lit"
"github.com/kkyr/fig"
"log"
_ "modernc.org/sqlite"
"strings"
"time"

Expand All @@ -13,12 +15,13 @@ import (
var (
cfg config
cache map[string]*tele.Video
db *sql.DB
)

func init() {
lit.LogLevel = lit.LogError

err := fig.Load(&cfg, fig.File("config.yml"))
err := fig.Load(&cfg, fig.File("config.yml"), fig.Dirs(".", "./data"))
if err != nil {
lit.Error(err.Error())
return
Expand All @@ -37,6 +40,16 @@ func init() {
}

cache = make(map[string]*tele.Video)

// Open database connection
db, err = sql.Open("sqlite", "./data/cache.db")
if err != nil {
lit.Error("Error opening db connection, %s", err)
return
}

execQuery(cacheTable)
load()
}

func main() {
Expand All @@ -58,4 +71,6 @@ func main() {
// Start bot
lit.Info("videoDownloader is now running")
b.Start()

_ = db.Close()
}
7 changes: 5 additions & 2 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func contains(str string, checkFor []string) bool {
return false
}

func checkAndDownload(link string) string {
func checkAndDownload(link string) (string, bool) {
hit := true
lit.Debug(link)

filename := idGen(link) + ".mp4"
Expand All @@ -54,9 +55,11 @@ func checkAndDownload(link string) string {
lit.Error(err.Error())
}
}()

hit = false
}

return filename
return filename, hit
}

// cleanURL removes tracking and other unnecessary parameters from a URL
Expand Down

0 comments on commit ba4b41c

Please sign in to comment.