Skip to content

Commit f6b2991

Browse files
committed
Add schedule task for main function
It'll be run every 15 seconds
1 parent 18396fe commit f6b2991

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Main.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
_ "github.com/go-sql-driver/mysql"
77
"github.com/gocolly/colly"
8+
"github.com/rakanalh/scheduler"
9+
"github.com/rakanalh/scheduler/storage"
810
"io/ioutil"
911
"log"
1012
"net/http"
@@ -41,14 +43,30 @@ func getRecentLinks() (string, string) {
4143
}
4244

4345
func main() {
44-
links, result := getRecentLinks()
45-
//jdbc:mariadb://192.168.1.65:3306/ideone
4646
dsn := DB_USER + ":" + DB_PASS + "@" + DB_HOST + "/" + DB_NAME + "?charset=utf8"
47+
var db *sql.DB
4748
db, err := sql.Open("mysql", dsn)
4849
checkErr(err)
50+
memStorage := storage.NewMemoryStorage()
51+
s := scheduler.New(memStorage)
52+
fmt.Println("Im good at school three huna worldwide")
53+
if _, err := s.RunEvery(15 * time.Second, scrapIdeone, db); err != nil {
54+
log.Fatal(err)
55+
}
56+
s.Start()
57+
s.Wait()
58+
}
59+
60+
func scrapIdeone(db *sql.DB) {
61+
links, result := getRecentLinks()
62+
//jdbc:mariadb://192.168.1.65:3306/ideone
4963

5064
split := strings.Split(links, ";")
5165
split = split[1:50]
66+
67+
results := strings.Split(result, ";")
68+
results = results[1:50]
69+
5270
for i, k := range split {
5371
fmt.Println("----------")
5472
var url = "https://ideone.com/plain/" + k[1:]
@@ -61,21 +79,13 @@ func main() {
6179
html, err := ioutil.ReadAll(response.Body)
6280
var txt string = string(html)
6381
fmt.Println(len(html))
64-
// fullurl VARCHAR(60),
65-
// codedate VARCHAR(60),
66-
// codekey VARCHAR(30),
67-
// size BIGINT,
68-
// codelines BIGINT,
69-
// language VARCHAR(30),
70-
// status VARCHAR(30),
71-
// txt LONGTEXT,
7282
stmt, err := db.Prepare("INSERT INTO IE (fullurl, codedate, codekey, size, codelines, language, " +
7383
"status, txt) VALUES (?,?,?,?,?,?,?,?)")
7484
checkErr(err)
7585

7686
currentTime := time.Now()
7787
res, err := stmt.Exec(url, currentTime.Format("2006-01-02 15:04:05"), k[1:], len(html),
78-
strings.Count(txt, "\n"), "language", result[i], txt)
88+
strings.Count(txt, "\n"), "language", results[i], txt)
7989
if res == nil {
8090

8191
}
@@ -85,7 +95,6 @@ func main() {
8595
fmt.Println(result[i], ", ", url)
8696
fmt.Println("____")
8797
}
88-
8998
}
9099

91100
func checkErr(err error) {

0 commit comments

Comments
 (0)