-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataCollector.go
More file actions
60 lines (54 loc) · 1.64 KB
/
dataCollector.go
File metadata and controls
60 lines (54 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/csv"
"fmt"
tgbotapi "github.com/OvyFlash/telegram-bot-api"
"log/slog"
"os"
"regexp"
"strconv"
)
func collectMapUrls(message tgbotapi.Message) {
urlPattern := `https://maps\.app\.goo\.gl/[^\s]+`
filename := "maps.csv"
re := regexp.MustCompile(urlPattern)
// Find the first matching URL in the message
fullURL := re.FindString(message.Text)
if fullURL != "" {
slog.Info("Detected new map URL: " + fullURL)
// Check if the file exists
err := createCSVFile(filename)
if err != nil {
return
}
// Open or create the CSV file
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
slog.Error("Failed to open or create file: %w", err)
}
defer file.Close()
writer := csv.NewWriter(file)
err = writer.Write([]string{message.Time().UTC().String(), message.Text, fullURL, message.From.UserName, strconv.Itoa(int(message.From.ID)), strconv.FormatBool(message.Chat.IsPrivate()), "https://t.me/" + message.Chat.UserName + "/" + strconv.Itoa(message.MessageID)})
if err != nil {
slog.Error("Failed to write to file: %w", err)
}
writer.Flush()
}
}
func createCSVFile(filename string) error {
// Check if the file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
// Create the file since it does not exist
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}
defer file.Close()
// Write UTF-8 BOM to the file
bom := []byte{0xEF, 0xBB, 0xBF} // UTF-8 BOM bytes
if _, err := file.Write(bom); err != nil {
return fmt.Errorf("failed to write BOM to file: %w", err)
}
}
return nil
}