smmry.rs (1892B)
1 use indoc::printdoc; 2 use serde::Deserialize; 3 use std::{env, process}; 4 5 #[derive(Deserialize)] 6 pub struct SmmryResponse { 7 sm_api_title: String, 8 sm_api_content: String, 9 sm_api_limitation: String, 10 } 11 pub fn summarize(url: &str) -> Result<SmmryResponse, ureq::Error> { 12 let api_key: String = match env::var("SMMRY_API_KEY") { 13 Ok(key) => key, 14 _ => { 15 eprintln!( 16 "Please set the environment variable SMMRY_API_KEY to your API key for smmry.com." 17 ); 18 process::exit(1); 19 } 20 }; 21 22 let base_url = "https://api.smmry.com/"; 23 let resp = ureq::get(base_url) 24 .query("SM_API_KEY", &api_key) 25 .query("SM_URL", url) 26 .call()?; 27 let data: SmmryResponse = resp.into_json()?; 28 Ok(data) 29 } 30 31 pub enum OutputFmt { 32 Text, 33 Html, 34 Markdown, 35 } 36 37 pub fn show_summary(summary: SmmryResponse, fmt: OutputFmt) { 38 match fmt { 39 OutputFmt::Text => { 40 printdoc! {" 41 {} 42 43 {} 44 45 Smmry: {} 46 ", summary.sm_api_title, summary.sm_api_content, summary.sm_api_limitation 47 }; 48 } 49 OutputFmt::Markdown => { 50 printdoc! {" 51 # {} 52 53 {} 54 55 _Smmry: {}_ 56 ", summary.sm_api_title, summary.sm_api_content, summary.sm_api_limitation 57 }; 58 } 59 OutputFmt::Html => { 60 printdoc! {" 61 <!DOCTYPE html> 62 <html> 63 <head><title>{}</title></head> 64 <body> 65 <h1>{}</h1> 66 <p>{}</p> 67 <footer>{}</footer> 68 </body> 69 </html> 70 ", summary.sm_api_title, summary.sm_api_title, summary.sm_api_content, summary.sm_api_limitation 71 }; 72 } 73 }; 74 }