-
Notifications
You must be signed in to change notification settings - Fork 13
/
archiveOrg.go
88 lines (70 loc) · 2.03 KB
/
archiveOrg.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
// archiver - archiveOrg
// 2020-11-08 16:21
// Benny <benny.think@gmail.com>
package main
// always keep standard library and 3rd separated
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"time"
)
import (
log "github.com/sirupsen/logrus"
)
// DO NOT involve any telegram bot objects here, such as `b`, `*tb.Message`
type archiveOrg struct{}
func (a archiveOrg) submit(userUrl string) (html string, err error) {
log.Infof("Requesting to archive.org %s", userUrl)
var body = url.Values{}
body.Set("url", userUrl)
body.Set("capture_all", "on")
resp, err := http.PostForm(saveUrl, body)
if err != nil || resp.StatusCode != http.StatusOK {
log.Errorf("Request to archive failed! %v", err)
return "", err
}
tmp, _ := io.ReadAll(resp.Body)
html = string(tmp)
_ = resp.Body.Close()
log.Debugln("Requesting to archive.org has completed successfully.")
return html, err
}
func (a archiveOrg) analysis(html string) (unique string, err error) {
log.Debugln("Doing some analysis job....extracting unique UUID...")
re := regexp.MustCompile(`spn\.watchJob\("(.+?)"`)
result := re.FindStringSubmatch(html)
if len(result) != 2 {
err = errors.New(fmt.Sprintf("regex result is not equal to 2, %v", result))
log.Errorf("Extract UUID failed! %v", err)
return "", err
}
log.Debugln("Extraction success.")
return result[1], nil
}
func (a archiveOrg) status(uuid string) (message string, err error) {
reqUrl := fmt.Sprintf("%s%s?_t=%d", statusUrl, uuid, time.Now().Unix())
resp, err := http.Get(reqUrl)
if err != nil {
return
}
var current status
_ = json.NewDecoder(resp.Body).Decode(¤t)
if current.Status == "success" {
message = fmt.Sprintf(
`✅ %s, %.2fs. Click [here](%s) to visit your snapshot.`,
current.Status,
current.Duration,
"https://web.archive.org/web/"+current.Timestamp+"/"+current.OriginalUrl,
)
log.Infof("✅ %s", current.OriginalUrl)
} else {
log.Infof("⌛️ %s - %s is %s", current.OriginalUrl, time.Now(), current.Status)
}
_ = resp.Body.Close()
return
}