Skip to content

Commit

Permalink
Merge pull request #2 from cybardev/main
Browse files Browse the repository at this point in the history
Pull from main
  • Loading branch information
cybardev authored Nov 18, 2024
2 parents 9557387 + e363ced commit 3b4558b
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 81 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
arch: [amd64, arm64]
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.22"

Expand All @@ -29,7 +29,7 @@ jobs:
go build -C cmd/ytgo -o "../../bin/ytgo-${GOOS}-${GOARCH}${EXT}"
- name: Upload ${{ matrix.os }}-${{ matrix.arch }} binary
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ytgo-${{ matrix.os }}-${{ matrix.arch }}
path: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/refresh-pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.22"

Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Download binaries
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
path: |
bin/
- name: Create Release with pushed tag
if: ${{ inputs.tagged }}
uses: "marvinpinto/action-automatic-releases@latest"
uses: softprops/action-gh-release@v2
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: |
bin/ytgo-*/ytgo-*
- name: Create Release with latest tag
if: ${{ ! inputs.tagged }}
uses: "marvinpinto/action-automatic-releases@latest"
uses: softprops/action-gh-release@v2
with:
automatic_release_tag: latest
repo_token: "${{ secrets.GITHUB_TOKEN }}"
tag_name: latest
token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: |
bin/ytgo-*/ytgo-*
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.22"

- name: Test build
run: go build -C cmd/ytgo -o "../../bin/ytgo"

- name: Run tests
run: go test -v -coverprofile=coverage.out ./cmd/ytgo
run: go test -short -v -coverprofile=coverage.out ./cmd/ytgo

- name: Report Coverage
uses: shogo82148/actions-goveralls@v1
Expand Down
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@

Click to navigate.

