~shockham/witcht

d3ab9e44d5a804f0d35c3ab6d520a84f8feb3a43 — shockham 7 months ago 86ec967
Remove web usage to make simpler
3 files changed, 14 insertions(+), 65 deletions(-)

M README.md
D index.html
M main.go
M README.md => README.md +12 -12
@@ 1,31 1,31 @@
# witcht

Watch twitch like a heathen:
- state of the art 480p
- defaults to state of the art 480p
- no chat

No deps aside standard lib.

Has a bit of javascript for hls so the stream will work for the web usage.
## Installation
```
    go install .
```

## Run Local

To open a stream to view using `mpv`:
```
    mpv `go run main.go <INSERT TWITCH CHANNEL NAME>`
    mpv `witcht [CHANNEL NAME]`
```

## Run Web

Start the server with:
Defaults to 480p if you want a higher quality (such as `720p60`):
```
    go run main.go
    mpv `witcht [CHANNEL NAME]:[QUALITY]`
```
once the cauldron is up and spicy go to

## Check live streams
For a provided list of channels will print if live, stream title and the stream category/game
```
    localhost:8080/<INSERT TWITCH CHANNEL NAME>
    witcht [LIST OF CHANNEL NAMES]
```

## Props

Big shout out to [twire](https://github.com/twireapp/Twire) providing a solid reference for this.

D index.html => index.html +0 -27
@@ 1,27 0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>witcht</title>
    <style>
        body { margin: 2rem 10rem; background: #333333; }
        video { width: 100%; }
    </style>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    <video id="video" controls></video>
    <script>
      var video = document.getElementById('video');
        var videoSrc = '{{.}}';
      if (video.canPlayType('application/vnd.apple.mpegurl')) {
        video.src = videoSrc;
      } else if (Hls.isSupported()) {
        var hls = new Hls();
        hls.loadSource(videoSrc);
        hls.attachMedia(video);
      }
    </script>
</body>
</html>

M main.go => main.go +2 -26
@@ 4,7 4,6 @@ import (
	"bytes"
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"log"
	"math/rand"


@@ 204,32 203,9 @@ func GetUrlAtQuality(channel string, quality string) (string, int) {

	if urlData, ok := streamData.Urls[quality]; ok {
		return urlData.url, streamId
	} else {
		return streamData.Urls[Quality480p].url, streamId
	}
}

func StartServer() {
	tmpl, err := template.ParseFiles("index.html")

	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		re := regexp.MustCompile(`/(?P<channel>.+)`)
		matches := re.FindStringSubmatch(r.URL.Path)
		channel := matches[1]

		url, _ := GetUrlAtQuality(channel, Quality480p)

		w.Header().Set("Content-Type", "text/html; charset=utf-8")

		err = tmpl.Execute(w, url)
	})

	log.Print("Warming up the cauldron")
	log.Fatal(http.ListenAndServe(":8080", nil))
	return streamData.Urls[Quality480p].url, streamId
}

func main() {


@@ 263,6 239,6 @@ func main() {

		wg.Wait()
	} else {
		StartServer()
		fmt.Println("witcht [channel/channels][:quality]")
	}
}