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

Commit

Permalink
Upgrade rustube to 0.3.7 (#34)
Browse files Browse the repository at this point in the history
Fix build issues ans updated release tags
  • Loading branch information
尼亚 authored and ch3ck committed Dec 27, 2021
1 parent d8b2bca commit 54078a6
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 128 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ jobs:
components: rustfmt, clippy
- name: build and test
working-directory: ./pkg/download
run: |
cargo build --tests --verbose && cargo test --verbose
run: cargo test --verbose --lib
build-and-test-go-module:
name: Build and test go source
strategy:
Expand All @@ -34,7 +33,6 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test -v -coverage ./...
- name: Build
run: make
- name: test build
run: go build -v ./...

51 changes: 19 additions & 32 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,25 @@ permissions:


jobs:
publish-crate-on-new-release:
name: "Publish crate on Release"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- uses: katyo/publish-crates@v1
with:
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
path: ./pkg/download
publish-go-package-on-release:
name: Release go binaries
name: Release Go Binaries
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: ["386", amd64, arm64]
exclude:
- goarch: "386"
goos: darwin
- goarch: arm64
goos: windows
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# GoReleaser Pro key, for 'goreleaser-pro' distribution
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
- uses: actions/checkout@v2
- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: 1.17
pre_command: make build-static
extra_files: LICENSE README.md
22 changes: 0 additions & 22 deletions .github/workflows/security.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*.dylib

## Compiled binaries
ytd
main
ydl


Expand Down
29 changes: 0 additions & 29 deletions .goreleaser.yaml

This file was deleted.

6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ run-all: run-static
.PHONY: build-static
build-static:
rustup toolchain install nightly
cd pkg/download && cargo +nightly build --release
cd pkg/download && rustup run nightly cargo build --release
cp pkg/download/target/release/libydl.a pkg/
go build -v ./...

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

# clean all packages
.PHONY: clean
clean:
rm -rf main_static pkg/libydl.so pkg/libydl.a pkg/download/target
rm -rf main ydl pkg/libydl.so pkg/libydl.a pkg/download/target
Binary file removed main
Binary file not shown.
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

const (
VERSION = "v0.1.0" // current version
VERSION = "v0.2.0" // current version
URL = "https://www.youtube.com/watch?v=lWEbEtr_Vng" // default video url
PATH = "." // default download path
)
Expand Down Expand Up @@ -55,15 +55,15 @@ func main() {
Action: func(c *cli.Context) error {
// process args here
cli.DefaultAppComplete(c)
cli.HandleExitCoder(errors.New("Invalid `ydl` command"))
cli.HandleExitCoder(errors.New("invalid `ydl` command"))
cli.ShowAppHelp(c)
cli.ShowCompletions(c)
cli.ShowVersion(c)

// get app names
fmt.Printf("Args: %#v\n", c.Args())
fmt.Printf("%#v\n", c.String("id"))
fmt.Printf("%#v\n", c.String("path"))
fmt.Printf("IDs: %#v\n", c.String("id"))
fmt.Printf("Path: %#v\n", c.String("path"))

// parse urls
url := c.String("id")
Expand Down
10 changes: 9 additions & 1 deletion pkg/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@

#ifndef DOWNLOAD_H
#define DOWNLOAD_H
#include <stdint.h>
#include <inttypes.h>

char * download(char *url, char *path);
// result type
typedef struct {
int32_t Ok;
int32_t Err;
} result_t;

extern result_t download(const char *url, const char *path);

#endif
5 changes: 3 additions & 2 deletions pkg/download/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "ydl"
description = "download youtube videos to path"
version = "0.1.1"
version = "0.3.0"
authors = ["Nyah Check <[email protected]>"]
license = "MIT"
homepage = "https://github.com/ch3ck"
Expand All @@ -24,6 +24,7 @@ panic = "abort"
[dependencies]
clippy = "0.0.302"
env_logger = "0.9.0"
libc = "0.2.112"
log = "0.4.14"
rustube = "0.3.6"
rustube = "0.3.7"
tokio = { version = "1.15.0", features = ["macros", "io-util", "sync", "rt-multi-thread"] }
70 changes: 42 additions & 28 deletions pkg/download/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
//! -*- mode: rust; -*-
//!
//! download - downloads youtube files
extern crate libc;
use env_logger;
use log::{debug, error, info, log_enabled, Level};
use rustube::{Id, Video};
use std::ffi;
use libc::c_char;
use log::{debug, info};
use rustube;
use std::ffi::CStr;

#[no_mangle]
pub async extern "C" fn download<'a>(
url: &'a str,
path: &'a str,
pub async extern "C" fn download(
c_url: *const c_char,
c_path: *const c_char,
) -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

// convert str in C to rust safely
let pre_url = unsafe {
assert!(!c_url.is_null());
CStr::from_ptr(c_url)
};

let url = pre_url.to_str().unwrap();
url.chars().count() as u32;

// convert str for path from C to rust safely
let pre_path = unsafe {
assert!(!c_path.is_null());
CStr::from_ptr(c_path)
};
let path = pre_path.to_str().unwrap();
path.chars().count() as u32;

// download video
info!("video_url: {:?}", url);
let id = Id::from_raw(&url)?;
info!("video_id: {:?}", id);

let video = Video::from_id(id.into_owned()).await?;
debug!("raw video: {:?}", video);

let _result = video
.streams()
.iter()
.filter(|stream| {
stream.includes_video_track && stream.includes_audio_track
})
.max_by_key(|stream| stream.quality_label)
.unwrap()
.download_to_dir(&path)
.await
.unwrap();
let _result = rustube::download_best_quality(url).await?;
debug!("download status: {:?}", _result);

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

#[tokio::test]
async fn test_download() {
let url = String::from("https://www.youtube.com/watch?v=lWEbEtr_Vng");
let fp = String::from("~/Downloads");
download(url.as_str(), fp.as_str())
.await
.unwrap_err();
let url = CString::new("https://www.youtube.com/watch?v=lWEbEtr_Vng")
.unwrap();
url.as_ptr() as *const libc::c_char;

let fp = CString::new("~/Downloads").unwrap();
fp.as_ptr() as *const libc::c_char;

download(
url.as_ptr() as *const libc::c_char,
fp.as_ptr() as *const libc::c_char,
)
.await
.unwrap();
}
}

0 comments on commit 54078a6

Please sign in to comment.