Verified Commit 2ba5b8b6 authored by Marcel's avatar Marcel

Change code to use a proper file structure and to use proper commandline flags

parent 2347fc9b
result/
tmp/
log
luftdatenImage*
luftdaten_screenshot_tool*
.idea
\ No newline at end of file
......@@ -10,8 +10,8 @@
## Benutzung
Die Benutzung ist simpel. Das Programm (bianry heisst `luftdaten_screenshot_tool`) öffnet chrome und macht einen screenshot in den ordner result mit aufsteigender Nummerierung bei jedem Start. Das ganze am besten in einen Cronjob machen und am ende dann zu ein gif umwandeln.
Mithilfe der option `-output=` kann zudem ein ausgabe ordner definiert werden.
Die Option `-headless` unterbindet das sichtbare öffnen des Browsers
Mithilfe der option `--output=` kann zudem ein ausgabe ordner definiert werden.
Die Option `--headless` unterbindet das sichtbare öffnen des Browsers
## GIF mit ffmpeg generieren
......
package actions
import (
"context"
"fmt"
"github.com/chromedp/cdproto/cdp"
cdr "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"io/ioutil"
"os"
"time"
)
func Screenshot(urlstr, folderPath string) chromedp.Tasks {
var buf []byte
var res *cdr.RemoteObject
currentTime := time.Now().Local()
var (
addTimeandDateScript = "" +
"var element = document.createElement('div');" +
"element.innerHTML = 'Current Time: " + currentTime.Format("02.01.2006 15:04:05") + "';" +
"element.style.position = 'absolute';" +
"element.style.top = '0';" +
"element.style.right = '0';" +
"element.style.zIndex = '500';" +
"element.style.fontSize = '25px';" +
"element.style.fontWeight = 'bold';" +
"element.className = 'timeDate';" +
"document.getElementsByClassName('map')[0].appendChild(element);"
)
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(2 * time.Second),
chromedp.WaitVisible(`.map`, chromedp.ByQuery),
chromedp.EvaluateAsDevTools(addTimeandDateScript, &res),
chromedp.WaitVisible(`.timeDate`, chromedp.ByQuery),
chromedp.Screenshot(`.map`, &buf, chromedp.ByQuery),
chromedp.ActionFunc(func(context.Context, cdp.Executor) error {
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.Mkdir(folderPath, os.ModePerm)
}
files, _ := ioutil.ReadDir(folderPath)
count := len(files) + 1
filename := fmt.Sprintf("%s/map%03d.png", folderPath, count)
return ioutil.WriteFile(filename, buf, 0644)
}),
}
}
package cmd
import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
"github.com/spf13/cobra"
"gitlab.com/iotssl/luftdaten_screenshot_tool/actions"
"log"
"os"
"runtime"
)
var FolderPath string
var Headless bool
func init() {
rootCmd.Flags().StringVarP(&FolderPath, "output", "o", "./result", "Folder where the images should go to")
rootCmd.Flags().BoolVarP(&Headless, "headless", "", false, "Use Headless chrome")
}
var rootCmd = &cobra.Command{
Use: "luftdaten_screenshot_tool",
Short: "LuftdatenImage is a simple luftdaten.info web screenshoter",
Long: `A simple headless screenshot Tool for luftdaten.info build with golang and Google Chrome`,
Run: Run,
}
func Run(cmd *cobra.Command, args []string) {
var err error
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
var opts chromedp.Option
if Headless {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.Port(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.Port(9222),
)
}
} else {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.Port(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.Port(9222),
)
}
}
c, err := chromedp.New(ctxt, opts)
if err != nil {
log.Fatal(err)
}
// run task list
err = c.Run(ctxt, actions.Screenshot(`http://deutschland.maps.luftdaten.info/`, FolderPath))
if err != nil {
log.Fatal(err)
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
}
log.Println("waiting for headless_shell to exit...")
// wait for chrome to finish
// TODO: Hope that Wait can be used again in the future
/*err = c.Wait()
if err != nil {
log.Fatal(err)
}*/
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
package main
import (
"context"
"io/ioutil"
"log"
"time"
"flag"
"fmt"
"github.com/chromedp/cdproto/cdp"
cdr "github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/runner"
"os"
"runtime"
"gitlab.com/iotssl/luftdaten_screenshot_tool/cmd"
)
func main() {
var folderPath string
var headless bool
flag.StringVar(&folderPath, "output", "./result", "Folder where the images should go to")
flag.BoolVar(&headless, "headless", false, "Use Headless chrome")
flag.Parse()
var err error
err = os.Remove("log")
f, err := os.OpenFile("log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
// create context
ctxt, cancel := context.WithCancel(context.Background())
defer cancel()
// create chrome instance
var opts chromedp.Option
if headless {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.Port(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-sandbox", true),
runner.Flag("headless", true),
runner.Flag("disable-gpu", true),
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.Port(9222),
)
}
} else {
if runtime.GOOS == "windows" {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.UserDataDir("./tmp"),
runner.Port(9222),
)
} else {
opts = chromedp.WithRunnerOptions(
runner.Flag("no-first-run", true),
runner.Flag("no-default-browser-check", true),
runner.WindowSize(1920, 1080),
runner.Port(9222),
)
}
}
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf), opts)
if err != nil {
log.Fatal(err)
}
// run task list
err = c.Run(ctxt, screenshot(`http://deutschland.maps.luftdaten.info/`, folderPath))
if err != nil {
log.Fatal(err)
}
// shutdown chrome
err = c.Shutdown(ctxt)
if err != nil {
log.Fatal(err)
}
log.Println("waiting for headless_shell to exit...")
// wait for chrome to finish
//TODO: Hope that Wait can be used again in the future
/*err = c.Wait()
if err != nil {
log.Fatal(err)
}*/
}
func screenshot(urlstr, folderPath string) chromedp.Tasks {
var buf []byte
var res *cdr.RemoteObject
currentTime := time.Now().Local()
var (
addTimeandDateScript = "" +
"var element = document.createElement('div');" +
"element.innerHTML = 'Current Time: " + currentTime.Format("02.01.2006 15:04:05") + "';" +
"element.style.position = 'absolute';" +
"element.style.top = '0';" +
"element.style.right = '0';" +
"element.style.zIndex = '500';" +
"element.style.fontSize = '25px';" +
"element.style.fontWeight = 'bold';" +
"element.className = 'timeDate';" +
"document.getElementsByClassName('map')[0].appendChild(element);"
)
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(2 * time.Second),
chromedp.WaitVisible(`.map`, chromedp.ByQuery),
chromedp.EvaluateAsDevTools(addTimeandDateScript, &res),
chromedp.WaitVisible(`.timeDate`, chromedp.ByQuery),
chromedp.Screenshot(`.map`, &buf, chromedp.ByQuery),
chromedp.ActionFunc(func(context.Context, cdp.Executor) error {
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.Mkdir(folderPath, os.ModePerm)
}
files, _ := ioutil.ReadDir(folderPath)
count := len(files) + 1
filename := fmt.Sprintf("%s/map%03d.png", folderPath, count)
return ioutil.WriteFile(filename, buf, 0644)
}),
}
cmd.Execute()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment