-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.go
139 lines (128 loc) · 3.97 KB
/
misc.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
139
/*
* BSD-3-Clause
* Copyright 2020 sot (PR_713, C_rho_272)
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
package TtKVC
import (
"bytes"
"errors"
"github.com/op/go-logging"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
"text/template"
)
var Version = "0"
const (
pVersion = "version"
pIndex = "index"
pId = "id"
pName = "name"
pWatch = "watch"
pAdmin = "admin"
pFilesPending = "files"
pVideoUrl = "videourl"
pIgnore = "ignorecmd"
pMeta = "meta"
pTags = "tags"
fReplace = "replace"
tCmdSwitchIgnore = "/switchignore"
tCmdForceUpload = "/forceupload"
)
var logger = logging.MustGetLogger("observer")
var nonLetterNumberSpaceRegexp = regexp.MustCompile(`(?m)[^\p{L}\p{N}_\s]`)
var nonEmptyRegexp = regexp.MustCompile("^$")
var allSpacesRegexp = regexp.MustCompile(`(?m)\s`)
func isEmpty(s string) bool {
return len(s) == 0
}
func checkResponse(resp *http.Response, httpErr error) bool {
return httpErr == nil && resp != nil && resp.StatusCode < 400
}
func responseError(resp *http.Response, httpErr error) error {
var err error
if httpErr != nil {
err = httpErr
} else {
errMsg := "http: "
if resp == nil {
errMsg += "empty response"
} else {
errMsg += resp.Status
}
err = errors.New(errMsg)
}
return err
}
func formatMessage(tmpl *template.Template, values map[string]interface{}) (string, error) {
var err error
var res string
if tmpl != nil {
buf := bytes.Buffer{}
values[fReplace] = strings.ReplaceAll
if err = tmpl.Execute(&buf, values); err == nil {
res = buf.String()
}
} else {
err = errors.New("template not inited")
}
return res, err
}
func downloadToDirectory(path, url, ext string) (string, error) {
var err error
var tmpFileName string
var tmpFile *os.File
if tmpFile, err = ioutil.TempFile(path, "*."+ext); err == nil {
tmpFileName = tmpFile.Name()
if resp, httpErr := http.Get(url); checkResponse(resp, httpErr) {
defer resp.Body.Close()
if _, err = io.Copy(tmpFile, resp.Body); err == nil{
err = tmpFile.Sync()
}
} else {
err = responseError(resp, httpErr)
}
if fErr := tmpFile.Close(); fErr != nil{
logger.Error(err)
}
}
return tmpFileName, err
}
func formatHashTags(commaSeparatedWords string) string {
sb := strings.Builder{}
if !isEmpty(commaSeparatedWords) {
for _, e := range strings.Split(commaSeparatedWords, ",") {
e = strings.TrimSpace(e)
if !isEmpty(e) {
sb.WriteRune('#')
sb.WriteString(e)
sb.WriteRune(' ')
}
}
}
return sb.String()
}