Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull from main #2

Merged
merged 24 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6e840e9
Merge pull request #1 from cybardev/dev
cybardev Mar 29, 2024
d06fe34
re: simplify logic
cybardev Apr 1, 2024
3558d80
feat: add prompt mode
cybardev Nov 17, 2024
c5f7ebd
update: new version
cybardev Nov 17, 2024
6ff6d5c
re: reorder tests
cybardev Nov 17, 2024
7bf6b1c
re: replace bufio.Reader with bufio.Scanner
cybardev Nov 17, 2024
7909081
feat: use readline for better prompts
cybardev Nov 18, 2024
a22e90d
re: comments
cybardev Nov 18, 2024
d062142
re: handle type conversion error
cybardev Nov 18, 2024
05f1797
re: raise error on non-OK HTTP response status
cybardev Nov 18, 2024
d067517
fix: skip YT-blocked test in pipeline
cybardev Nov 18, 2024
1add6fb
fix: skip YT-blocked test in pipeline
cybardev Nov 18, 2024
0ee3b9d
Merge branch 'main' of https://github.com/cybardev/ytgo
cybardev Nov 18, 2024
2a73d50
fix: update GitHub Action dependency
cybardev Nov 18, 2024
68b3e0e
fix: update GitHub Action dependencies
cybardev Nov 18, 2024
401f4b5
Merge branch 'main' of https://github.com/cybardev/ytgo
cybardev Nov 18, 2024
32b1fd5
Merge branch 'main' of https://github.com/cybardev/ytgo
cybardev Nov 18, 2024
1d2020f
Merge branch 'main' of https://github.com/cybardev/ytgo
cybardev Nov 18, 2024
ecbd831
re: make efficient use of labels
cybardev Nov 18, 2024
f7e71a6
update: new version
cybardev Nov 18, 2024
3afd013
docs: update usage instructions
cybardev Nov 18, 2024
b2ba9c9
update: new version
cybardev Nov 18, 2024
c24fc19
re: unified variable declarations
cybardev Nov 18, 2024
e363ced
update: new version
cybardev Nov 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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