123 lines
3.8 KiB
Go
Executable file
123 lines
3.8 KiB
Go
Executable file
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gocv.io/x/gocv"
|
|
)
|
|
|
|
// Stream Unexported
|
|
type Stream struct {
|
|
URL string
|
|
base64 string
|
|
streamDirPath string
|
|
}
|
|
|
|
// NewStream creates a new Stream Object
|
|
func NewStream(URL string) Stream {
|
|
base64 := URLToBase64(URL)
|
|
streamDirPath := filepath.Join(".", "streams", base64)
|
|
return Stream{
|
|
URL: URL,
|
|
base64: base64,
|
|
streamDirPath: streamDirPath,
|
|
}
|
|
}
|
|
|
|
// GetStreamInstant http.Get(URL) and returns the response
|
|
func (s Stream) GetStreamInstant(URL string) []byte {
|
|
resp, err := http.Get(URL)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
img, err := ioutil.ReadAll(resp.Body)
|
|
defer resp.Body.Close()
|
|
return img
|
|
}
|
|
|
|
// SaveStreamInstant writes the img to the CurrentStreamInstantPath, moves existing instant to PreviousStreamInstantPath
|
|
func (s Stream) SaveStreamInstant(URL string, img []byte) {
|
|
streamDir := URLToBase64(URL)
|
|
streamStoreDir := filepath.Join(".", "streams", string(streamDir))
|
|
os.MkdirAll(streamStoreDir, os.ModePerm)
|
|
|
|
currentStreamInstantPath := filepath.Join(streamStoreDir, "current.jpg")
|
|
swap := FileExists(streamStoreDir)
|
|
|
|
if swap {
|
|
currentStreamInstantPath = filepath.Join(streamStoreDir, "next.jpg")
|
|
}
|
|
|
|
err := ioutil.WriteFile(currentStreamInstantPath, img, os.ModePerm)
|
|
if err != nil {
|
|
log.Fatal("Can't write latest stream instant.")
|
|
}
|
|
|
|
if swap {
|
|
previousStreamInstantPath := filepath.Join(streamStoreDir, "previous.jpg")
|
|
nextStreamInstantPath := currentStreamInstantPath
|
|
currentStreamInstantPath = filepath.Join(streamStoreDir, "current.jpg")
|
|
|
|
err = os.Rename(currentStreamInstantPath, previousStreamInstantPath)
|
|
err = os.Rename(nextStreamInstantPath, currentStreamInstantPath)
|
|
}
|
|
}
|
|
|
|
// GetStreamDirPath returns filepath.Join(".", "streams")
|
|
func (s Stream) GetStreamDirPath() string {
|
|
return filepath.Join(".", "streams")
|
|
}
|
|
|
|
// StreamStorePathExists returns FileExists(GetStreamDirPath())
|
|
func (s Stream) StreamStorePathExists() bool {
|
|
return FileExists(s.GetStreamDirPath())
|
|
}
|
|
|
|
// GetPreviousInstantPath returns filepath.Join(GetStreamDirPath(), s.base64, "previous.jpg")
|
|
func (s Stream) GetPreviousInstantPath() string {
|
|
return filepath.Join(s.GetStreamDirPath(), s.base64, "previous.jpg")
|
|
}
|
|
|
|
// PreviousInstantPathExists returns FileExists(GetPreviousInstantPath())
|
|
func (s Stream) PreviousInstantPathExists() bool {
|
|
return FileExists(s.GetPreviousInstantPath())
|
|
}
|
|
|
|
// IMReadPrevious returns gocv.IMRead(GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
|
func (s Stream) IMReadPrevious() gocv.Mat {
|
|
return gocv.IMRead(s.GetPreviousInstantPath(), gocv.IMReadGrayScale)
|
|
}
|
|
|
|
// GetCurrentInstantPath returns filepath.Join(s.GetStreamDirPath(), s.base64, "current.jpg")
|
|
func (s Stream) GetCurrentInstantPath() string {
|
|
return filepath.Join(s.GetStreamDirPath(), s.base64, "current.jpg")
|
|
}
|
|
|
|
// CurrentInstantPathExists returns FileExists(GetCurrentInstantPath())
|
|
func (s Stream) CurrentInstantPathExists() bool {
|
|
return FileExists(s.GetCurrentInstantPath())
|
|
}
|
|
|
|
// IMReadCurrent returns gocv.IMRead(GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
|
func (s Stream) IMReadCurrent() gocv.Mat {
|
|
return gocv.IMRead(s.GetCurrentInstantPath(), gocv.IMReadGrayScale)
|
|
}
|
|
|
|
// GetLastInstantPath returns filepath.Join(s.GetStreamDirPath(), s.base64, "last.jpg")
|
|
func (s Stream) GetLastInstantPath() string {
|
|
return filepath.Join(s.GetStreamDirPath(), s.base64, "last.jpg")
|
|
}
|
|
|
|
// LastInstantPathExists returns FileExists(GetLastInstantPath())
|
|
func (s Stream) LastInstantPathExists() bool {
|
|
return FileExists(s.GetLastInstantPath())
|
|
}
|
|
|
|
// IMReadLast returns gocv.IMRead(GetLastInstantPath(), gocv.IMReadGrayScale)
|
|
func (s Stream) IMReadLast() gocv.Mat {
|
|
return gocv.IMRead(s.GetLastInstantPath(), gocv.IMReadGrayScale)
|
|
}
|