util.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package spider_com
  2. import (
  3. "crypto/sha256"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "math/big"
  10. "math/rand"
  11. "net/http"
  12. "os"
  13. "regexp"
  14. "time"
  15. "github.com/yuin/gopher-lua"
  16. )
  17. var Reg = regexp.MustCompile("[^0-9A-Za-z\u4e00-\u9fa5]+")
  18. var Filter = regexp.MustCompile("<[^>]*?>|[\\s\u3000\u2003\u00a0]")
  19. //time.AfterFunc 加锁
  20. func TimeAfterFunc(td time.Duration, f func(), ch chan bool) {
  21. ch <- true
  22. time.Sleep(10 * time.Millisecond)
  23. <-ch
  24. time.AfterFunc(td, func() {
  25. f()
  26. })
  27. }
  28. //time.AfterFunc 加锁
  29. func TimeSleepFunc(td time.Duration, ch chan bool) {
  30. ch <- true
  31. time.Sleep(10 * time.Millisecond)
  32. <-ch
  33. time.Sleep(td)
  34. }
  35. func ParseHttpCookie(obj interface{}) (ret []*http.Cookie) {
  36. if arr, ok := obj.([]interface{}); ok {
  37. for _, v := range arr {
  38. item := v.(map[string]interface{})
  39. maxAge := 0
  40. if item["MaxAge"] != nil {
  41. maxAge = int(item["MaxAge"].(float64))
  42. }
  43. secure := false
  44. if item["Secure"] != nil {
  45. secure = item["Secure"].(bool)
  46. }
  47. httpOnly := false
  48. if item["HttpOnly"] != nil {
  49. httpOnly = item["HttpOnly"].(bool)
  50. }
  51. exp := item["Expires"].(string)
  52. expt, _ := time.Parse("2006-01-02T15:04:05Z", exp)
  53. ret = append(ret, &http.Cookie{
  54. Value: item["Value"].(string),
  55. Name: item["Name"].(string),
  56. Domain: item["Domain"].(string),
  57. Path: item["Path"].(string),
  58. Expires: expt,
  59. MaxAge: maxAge,
  60. Secure: secure,
  61. HttpOnly: httpOnly,
  62. })
  63. }
  64. }
  65. return
  66. }
  67. func TableToMap(tab *lua.LTable) map[string]interface{} {
  68. tmp := make(map[string]interface{})
  69. tab.ForEach(func(k, v lua.LValue) {
  70. key := fmt.Sprint(k)
  71. if val, ok := v.(*lua.LTable); ok {
  72. tmp[key] = TableToMap(val)
  73. } else {
  74. if val, ok := v.(lua.LString); ok {
  75. tmp[key] = string(val)
  76. } else if val, ok := v.(lua.LNumber); ok {
  77. tmp[key] = int64(val)
  78. } else if val, ok := v.(lua.LBool); ok {
  79. tmp[key] = bool(val)
  80. } else {
  81. tmp[key] = fmt.Sprint(v)
  82. }
  83. }
  84. })
  85. return tmp
  86. }
  87. func MapToTable(l *lua.LState, obj []interface{}) *lua.LTable {
  88. listtb := l.NewTable()
  89. for i := 0; i < len(obj); i++ {
  90. tb := l.NewTable()
  91. if tmp, ok := obj[i].(map[string]string); ok {
  92. for k, v := range tmp {
  93. tb.RawSet(lua.LString(k), lua.LString(v))
  94. }
  95. listtb.Insert((i + 1), tb)
  96. } else if tmp, ok := obj[i].(string); ok {
  97. listtb.Insert(i, lua.LString(tmp))
  98. }
  99. }
  100. return listtb
  101. }
  102. func MapToLuaTable(l *lua.LState, obj map[string]interface{}) *lua.LTable {
  103. tab := l.NewTable()
  104. for k, v := range obj {
  105. if val, ok := v.(string); ok {
  106. tab.RawSet(lua.LString(k), lua.LString(val))
  107. } else if val, ok := v.(int64); ok {
  108. tab.RawSet(lua.LString(k), lua.LNumber(val))
  109. } else if val, ok := v.(int32); ok {
  110. tab.RawSet(lua.LString(k), lua.LNumber(val))
  111. } else if val, ok := v.(float64); ok {
  112. tab.RawSet(lua.LString(k), lua.LNumber(val))
  113. } else if val, ok := v.(float32); ok {
  114. tab.RawSet(lua.LString(k), lua.LNumber(val))
  115. } else if val, ok := v.(bool); ok {
  116. tab.RawSet(lua.LString(k), lua.LBool(val))
  117. } else if val, ok := v.(map[string]interface{}); ok {
  118. tab.RawSet(lua.LString(k), MapToLuaTable(l, val))
  119. } else if val, ok := v.([]interface{}); ok {
  120. bs, _ := json.Marshal(val)
  121. tab.RawSet(lua.LString(k), lua.LString(string(bs)))
  122. }
  123. }
  124. return tab
  125. }
  126. func GetTable(param *lua.LTable) map[string]interface{} {
  127. tmp := map[string]interface{}{}
  128. param.ForEach(func(key, val lua.LValue) {
  129. k := fmt.Sprint(key)
  130. if v, ok := val.(lua.LString); ok {
  131. tmp[k] = string(v)
  132. } else if v, ok := val.(*lua.LTable); ok {
  133. tmp[k] = GetTable(v)
  134. } else {
  135. tmp[k] = val
  136. }
  137. })
  138. return tmp
  139. }
  140. func GetTableEx(param *lua.LTable) map[string]interface{} {
  141. rep := make(map[string]interface{})
  142. param.ForEach(func(key, val lua.LValue) {
  143. rep[key.String()] = val
  144. })
  145. return rep
  146. }
  147. func ParseDate2Int64(str string) int64 {
  148. t, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  149. if err != nil {
  150. return time.Now().Unix()
  151. } else {
  152. return t.Unix()
  153. }
  154. }
  155. func EncodeB64(message string) (retour string) {
  156. base64Text := make([]byte, base64.StdEncoding.EncodedLen(len(message)))
  157. base64.StdEncoding.Encode(base64Text, []byte(message))
  158. return string(base64Text)
  159. }
  160. func DecodeB64(message string) (retour string) {
  161. base64Text := make([]byte, base64.StdEncoding.DecodedLen(len(message)))
  162. base64.StdEncoding.Decode(base64Text, []byte(message))
  163. return string(base64Text)
  164. }
  165. // 获取随机数
  166. func GetRandMath(num int) int {
  167. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  168. return r.Intn(num)
  169. }
  170. // 对href哈希取模
  171. func HexToBigIntMod(href string) int {
  172. //取哈希值
  173. t := sha256.New()
  174. io.WriteString(t, href)
  175. hex := fmt.Sprintf("%x", t.Sum(nil))
  176. //取模
  177. n := new(big.Int)
  178. n, _ = n.SetString(hex[2:], 16)
  179. return int(n.Mod(n, big.NewInt(16)).Int64())
  180. }
  181. // 求hash
  182. func HexText(href string) string {
  183. h := sha256.New()
  184. h.Write([]byte(href))
  185. return fmt.Sprintf("%x", h.Sum(nil))
  186. }
  187. func HexTextByte(text []byte) string {
  188. h := sha256.New()
  189. h.Write(text)
  190. return fmt.Sprintf("%x", h.Sum(nil))
  191. }
  192. func Sha(con string) string {
  193. h := sha256.New()
  194. con = Reg.ReplaceAllString(Filter.ReplaceAllString(con, ""), "")
  195. h.Write([]byte(con))
  196. return fmt.Sprintf("%x", h.Sum(nil))
  197. }
  198. func FilterDetail(con string) string {
  199. return Reg.ReplaceAllString(Filter.ReplaceAllString(con, ""), "")
  200. }
  201. // 判断当前时间是否是工作时间,工作时间周一至周五早7点至晚7点
  202. func IsWorkTime() bool {
  203. tt := time.Now()
  204. //"Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
  205. if tt.Weekday().String() == "Saturday" || tt.Weekday().String() == "Sunday" {
  206. return false
  207. } else {
  208. if tt.Hour() > 7 && tt.Hour() < 19 {
  209. return true
  210. } else {
  211. return false
  212. }
  213. }
  214. }
  215. // 生成文件
  216. func CreateFile(code, script string) (string, error) {
  217. filepath := "res/" + time.Now().Format("2006/01/02")
  218. err := os.MkdirAll(filepath, 0777)
  219. filepath = filepath + "/spider_" + code + ".lua"
  220. f, err := os.Create(filepath)
  221. defer f.Close()
  222. if err == nil {
  223. f.WriteString(script)
  224. return filepath, nil
  225. } else {
  226. return "", err
  227. }
  228. }
  229. func GetProxyAddr(proxyAddr, roxyAuthor string) string {
  230. //获取代理
  231. req, err := http.NewRequest(http.MethodGet, proxyAddr, nil)
  232. if err != nil {
  233. fmt.Println("get proxy request err:", err)
  234. return ""
  235. }
  236. //添加请求头
  237. req.Header.Add("Authorization", roxyAuthor)
  238. client := http.Client{}
  239. //发送请求
  240. resp, err := client.Do(req)
  241. if err != nil {
  242. fmt.Println("get proxy client err:", err)
  243. return ""
  244. }
  245. defer resp.Body.Close()
  246. bodyByte, err := ioutil.ReadAll(resp.Body)
  247. if err != nil {
  248. fmt.Println("get proxy read body err:", err)
  249. return ""
  250. }
  251. tmp := map[string]interface{}{}
  252. if json.Unmarshal(bodyByte, &tmp) != nil {
  253. return ""
  254. }
  255. if data, ok := tmp["data"].(map[string]interface{}); ok && len(data) > 0 {
  256. if httpProxy, ok := data["http"].(string); ok {
  257. return httpProxy
  258. } else if httpsProxy, ok := data["https"].(string); ok {
  259. return httpsProxy
  260. }
  261. }
  262. return ""
  263. }