- [Dependencies](#dependencies)
- [Installation](#installation)
- [Manual](#manual)
- [With Go](#with-go)
- [Usage](#usage)
- [Examples](#examples)
- [Credits](#credits)
- [Dependencies](#dependencies)
- [Installation](#installation)
- [Manual](#manual)
- [With Go](#with-go)
- [Usage](#usage)
- [Examples](#examples)
- [Credits](#credits)

### Dependencies

- [mpv][mpv]
- [yt-dlp][ytdl]
- [ffmpeg][ffmpeg]
- [mpv][mpv]
- [yt-dlp][ytdl]
- [ffmpeg][ffmpeg]

### Installation

#### Manual

- Download the file from the Releases page: [ytgo-{os}-{arch}][release]
- Download the file from the Releases page: [ytgo-{os}-{arch}][release]

- **PS**: Make sure to choose the right binary for your OS and architecture
- **PS**: Make sure to choose the right binary for your OS and architecture

- Place it on your `$PATH` and make it executable.
- Place it on your `$PATH` and make it executable.

#### With Go

> **Link to package**: [pkg.go.dev/github.com/cybardev/ytgo/v3][gopkg]
- Run the following command:
- Run the following command:

```sh
go install github.com/cybardev/ytgo/v3/cmd/ytgo@latest
```

- Ensure `$GOPATH/bin` is added to `$PATH`. An easy way is to add this line to `~/.profile`:
- Ensure `$GOPATH/bin` is added to `$PATH`. An easy way is to add this line to `~/.profile`:

```sh
export PATH="$(go env GOPATH)/bin:$PATH"
Expand All @@ -63,50 +63,51 @@ Output of `ytgo -h`:

```sh
Usage of ytgo:
-d Display URL only
-i Interactive selection
-m Play music only
-d Display URL only
-i Interactive selection
-m Play music only
-n int
Play nth media (default 1)
-u Play from URL
-v Display version
Play nth media (default 1)
-p Prompt mode
-u Play from URL
-v Display version
```

**HINT**: [Here][mpv_hotkeys]'s a list of mpv keyboard shortcuts for your convenience.
#### Examples
- Play a video:
- Play a video:
`ytgo rickroll`
- Play an audio:
- Play an audio:
`ytgo -m gurenge band cover`
- Play the third search result:
- Play the third search result:
`ytgo -n 3 racing into the night`
- Play an audio from URL:
- Play an audio from URL:
`ytgo -u -m "https://www.youtube.com/watch?v=y6120QOlsfU"`
- **PS**: The URL must be quoted to avoid parsing by the shell
- **PS**: The URL must be quoted to avoid parsing by the shell
- Find the URL of a video:
- Find the URL of a video:
`ytgo -d hotaru maiko fujita`
- Interactive selection mode:
- Interactive selection mode:
`ytgo -i marmot scream meme`
### Credits
- [pystardust][pystardust]'s [ytfzf][ytfzf]
- [This article][article] I found during my quest to implement a simplified version of ytfzf in Python3
- [StackOverflow answer][regex] used for the regex `var ytInitialData = ({.*?});`
- [pystardust][pystardust]'s [ytfzf][ytfzf]
- [This article][article] I found during my quest to implement a simplified version of ytfzf in Python3
- [StackOverflow answer][regex] used for the regex `var ytInitialData = ({.*?});`

<!-- Links -->

Expand Down
9 changes: 7 additions & 2 deletions cmd/ytgo/resparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
Expand All @@ -17,8 +18,12 @@ func (r VideoRes) Parse() (*Video, error) {
if err != nil {
return nil, err
}
k := j.(map[string]interface{})["videoDetails"].(map[string]interface{})
return getVideoFromDetails(&k)
k, ok := j.(map[string]interface{})["videoDetails"].(map[string]interface{})
if ok {
return getVideoFromDetails(&k)
} else {
return &Video{}, errors.New("interface type mismatch")
}
}

func getVideoFromDetails(j *map[string]interface{}) (*Video, error) {
Expand Down
4 changes: 4 additions & 0 deletions cmd/ytgo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func GetRequest(url string) (string, error) {
}
defer res.Body.Close()

if res.StatusCode != 200 {
return "", errors.New("HTTP response status not OK")
}

body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
Expand Down
63 changes: 47 additions & 16 deletions cmd/ytgo/yt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"log"
"strings"

"github.com/chzyer/readline"
)

const VERSION string = "v3.0.12"
const VERSION string = "v3.1.3"

const (
C_RED string = "\x1b[31m"
Expand All @@ -17,18 +19,23 @@ const (
)

func main() {
// specify available flags
var (
d, i, m, u, ver bool
n int
query string
// command-line args
d, i, m, p, u, ver bool
n int
// declare vars
err error
query string
v *Video
rl *readline.Instance
)

// parse CLI args
flag.BoolVar(&ver, "v", false, "Display version")
flag.BoolVar(&d, "d", false, "Display URL only")
flag.BoolVar(&i, "i", false, "Interactive selection")
flag.BoolVar(&m, "m", false, "Play music only")
flag.BoolVar(&p, "p", false, "Prompt mode")
flag.BoolVar(&u, "u", false, "Play from URL")
flag.IntVar(&n, "n", 1, "Play nth media")
flag.Parse()
Expand All @@ -40,16 +47,32 @@ func main() {
}

// get search query
query = strings.Join(flag.Args(), " ")
if p {
// create line reader for search
rl, err = readline.New(fmt.Sprintf("%sSearch:%s ", C_CYAN, C_RESET))
if err != nil {
log.Fatalln(err)
}
defer rl.Close()

goto endloop
} else {
query = strings.Join(flag.Args(), " ")
}

loop:
if query == "" {
flag.Usage()
fmt.Println()
log.Fatalln("no query provided")
if p {
fmt.Println("No search query provided.")
goto endloop
} else {
flag.Usage()
fmt.Println()
log.Fatalln("no query provided")
}
}

// play media from YT or display URL
var v *Video
var err error
if u {
v, err = GetVideoFromURL(query)
} else if i {
Expand All @@ -60,14 +83,22 @@ func main() {
if err != nil {
log.Fatalln(err)
} else if v == nil {
return
}
if d {
fmt.Println(v.Id.URL())
return
} else if d {
fmt.Println(v.Id.URL())
} else {
err = v.Play(m)
}
err = v.Play(m)
if err != nil {
log.Fatalln(err)
}

endloop:
if p {
query, err = rl.Readline()
if err != nil {
return // exit on EOF/SIGINT
}
goto loop
}
}
Loading

0 comments on commit 3b4558b

Please sign in to comment.