-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic.go
More file actions
118 lines (93 loc) · 2.75 KB
/
traffic.go
File metadata and controls
118 lines (93 loc) · 2.75 KB
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
package main
import (
"bytes"
"fmt"
"image"
"os"
"os/exec"
"strconv"
"time"
"context"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
"github.com/pkg/errors"
"github.com/tebeka/selenium"
)
// TrafficScreenshot returns a screenshot of a map of the current U.S. traffic conditions.
func TrafficScreenshot() ([]byte, error) {
var err error
var webSession selenium.WebDriver
cmdCtx, cmdCancel := context.WithCancel(context.Background())
defer cmdCancel()
port := "4444"
cmd := exec.CommandContext(cmdCtx, "phantomjs", "--webdriver=localhost:"+port)
err = cmd.Start()
if err != nil {
return nil, errors.Wrap(err, "could not launch phantomjs")
}
time.Sleep(time.Second)
// Connect to the WebDriver instance running locally.
caps := selenium.Capabilities{}
for webSession == nil {
time.Sleep(time.Second)
webSession, err = selenium.NewRemote(caps, "http://localhost:"+port)
if err != nil {
fmt.Println("error launching webdriver:", err)
}
}
wh, _ := webSession.CurrentWindowHandle()
webSession.ResizeWindow(wh, 2000, 1500)
url := "https://www.google.com/maps/@43.2927733,-96.7809004,5z/data=!5m1!1e1"
if err := webSession.Get(url); err != nil {
return nil, err
}
// Give the page time to finish loading
time.Sleep(10 * time.Second)
img, err := webSession.Screenshot()
if err != nil {
return nil, err
}
return img, nil
}
// PostProcess post-processes the screenshot image to prepare it to be used as a video frame.
func PostProcess(img image.Image, t time.Time) image.Image {
// Create a 720p output image
out := image.NewRGBA(image.Rect(0, 0, 1280, 720))
gc := draw2dimg.NewGraphicContext(out)
// Scale the image to fit the frame. Although the screenshot could have been generated
// directly in the target size, doing so would leave unwanted UI artifacts in it.
gc.Translate(-270, -450)
gc.Scale(0.9, 0.9)
gc.DrawImage(img)
gc.SetMatrixTransform(draw2d.NewIdentityMatrix())
gc.SetFontData(draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold})
gc.SetFillColor(image.Black)
gc.SetFontSize(14)
gc.FillStringAt(t.String(), 900, 50)
return out
}
func main() {
os.Mkdir("frames", 0755)
frameNumber := 0
for {
imgData, err := TrafficScreenshot()
if err != nil {
fmt.Println("Error creating screenshot: ", err)
continue
}
img, _, err := image.Decode(bytes.NewBuffer(imgData))
if err != nil {
fmt.Println("Error decoding screenshot: ", err)
continue
}
frame := PostProcess(img, time.Now().Round(time.Second))
frameNumber++
err = draw2dimg.SaveToPngFile("frames/"+strconv.Itoa(frameNumber)+".png", frame)
if err != nil {
fmt.Println("Error saving frame: ", err)
continue
}
// Pause before the next snapshot
time.Sleep(time.Minute)
}
}