Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit 0b1fc0a

Browse files
author
尼亚
authored
Upgrade rustube to 0.3.7 (#34)
Fix build issues ans updated release tags
1 parent d8b2bca commit 0b1fc0a

File tree

12 files changed

+116
-128
lines changed

12 files changed

+116
-128
lines changed

.github/workflows/ci.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ jobs:
1818
components: rustfmt, clippy
1919
- name: build and test
2020
working-directory: ./pkg/download
21-
run: |
22-
cargo build --tests --verbose && cargo test --verbose
21+
run: cargo test --verbose --lib
2322
build-and-test-go-module:
2423
name: Build and test go source
2524
strategy:
@@ -34,7 +33,6 @@ jobs:
3433
go-version: ${{ matrix.go-version }}
3534
- name: Checkout code
3635
uses: actions/checkout@v2
37-
- name: Test
38-
run: go test -v -coverage ./...
39-
- name: Build
40-
run: make
36+
- name: test build
37+
run: go build -v ./...
38+

.github/workflows/release.yml

+19-32
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,25 @@ permissions:
1515

1616

1717
jobs:
18-
publish-crate-on-new-release:
19-
name: "Publish crate on Release"
20-
runs-on: ubuntu-latest
21-
steps:
22-
- uses: actions/checkout@v2
23-
- uses: actions-rs/toolchain@v1
24-
with:
25-
toolchain: stable
26-
override: true
27-
- uses: katyo/publish-crates@v1
28-
with:
29-
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
30-
path: ./pkg/download
3118
publish-go-package-on-release:
32-
name: Release go binaries
19+
name: Release Go Binaries
3320
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
goos: [linux, windows, darwin]
24+
goarch: ["386", amd64, arm64]
25+
exclude:
26+
- goarch: "386"
27+
goos: darwin
28+
- goarch: arm64
29+
goos: windows
3430
steps:
35-
- name: Checkout
36-
uses: actions/checkout@v2
37-
with:
38-
fetch-depth: 0
39-
- name: Setup Go
40-
uses: actions/setup-go@v2
41-
with:
42-
go-version: 1.17
43-
- name: Run GoReleaser
44-
uses: goreleaser/goreleaser-action@v2
45-
with:
46-
distribution: goreleaser
47-
version: latest
48-
args: release --rm-dist
49-
env:
50-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51-
# GoReleaser Pro key, for 'goreleaser-pro' distribution
52-
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
31+
- uses: actions/checkout@v2
32+
- uses: wangyoucao577/[email protected]
33+
with:
34+
github_token: ${{ secrets.GITHUB_TOKEN }}
35+
goos: ${{ matrix.goos }}
36+
goarch: ${{ matrix.goarch }}
37+
goversion: 1.17
38+
pre_command: make build-static
39+
extra_files: LICENSE README.md

.github/workflows/security.yml

-22
This file was deleted.

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*.dylib
1919

2020
## Compiled binaries
21-
ytd
21+
main
2222
ydl
2323

2424

.goreleaser.yaml

-29
This file was deleted.

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ run-all: run-static
77
.PHONY: build-static
88
build-static:
99
rustup toolchain install nightly
10-
cd pkg/download && cargo +nightly build --release
10+
cd pkg/download && rustup run nightly cargo build --release
1111
cp pkg/download/target/release/libydl.a pkg/
1212
go build -v ./...
1313

@@ -18,9 +18,9 @@ run-static:
1818
# test rust lib
1919
.PHONY: test-rs
2020
test-rs:
21-
cd pkg/download && RUST_LOG=trace cargo test -- --nocapture
21+
cd pkg/download && RUST_LOG=trace rustup run nightly cargo test --lib
2222

2323
# clean all packages
2424
.PHONY: clean
2525
clean:
26-
rm -rf main_static pkg/libydl.so pkg/libydl.a pkg/download/target
26+
rm -rf main ydl pkg/libydl.so pkg/libydl.a pkg/download/target

main

-16.2 MB
Binary file not shown.

main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
const (
23-
VERSION = "v0.1.0" // current version
23+
VERSION = "v0.2.0" // current version
2424
URL = "https://www.youtube.com/watch?v=lWEbEtr_Vng" // default video url
2525
PATH = "." // default download path
2626
)
@@ -55,15 +55,15 @@ func main() {
5555
Action: func(c *cli.Context) error {
5656
// process args here
5757
cli.DefaultAppComplete(c)
58-
cli.HandleExitCoder(errors.New("Invalid `ydl` command"))
58+
cli.HandleExitCoder(errors.New("invalid `ydl` command"))
5959
cli.ShowAppHelp(c)
6060
cli.ShowCompletions(c)
6161
cli.ShowVersion(c)
6262

6363
// get app names
6464
fmt.Printf("Args: %#v\n", c.Args())
65-
fmt.Printf("%#v\n", c.String("id"))
66-
fmt.Printf("%#v\n", c.String("path"))
65+
fmt.Printf("IDs: %#v\n", c.String("id"))
66+
fmt.Printf("Path: %#v\n", c.String("path"))
6767

6868
// parse urls
6969
url := c.String("id")

pkg/download.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77

88
#ifndef DOWNLOAD_H
99
#define DOWNLOAD_H
10+
#include <stdint.h>
11+
#include <inttypes.h>
1012

11-
char * download(char *url, char *path);
13+
// result type
14+
typedef struct {
15+
int32_t Ok;
16+
int32_t Err;
17+
} result_t;
18+
19+
extern result_t download(const char *url, const char *path);
1220

1321
#endif

pkg/download/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "ydl"
33
description = "download youtube videos to path"
4-
version = "0.1.1"
4+
version = "0.3.0"
55
authors = ["Nyah Check <[email protected]>"]
66
license = "MIT"
77
homepage = "https://github.com/ch3ck"
@@ -24,6 +24,7 @@ panic = "abort"
2424
[dependencies]
2525
clippy = "0.0.302"
2626
env_logger = "0.9.0"
27+
libc = "0.2.112"
2728
log = "0.4.14"
28-
rustube = "0.3.6"
29+
rustube = "0.3.7"
2930
tokio = { version = "1.15.0", features = ["macros", "io-util", "sync", "rt-multi-thread"] }

pkg/download/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ydl
2+
3+
[![Build](https://github.com/ch3ck/youtube-dl/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/ch3ck/youtube-dl/actions/workflows/ci.yml)
4+
[![CodeQL](https://github.com/ch3ck/youtube-dl/actions/workflows/codeql-analysis.yml/badge.svg?branch=master)](https://github.com/ch3ck/youtube-dl/actions/workflows/codeql-analysis.yml))
5+
[![forthebadge](https://forthebadge.com/images/badges/contains-technical-debt.svg)](https://forthebadge.com)
6+
7+
`ydl` is a simple youtube video downloader.
8+
9+
10+
## Build
11+
12+
### Pre-requisites
13+
14+
1. Install [rust nightly](https://rust-lang.github.io/rustup/concepts/channels.html)
15+
2. Install the [go](https://go.dev/doc/install)
16+
17+
18+
### Install and Run
19+
20+
![image](https://user-images.githubusercontent.com/96080444/147299032-3459f700-c3b7-448e-83c3-ff2ba27a62f9.png)
21+
22+
23+
## License
24+
The scripts and documentation in this project are released under the [MIT License](LICENSE.md)
25+
26+
27+
## Author
28+
29+
**NOTE** Will stop maintaining this package soon, the *Youtube API* is highly unreliable(its a rat race).
30+
31+
- [Nyah Check](https://nyah.dev)

pkg/download/src/lib.rs

+42-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
//! -*- mode: rust; -*-
22
//!
33
//! download - downloads youtube files
4+
extern crate libc;
45
use env_logger;
5-
use log::{debug, error, info, log_enabled, Level};
6-
use rustube::{Id, Video};
7-
use std::ffi;
6+
use libc::c_char;
7+
use log::{debug, info};
8+
use rustube;
9+
use std::ffi::CStr;
810

911
#[no_mangle]
10-
pub async extern "C" fn download<'a>(
11-
url: &'a str,
12-
path: &'a str,
12+
pub async extern "C" fn download(
13+
c_url: *const c_char,
14+
c_path: *const c_char,
1315
) -> Result<(), Box<dyn std::error::Error>> {
1416
env_logger::init();
1517

18+
// convert str in C to rust safely
19+
let pre_url = unsafe {
20+
assert!(!c_url.is_null());
21+
CStr::from_ptr(c_url)
22+
};
23+
24+
let url = pre_url.to_str().unwrap();
25+
url.chars().count() as u32;
26+
27+
// convert str for path from C to rust safely
28+
let pre_path = unsafe {
29+
assert!(!c_path.is_null());
30+
CStr::from_ptr(c_path)
31+
};
32+
let path = pre_path.to_str().unwrap();
33+
path.chars().count() as u32;
34+
35+
// download video
1636
info!("video_url: {:?}", url);
17-
let id = Id::from_raw(&url)?;
18-
info!("video_id: {:?}", id);
19-
20-
let video = Video::from_id(id.into_owned()).await?;
21-
debug!("raw video: {:?}", video);
22-
23-
let _result = video
24-
.streams()
25-
.iter()
26-
.filter(|stream| {
27-
stream.includes_video_track && stream.includes_audio_track
28-
})
29-
.max_by_key(|stream| stream.quality_label)
30-
.unwrap()
31-
.download_to_dir(&path)
32-
.await
33-
.unwrap();
37+
let _result = rustube::download_best_quality(url).await?;
3438
debug!("download status: {:?}", _result);
3539

3640
Ok(())
@@ -39,13 +43,23 @@ pub async extern "C" fn download<'a>(
3943
#[cfg(test)]
4044
pub mod tests {
4145
use super::*;
46+
use libc;
47+
use std::ffi::CString;
4248

4349
#[tokio::test]
4450
async fn test_download() {
45-
let url = String::from("https://www.youtube.com/watch?v=lWEbEtr_Vng");
46-
let fp = String::from("~/Downloads");
47-
download(url.as_str(), fp.as_str())
48-
.await
49-
.unwrap_err();
51+
let url = CString::new("https://www.youtube.com/watch?v=lWEbEtr_Vng")
52+
.unwrap();
53+
url.as_ptr() as *const libc::c_char;
54+
55+
let fp = CString::new("~/Downloads").unwrap();
56+
fp.as_ptr() as *const libc::c_char;
57+
58+
download(
59+
url.as_ptr() as *const libc::c_char,
60+
fp.as_ptr() as *const libc::c_char,
61+
)
62+
.await
63+
.unwrap();
5064
}
5165
}

0 commit comments

Comments
 (0)