-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"id": "extract-url", | ||
"name": "Extract url content", | ||
"version": "0.7.1", | ||
"version": "0.8.0", | ||
"description": "Extract url converting content into markdown", | ||
"author": "Stephen Solka", | ||
"authorUrl": "https://github.com/trashhalo", | ||
"isDesktopOnly": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"rollup-plugin-base64": "^1.0.1" | ||
}, | ||
"dependencies": { | ||
"hasbin": "^1.2.3", | ||
"node-fetch": "2.6.1" | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use futures::channel::oneshot; | ||
use js_sys::Error; | ||
use serde::Deserialize; | ||
use serde_json; | ||
use thiserror::Error; | ||
use url::{Host, Url}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen(module = "hasbin")] | ||
extern "C" { | ||
#[wasm_bindgen(js_name=sync)] | ||
pub fn has_bin(app: &str) -> bool; | ||
} | ||
|
||
#[wasm_bindgen(module = "child_process")] | ||
extern "C" { | ||
#[wasm_bindgen(js_name=exec)] | ||
pub fn node_exec(cmd: &str, f: &Closure<dyn FnMut(Option<Error>, String, String)>) -> JsValue; | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ExecError { | ||
#[error("error executing command. {0} {1}")] | ||
Error(String, String), | ||
|
||
#[error("oneshot caneled")] | ||
Canceled(#[from] oneshot::Canceled), | ||
} | ||
|
||
async fn exec(cmd: &str) -> Result<Option<String>, ExecError> { | ||
let (sender, receiver) = oneshot::channel::<Result<String, ExecError>>(); | ||
let cb = Closure::once(|err_val: Option<Error>, out: String, out_err: String| { | ||
let res = match err_val { | ||
None => sender.send(Ok(out)), | ||
Some(err) => { | ||
let msg: String = err.message().into(); | ||
sender.send(Err(ExecError::Error(msg, out_err))) | ||
} | ||
}; | ||
res.unwrap(); | ||
}); | ||
node_exec(cmd, &cb); | ||
let body = receiver.await??; | ||
Ok(Some(body)) | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct VideoMetadata { | ||
pub channel: String, | ||
pub uploader_url: String, | ||
pub description: String, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum MetadataError { | ||
#[error("error running youtube-dl {0}")] | ||
Exec(#[from] ExecError), | ||
|
||
#[error("Error serializing youtub data. {0}")] | ||
Serde(#[from] serde_json::error::Error), | ||
} | ||
|
||
pub async fn metadata(url: &Url) -> Result<Option<VideoMetadata>, MetadataError> { | ||
if !is_youtube(url) { | ||
return Ok(None); | ||
} else if !has_bin("youtube-dl") { | ||
return Ok(None); | ||
} | ||
match exec(&format!("youtube-dl -j {}", url)).await? { | ||
None => Ok(None), | ||
Some(s) => { | ||
let m: VideoMetadata = serde_json::from_str(&s)?; | ||
Ok(Some(m)) | ||
} | ||
} | ||
} | ||
|
||
fn is_youtube(url: &Url) -> bool { | ||
match url.host() { | ||
Some(Host::Domain("youtu.be")) | ||
| Some(Host::Domain("youtube.com")) | ||
| Some(Host::Domain("www.youtube.com")) => true, | ||
_ => false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters