Ajoute /cite, met à jour la CI et répare les liens de vidéo
This commit is contained in:
53
bot.go
53
bot.go
@@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -21,27 +21,27 @@ const (
|
||||
// Quote represent a quote of the flim
|
||||
type Quote struct {
|
||||
Quote string `json:"quote"`
|
||||
YoutubeKey string `json:"youtube_key"`
|
||||
}
|
||||
|
||||
// ThumbURL get url of the thumbnail
|
||||
func (q *Quote) ThumbURL() string {
|
||||
return fmt.Sprintf(
|
||||
"https://img.youtube.com/vi/%s/0.jpg",
|
||||
q.YoutubeKey,
|
||||
)
|
||||
|
||||
Page string `json:"page"`
|
||||
ThumbURL string `json:"thumb_url"`
|
||||
}
|
||||
|
||||
// VideoURL get url of the video
|
||||
func (q *Quote) VideoURL() string {
|
||||
return fmt.Sprintf(
|
||||
"https://www.youtube.com/watch?v=%s",
|
||||
q.YoutubeKey,
|
||||
"https://george-abitbol.fr/%s",
|
||||
q.Page,
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args
|
||||
|
||||
if len(args) > 1 && strings.Compare(args[1], "--update") == 0 {
|
||||
fmt.Println("Updating quotes")
|
||||
Update()
|
||||
return
|
||||
}
|
||||
|
||||
var quotes []Quote
|
||||
|
||||
// Load quote database
|
||||
@@ -52,7 +52,7 @@ func main() {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
bytes, err := ioutil.ReadAll(file)
|
||||
bytes, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read quote file: %s\n", err)
|
||||
return
|
||||
@@ -76,21 +76,33 @@ func main() {
|
||||
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Colud not connect to telegram: %s\n", err)
|
||||
log.Fatalf("Could not connect to telegram: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Add bot capabilities
|
||||
// Cite command
|
||||
b.Handle("/cite", func(m *tb.Message) {
|
||||
quotes, err := index.Search(m.Text)
|
||||
if err != nil || len(quotes) == 0{
|
||||
b.Send(m.Chat, "Pas de citation trouvée")
|
||||
return
|
||||
}
|
||||
|
||||
b.Send(m.Chat, fmt.Sprintf("%s\n\n%s", quotes[0].Quote, quotes[0].VideoURL()))
|
||||
})
|
||||
|
||||
// Search feature
|
||||
b.Handle(tb.OnQuery, func(q *tb.Query) {
|
||||
quotes, err := index.Search(q.Text)
|
||||
if err != nil {
|
||||
log.Println("Pas de fichier citation trouvé")
|
||||
log.Println("Pas de fichier de citations trouvé")
|
||||
return
|
||||
}
|
||||
results := make(tb.Results, len(quotes))
|
||||
for i, quote := range quotes {
|
||||
url := quote.VideoURL()
|
||||
thumb := quote.ThumbURL()
|
||||
thumb := quote.ThumbURL
|
||||
log.Printf("{%s, %s}\n", url, thumb)
|
||||
results[i] = &tb.VideoResult{
|
||||
URL: url,
|
||||
@@ -136,12 +148,13 @@ func Update() error {
|
||||
)
|
||||
|
||||
// Find and visit all links
|
||||
c.OnHTML("img[data-original]", func(e *colly.HTMLElement) {
|
||||
c.OnHTML("div.thumb", func(e *colly.HTMLElement) {
|
||||
videos = append(
|
||||
videos,
|
||||
Quote{
|
||||
Quote: e.Attr("alt"),
|
||||
YoutubeKey: strings.Split(e.Attr("data-original"), "/")[4],
|
||||
Quote: e.ChildAttr("img", "alt"),
|
||||
ThumbURL: e.ChildAttr("img", "data-original"),
|
||||
Page: e.ChildAttr("a", "href"),
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user