-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
executable file
·138 lines (120 loc) · 3.76 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
_ "github.com/mattn/go-sqlite3"
)
func main() {
// This will set up a tick to trigger every hour
for range time.Tick(1 * time.Hour) {
scrapeEvents()
}
}
func notify(title string, category string, location string,
price string, eventDate string, buyTicketsURL string, imageUrl string) {
// Structure the data
message := fmt.Sprintf("#%s\n\n Event: %s\n\nLocation: %s\nPrice: %s\nEvent Date: %s\n\n<a href=\"%s\">Buy Tickets</a>",
category, title, location, price, eventDate, buyTicketsURL)
data := map[string]string{
"chat_id": os.Getenv("TG_CHAT_ID"), // Assuming chatID is defined somewhere in your code
"photo": imageUrl,
"caption": message,
"parse_mode": "HTML",
}
// Convert the data to JSON
jsonData, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
// Send the JSON data as a POST request
apiEndpoint := "https://api.telegram.org/bot" + os.Getenv("TG_BOT_TOKEN") + "/sendPhoto" // Replace with your API endpoint
_, err = http.Post(apiEndpoint, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
log.Fatal(err)
}
log.Printf("Event sent: %s\n", title)
}
func scrapeEvents() {
// Open or create an SQLite database file
db, err := sql.Open("sqlite3", os.Getenv("SQLITE_PATH"))
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a table to store the scraped data
createTableSQL := `
CREATE TABLE IF NOT EXISTS events (
title TEXT,
category TEXT,
location TEXT,
price TEXT,
event_date TEXT,
buy_tickets_url TEXT,
PRIMARY KEY (title, category, location, price, event_date, buy_tickets_url)
);
`
_, err = db.Exec(createTableSQL)
if err != nil {
log.Fatal(err)
}
// URL of the web page to scrape
baseUrl := "https://www.soldoutticketbox.com"
homeEn := baseUrl + "/en/home"
// Make an HTTP GET request to the URL
response, err := http.Get(homeEn)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
// Parse the HTML document
document, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
log.Fatal(err)
}
// Find all elements with class "homeBoxEvent"
document.Find("div.homeBoxEvent").Each(func(index int, element *goquery.Selection) {
// Extract data from each "homeBoxEvent" div
title := element.Find("h2 a").Text()
category := element.Find("a.h3Style").First().Text()
location := element.Find("p.blackSmall span").First().Text()
price := element.Find("p.blackSmall span").Last().Text()
imgRelUrl, _ := element.Find("a img").Attr("src")
imgUrl := fmt.Sprintf("%s/%s", baseUrl, imgRelUrl)
// Extract event date and clean up the text
eventDate := element.Find("p.blackSmall span").Eq(1).Text()
eventDate = strings.TrimSpace(strings.Split(eventDate, "(")[0])
eventDate = strings.ReplaceAll(eventDate, " -", "-")
// Use a regular expression to replace multiple spaces with a single space
re := regexp.MustCompile(`\s+`)
eventDate = re.ReplaceAllString(eventDate, " ")
buyTicketsURL, _ := element.Find("a.buyTicket").Attr("href")
buyTicketsURL = baseUrl + buyTicketsURL
// Insert the scraped data into the SQLite database
insertDataSQL := `
INSERT OR IGNORE INTO events (title, category, location, price, event_date, buy_tickets_url)
VALUES (?, ?, ?, ?, ?, ?)
`
result, err := db.Exec(insertDataSQL, title, category, location, price, eventDate, buyTicketsURL)
if err != nil {
log.Fatal(err)
} else {
rowsAffected, err := result.RowsAffected()
if err != nil {
log.Fatal(err)
} else if rowsAffected > 0 {
notify(title, category, location, price, eventDate, buyTicketsURL, imgUrl)
} else if rowsAffected == 0 {
log.Printf("No new events found")
}
}
})
}