Skip to content

Commit

Permalink
feat: downloader tiktok albums
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTipo01 committed Oct 14, 2023
1 parent 89d56e7 commit a54606f
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 113 deletions.
52 changes: 42 additions & 10 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
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`) )"
const videoTable = "CREATE TABLE IF NOT EXISTS `video` ( `filename` VARCHAR(15) NOT NULL, `video` TEXT NOT NULL, PRIMARY KEY (`filename`) )"
const albumTable = "CREATE TABLE IF NOT EXISTS `album` ( `filename` VARCHAR(11) NOT NULL, `album` 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 execQuery(query ...string) {
for _, q := range query {
_, err := db.Exec(q)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}
}

Expand All @@ -22,7 +25,7 @@ func load() {
bytes []byte
)

rows, err := db.Query("SELECT filename, video FROM cache")
rows, err := db.Query("SELECT filename, video FROM video")
if err != nil {
lit.Error("Error executing query, %s", err)
return
Expand All @@ -38,14 +41,43 @@ func load() {
}

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

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

for rows.Next() {
var photos []*tele.Photo

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

err = json.Unmarshal(bytes, &photos)
cacheAlbum[filename] = &photos
}
}

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

_, err := db.Exec("INSERT INTO cache (filename, video) VALUES (?, ?)", video.FileName, bytes)
_, err := db.Exec("INSERT INTO video (filename, video) VALUES (?, ?)", video.FileName, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
}
}

func saveAlbum(album *[]*tele.Photo, filename string) {
bytes, _ := json.Marshal(album)

_, err := db.Exec("INSERT INTO album (filename, album) VALUES (?, ?)", filename, bytes)
if err != nil {
lit.Error("Error executing query, %s", err)
return
Expand Down
70 changes: 57 additions & 13 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,69 @@ import (
func videoDownload(c tele.Context) error {
for _, e := range c.Message().Entities {
if e.Type == tele.EntityURL {
url := cleanURL(c.Message().EntityText(e))
link := cleanURL(c.Message().EntityText(e))

if contains(url, cfg.URLs) {
filename, hit := checkAndDownload(url)
if contains(link, cfg.URLs) {
// Use the downloader to get videos and albums from tiktok
if strings.Contains(link, "tiktok.com") {
filename, hit, media := downloadTikTok(link)
if filename != "" {
if media == Video {
err := c.Reply(cacheVideo[filename], tele.Silent)
if err == nil {
if !hit {
go saveVideo(cacheVideo[filename])
continue
}
}
} else {
if _, ok := cacheAlbum[filename]; ok {
var err error

err := c.Reply(cache[filename], tele.Silent)
photos := *cacheAlbum[filename]
album := make(tele.Album, 0, 10)

for i := 0; i < len(photos); i += 10 {
// Add photos to album
for j := 0; j < 10; j++ {
if i+j < len(photos) {
album = append(album, photos[i+j])
}
}

err = c.SendAlbum(album, tele.Silent)
if err != nil {
break
}
}

if err == nil {
if !hit {
go saveAlbum(&photos, filename)
continue
}
}
}
}
}
}

filename, hit := downloadYtDlp(link)

err := c.Reply(cacheVideo[filename], tele.Silent)
if err == nil && !hit {
go save(cache[filename])
go saveVideo(cacheVideo[filename])
}
} else {
// For twitter, we send the same url with only fx appended to it
if strings.HasPrefix(url, "https://twitter.com") {
err := c.Reply(strings.Replace(url, "https://twitter.com", "https://fxtwitter.com", 1), tele.Silent)
if strings.HasPrefix(link, "https://twitter.com") {
err := c.Reply(strings.Replace(link, "https://twitter.com", "https://fxtwitter.com", 1), tele.Silent)
if err != nil {
lit.Error(err.Error())
}
} else {
if strings.HasPrefix(url, "https://x.com") {
err := c.Reply(strings.Replace(url, "https://x.com", "https://fixupx.com", 1), tele.Silent)
if strings.HasPrefix(link, "https://x.com") {
err := c.Reply(strings.Replace(link, "https://x.com", "https://fixupx.com", 1), tele.Silent)
if err != nil {
lit.Error(err.Error())
}
Expand All @@ -48,21 +92,21 @@ func inlineQuery(c tele.Context) error {

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

// Upload video to channel, so we can send it even in inline mode
_, err := c.Bot().Send(tele.ChatID(cfg.Channel), cache[filename])
_, err := c.Bot().Send(tele.ChatID(cfg.Channel), cacheVideo[filename])
if err != nil {
return err
}

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

// Create result
results[0] = &tele.VideoResult{
Cache: cache[filename].FileID,
Cache: cacheVideo[filename].FileID,
Title: "Send video",
MIME: "video/mp4",
}
Expand Down
7 changes: 6 additions & 1 deletion example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ urls:
channel:

# Log level
loglevel: informational
loglevel: informational

# TikTok downloader,
# see https://github.com/Evil0ctal/Douyin_TikTok_Download_API/blob/main/README.en.md#deployment-method-1-linux
# for more information on how to deploy it
downloader: "http://localhost:8080"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/TheTipo01/videoDownloader

go 1.17
go 1.21

require (
github.com/bwmarrin/lit v0.0.0-20190813132558-fd4b44871312
Expand Down
Loading

0 comments on commit a54606f

Please sign in to comment.