util.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // util
  2. package spiderutil
  3. import (
  4. "crypto/md5"
  5. "encoding/base64"
  6. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "math/rand"
  10. "net/http"
  11. "os"
  12. "qfw/util"
  13. "time"
  14. "github.com/yuin/gopher-lua"
  15. )
  16. //time.AfterFunc 加锁
  17. func TimeAfterFunc(td time.Duration, f func(), ch chan bool) {
  18. ch <- true
  19. time.Sleep(10 * time.Millisecond)
  20. <-ch
  21. time.AfterFunc(td, func() {
  22. f()
  23. })
  24. }
  25. //time.AfterFunc 加锁
  26. func TimeSleepFunc(td time.Duration, ch chan bool) {
  27. ch <- true
  28. time.Sleep(10 * time.Millisecond)
  29. <-ch
  30. time.Sleep(td)
  31. }
  32. func ParseHttpCookie(obj interface{}) (ret []*http.Cookie) {
  33. if arr, ok := obj.([]interface{}); ok {
  34. for _, v := range arr {
  35. item := v.(map[string]interface{})
  36. maxAge := 0
  37. if item["MaxAge"] != nil {
  38. maxAge = int(item["MaxAge"].(float64))
  39. }
  40. secure := false
  41. if item["Secure"] != nil {
  42. secure = item["Secure"].(bool)
  43. }
  44. httpOnly := false
  45. if item["HttpOnly"] != nil {
  46. httpOnly = item["HttpOnly"].(bool)
  47. }
  48. exp := item["Expires"].(string)
  49. expt, _ := time.Parse("2006-01-02T15:04:05Z", exp)
  50. ret = append(ret, &http.Cookie{
  51. Value: item["Value"].(string),
  52. Name: item["Name"].(string),
  53. Domain: item["Domain"].(string),
  54. Path: item["Path"].(string),
  55. Expires: expt,
  56. MaxAge: maxAge,
  57. Secure: secure,
  58. HttpOnly: httpOnly,
  59. })
  60. }
  61. }
  62. return
  63. }
  64. func TableToMap(tab *lua.LTable) map[string]interface{} {
  65. tmp := make(map[string]interface{})
  66. tab.ForEach(func(k, v lua.LValue) {
  67. key := fmt.Sprint(k)
  68. if val, ok := v.(*lua.LTable); ok {
  69. tmp[key] = TableToMap(val)
  70. } else {
  71. if val, ok := v.(lua.LString); ok {
  72. tmp[key] = string(val)
  73. } else if val, ok := v.(lua.LNumber); ok {
  74. tmp[key] = int64(val)
  75. } else if val, ok := v.(lua.LBool); ok {
  76. tmp[key] = bool(val)
  77. } else {
  78. tmp[key] = fmt.Sprint(v)
  79. }
  80. }
  81. })
  82. return tmp
  83. }
  84. func MapToTable(l *lua.LState, obj []interface{}) *lua.LTable {
  85. listtb := l.NewTable()
  86. for i := 0; i < len(obj); i++ {
  87. tb := l.NewTable()
  88. if tmp, ok := obj[i].(map[string]string); ok {
  89. for k, v := range tmp {
  90. tb.RawSet(lua.LString(k), lua.LString(v))
  91. }
  92. listtb.Insert((i + 1), tb)
  93. }
  94. }
  95. return listtb
  96. }
  97. func MapToLuaTable(l *lua.LState, obj map[string]interface{}) *lua.LTable {
  98. tab := l.NewTable()
  99. for k, v := range obj {
  100. if val, ok := v.(string); ok {
  101. tab.RawSet(lua.LString(k), lua.LString(val))
  102. } else if val, ok := v.(int64); ok {
  103. tab.RawSet(lua.LString(k), lua.LNumber(val))
  104. } else if val, ok := v.(int32); ok {
  105. tab.RawSet(lua.LString(k), lua.LNumber(val))
  106. } else if val, ok := v.(float64); ok {
  107. tab.RawSet(lua.LString(k), lua.LNumber(val))
  108. } else if val, ok := v.(float32); ok {
  109. tab.RawSet(lua.LString(k), lua.LNumber(val))
  110. } else if val, ok := v.(bool); ok {
  111. tab.RawSet(lua.LString(k), lua.LBool(val))
  112. } else if val, ok := v.(map[string]interface{}); ok {
  113. tab.RawSet(lua.LString(k), MapToLuaTable(l, val))
  114. } else if val, ok := v.([]interface{}); ok {
  115. bs, _ := json.Marshal(val)
  116. tab.RawSet(lua.LString(k), lua.LString(string(bs)))
  117. }
  118. }
  119. return tab
  120. }
  121. func GetTable(param *lua.LTable) map[string]interface{} {
  122. tmp := map[string]interface{}{}
  123. param.ForEach(func(key, val lua.LValue) {
  124. k := fmt.Sprint(key)
  125. if v, ok := val.(lua.LString); ok {
  126. tmp[k] = string(v)
  127. } else if v, ok := val.(*lua.LTable); ok {
  128. tmp[k] = GetTable(v)
  129. } else {
  130. tmp[k] = val
  131. }
  132. })
  133. return tmp
  134. }
  135. func GetTableEx(param *lua.LTable) map[string]interface{} {
  136. rep := make(map[string]interface{})
  137. param.ForEach(func(key, val lua.LValue) {
  138. rep[key.String()] = val
  139. })
  140. return rep
  141. }
  142. func ParseDate2Int64(str string) int64 {
  143. t, err := time.ParseInLocation("2006-01-02 15:04:05", str, time.Local)
  144. if err != nil {
  145. return time.Now().Unix()
  146. } else {
  147. return t.Unix()
  148. }
  149. }
  150. func EncodeB64(message string) (retour string) {
  151. base64Text := make([]byte, base64.StdEncoding.EncodedLen(len(message)))
  152. base64.StdEncoding.Encode(base64Text, []byte(message))
  153. return string(base64Text)
  154. }
  155. func DecodeB64(message string) (retour string) {
  156. base64Text := make([]byte, base64.StdEncoding.DecodedLen(len(message)))
  157. base64.StdEncoding.Decode(base64Text, []byte(message))
  158. return string(base64Text)
  159. }
  160. //获取随机数
  161. func GetRandMath(num int) int {
  162. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  163. return r.Intn(num)
  164. }
  165. //判断当前时间是否是工作时间,工作时间周一至周五早7点至晚7点
  166. func IsWorkTime() bool {
  167. tt := time.Now()
  168. //"Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday"
  169. if tt.Weekday().String() == "Saturday" || tt.Weekday().String() == "Sunday" {
  170. return false
  171. } else {
  172. if tt.Hour() > 7 && tt.Hour() < 19 {
  173. return true
  174. } else {
  175. return false
  176. }
  177. }
  178. }
  179. //生成文件
  180. func CreateFile(code, script string) (string, error) {
  181. filepath := "res/" + time.Now().Format("2006/01/02")
  182. err := os.MkdirAll(filepath, 0777)
  183. filepath = filepath + "/spider_" + code + ".lua"
  184. f, err := os.Create(filepath)
  185. defer f.Close()
  186. if err == nil {
  187. f.WriteString(script)
  188. return filepath, nil
  189. } else {
  190. return "", err
  191. }
  192. }
  193. func GetMd5String(s string) string {
  194. h := md5.New()
  195. h.Write([]byte(s))
  196. return hex.EncodeToString(h.Sum(nil))
  197. }
  198. var Se = util.SimpleEncrypt{Key: "topnet#2017@editor"}