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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| package main
import ( "context" "encoding/base64" "flag" "fmt" "io/ioutil" "log" "net/http" "time"
"github.com/chromedp/chromedp" "github.com/gorilla/websocket" )
var addr = flag.String("addr", "localhost:9999", "http service address")
var upgrader = websocket.Upgrader{} // use default options
// 用来记录所有的客户端连接 var ConnMap map[string]*websocket.Conn
var MAX_WATCH_TIME = 500
const DATE_TIME_FORMAT = "2006-01-02 15:04:05" const DATE_FORMAT = "2006-01-02" const DATE_FORMAT_NUM = "20060102"
var CH_SC = make(chan []byte)
func echo(w http.ResponseWriter, r *http.Request) { // 允许跨域 upgrader.CheckOrigin = func(r *http.Request) bool { return true } c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close()
// 新连接加入map ConnMap[c.RemoteAddr().String()] = c
for { mt, message, err := c.ReadMessage() if err != nil { log.Println("read:", err) break } log.Printf("recv: %s", message) err = c.WriteMessage(mt, message) if err != nil { log.Println("write:", err) break } } }
func main() { flag.Parse() log.SetFlags(0) begin := time.Now() ConnMap = make(map[string]*websocket.Conn)
// 启动 chrome 并开启截屏进程 go func() { opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", true), chromedp.Flag("disable-gpu", false), chromedp.Flag("enable-automation", false), chromedp.Flag("disable-extensions", true), )
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) defer cancel()
// create context ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf)) defer cancel()
urlstr := `https://youku.com/` // 需要截图的元素,支持CSS selector以及XPath query selector := `#m_15319` //`#m_2556` //
if err := chromedp.Run(ctx, // emulate iPhone 7 landscape // chromedp.Emulate(device.IPhone7landscape), chromedp.Navigate(urlstr), chromedp.EmulateViewport(1200, 0, chromedp.EmulateScale(1)), ); err != nil { log.Fatal(err) }
var buf []byte for { buf = []byte{} if err := chromedp.Run(ctx, elementScreenshot(urlstr, selector, &buf)); err != nil { log.Fatal(err) } // 发送到全局channel CH_SC <- buf fmt.Println(int64(time.Since(begin).Seconds()), MAX_WATCH_TIME) // if int64(time.Since(begin).Seconds()) > int64(MAX_WATCH_TIME) { // return // } } }()
// 启动发送消息通道 go func() { for { select { case task := <-CH_SC: //task.Process() fmt.Println(time.Now().Format(DATE_TIME_FORMAT)) sendPictureData(task) // 写入文件 if err := ioutil.WriteFile("youku_b.png", task, 0644); err != nil { log.Fatal(err) } } } }()
http.HandleFunc("/echo", echo) log.Fatal(http.ListenAndServe(*addr, nil))
}
// 发送二进制图片数据 func sendPictureData(picture []byte) { // img, _ := png.Decode(bytes.NewReader(picture)) // resizeImg := resize.Resize(hight, 0, img, resize.Lanczos3) //base64压缩 sourcestring := base64.StdEncoding.EncodeToString(picture) // err := c.WriteMessage(websocket.BinaryMessage, []byte(sourcestring)) for _, c := range ConnMap { err := c.WriteMessage(websocket.TextMessage, []byte(sourcestring)) if err != nil { log.Println("write:", err) } } }
// 截图方法 func elementScreenshot(urlstr, sel string, res *[]byte) chromedp.Tasks { // ssOpts := append() return chromedp.Tasks{ // chromedp.Navigate(urlstr), chromedp.WaitVisible(sel, chromedp.ByID), //chromedp.Sleep(time.Duration(3) * time.Second), // 执行截图 chromedp.Screenshot(sel, res, chromedp.NodeVisible, chromedp.ByID), } }
|