<commit_before>package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/mafredri/cdp"
	"github.com/mafredri/cdp/cdpcmd"
	"github.com/mafredri/cdp/devtool"
	"github.com/mafredri/cdp/rpcc"
)

func main() {
	if err := run(); err != nil {
		panic(err)
	}
}

func run() error {
	ctx, cancel := context.WithCancel(context.TODO())
	defer cancel()

	devt := devtool.New("http://localhost:9222")

	page, err := devt.Get(ctx, devtool.Page)
	if err != nil {
		return err
	}

	conn, err := rpcc.DialContext(ctx, page.WebSocketDebuggerURL)
	if err != nil {
		return err
	}

	c := cdp.NewClient(conn)

	err = c.Page.Enable(ctx)
	if err != nil {
		return err
	}

	// Navigate to GitHub, block until ready.
	loadEventFired, err := c.Page.LoadEventFired(ctx)
	if err != nil {
		return err
	}

	_, err = c.Page.Navigate(ctx, cdpcmd.NewPageNavigateArgs("https://github.com"))
	if err != nil {
		return err
	}

	_, err = loadEventFired.Recv()
	if err != nil {
		return err
	}

	screencastFrame, err := c.Page.ScreencastFrame(ctx)
	if err != nil {
		return err
	}

	go func() {
		defer screencastFrame.Close()

		frameN := 0
		for {
			ev, err := screencastFrame.Recv()
			if err != nil {
				log.Printf("Failed to receive ScreencastFrame: %v", err)
				return
			}
			log.Printf("Got frame with sessionID: %d: %+v", ev.SessionID, ev.Metadata)

			err = c.Page.ScreencastFrameAck(ctx, cdpcmd.NewPageScreencastFrameAckArgs(ev.SessionID))
			if err != nil {
				log.Printf("Failed to ack ScreencastFrame: %v", err)
				return
			}

			// Write to screencast_frame-00000N.png.
			name := fmt.Sprintf("screencast_frame-%06d.png", frameN)
			frameN++

			// Write the frame to file (without blocking).
			go func() {
				err = ioutil.WriteFile(name, ev.Data, 0644)
				if err != nil {
					log.Printf("Failed to write ScreencastFrame to %q: %v", name, err)
				}
			}()
		}
	}()

	screencastArgs := cdpcmd.NewPageStartScreencastArgs().SetEveryNthFrame(1).SetFormat("png")
	err = c.Page.StartScreencast(ctx, screencastArgs)
	if err != nil {
		return err
	}

	// Random delay for our screencast.
	time.Sleep(30 * time.Second)

	err = c.Page.StopScreencast(ctx)
	if err != nil {
		return err
	}

	return nil
}<commit_msg>example(screencast): Use timestamp for frames<commit_after>package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/mafredri/cdp"
	"github.com/mafredri/cdp/cdpcmd"
	"github.com/mafredri/cdp/devtool"
	"github.com/mafredri/cdp/rpcc"
)

func main() {
	if err := run(); err != nil {
		panic(err)
	}
}

func run() error {
	ctx, cancel := context.WithCancel(context.TODO())
	defer cancel()

	devt := devtool.New("http://localhost:9222")

	page, err := devt.Get(ctx, devtool.Page)
	if err != nil {
		return err
	}

	conn, err := rpcc.DialContext(ctx, page.WebSocketDebuggerURL)
	if err != nil {
		return err
	}

	c := cdp.NewClient(conn)

	err = c.Page.Enable(ctx)
	if err != nil {
		return err
	}

	// Navigate to GitHub, block until ready.
	loadEventFired, err := c.Page.LoadEventFired(ctx)
	if err != nil {
		return err
	}

	_, err = c.Page.Navigate(ctx, cdpcmd.NewPageNavigateArgs("https://github.com"))
	if err != nil {
		return err
	}

	_, err = loadEventFired.Recv()
	if err != nil {
		return err
	}

	// Start listening to ScreencastFrame events.
	screencastFrame, err := c.Page.ScreencastFrame(ctx)
	if err != nil {
		return err
	}

	go func() {
		defer screencastFrame.Close()

		for {
			ev, err := screencastFrame.Recv()
			if err != nil {
				log.Printf("Failed to receive ScreencastFrame: %v", err)
				return
			}
			log.Printf("Got frame with sessionID: %d: %+v", ev.SessionID, ev.Metadata)

			err = c.Page.ScreencastFrameAck(ctx, cdpcmd.NewPageScreencastFrameAckArgs(ev.SessionID))
			if err != nil {
				log.Printf("Failed to ack ScreencastFrame: %v", err)
				return
			}

			// Write to screencast_frame-[timestamp].png.
			name := fmt.Sprintf("screencast_frame-%d.png", ev.Metadata.Timestamp.Time().Unix())

			// Write the frame to file (without blocking).
			go func() {
				err = ioutil.WriteFile(name, ev.Data, 0644)
				if err != nil {
					log.Printf("Failed to write ScreencastFrame to %q: %v", name, err)
				}
			}()
		}
	}()

	screencastArgs := cdpcmd.NewPageStartScreencastArgs().
		SetEveryNthFrame(1).
		SetFormat("png")
	err = c.Page.StartScreencast(ctx, screencastArgs)
	if err != nil {
		return err
	}

	// Random delay for our screencast.
	time.Sleep(30 * time.Second)

	err = c.Page.StopScreencast(ctx)
	if err != nil {
		return err
	}

	return nil
}
{'fraction_non_alphanumeric': 0.14844267726971505, 'fraction_numerical': 0.0066269052352551355, 'mean_word_length': 3.2960151802656545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3485023', 'n_tokens_mistral': 1964, 'n_tokens_neox': 1741, 'n_words': 394}