mirror of
https://github.com/Tilo-K/pastebin-cli.git
synced 2026-01-10 17:01:01 +00:00
List command
This commit is contained in:
38
src/main.rs
38
src/main.rs
@@ -1,3 +1,5 @@
|
||||
use chrono::prelude::*;
|
||||
use chrono::DateTime;
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
@@ -16,12 +18,7 @@ struct Cli {
|
||||
enum Action {
|
||||
Create { file_path: std::path::PathBuf },
|
||||
Delete { paste_id: String },
|
||||
/*
|
||||
Edit {
|
||||
paste_id: String,
|
||||
file_path: std::path::PathBuf,
|
||||
},
|
||||
*/
|
||||
List { max_results: Option<u16> },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -34,6 +31,7 @@ async fn main() {
|
||||
match args.action {
|
||||
Action::Create { file_path } => create(file_path).await,
|
||||
Action::Delete { paste_id } => delete(paste_id).await,
|
||||
Action::List { max_results } => list(max_results).await,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -69,3 +67,31 @@ async fn delete(paste_id: String) {
|
||||
async fn edit(paste_id: String, file_path: PathBuf) {
|
||||
todo!("No offical way to do that yet");
|
||||
}
|
||||
|
||||
async fn list(max_results: Option<u16>) {
|
||||
let (x, y) = termion::terminal_size().unwrap();
|
||||
let resp = pastebin::list_pastes(
|
||||
&keys::API_KEY.lock().unwrap(),
|
||||
&keys::USER_KEY.lock().unwrap(),
|
||||
max_results.unwrap_or(10),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let line = "-".repeat(((x - 15) / 2).into());
|
||||
println!("{}Top {} pastes{}", line, max_results.unwrap_or(10), line);
|
||||
|
||||
for paste in resp {
|
||||
let mut title = paste.paste_title;
|
||||
if title == "" {
|
||||
title = "Untitled".to_owned();
|
||||
}
|
||||
|
||||
let naive = NaiveDateTime::from_timestamp_opt(paste.paste_date.into(), 0).unwrap();
|
||||
|
||||
let datetime: DateTime<Utc> = DateTime::from_utc(naive, Utc);
|
||||
let newdate = datetime.format("%Y-%m-%d %H:%M:%S");
|
||||
|
||||
println!("{}\t{}\t{}", title, newdate, paste.paste_url);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,21 @@
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{collections::HashMap, error::Error};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Paste {
|
||||
pub paste_key: String,
|
||||
pub paste_date: i64,
|
||||
pub paste_title: String,
|
||||
pub paste_size: u64,
|
||||
pub paste_expire_date: i64,
|
||||
pub paste_private: u8,
|
||||
pub paste_format_long: String,
|
||||
pub paste_format_short: String,
|
||||
pub paste_url: String,
|
||||
pub paste_hits: u64,
|
||||
}
|
||||
|
||||
pub async fn create_paste(
|
||||
api_key: &str,
|
||||
user_key: &str,
|
||||
@@ -61,11 +77,7 @@ pub async fn delete_paste(
|
||||
Ok(req.status().to_string() + "\n" + &req.text().await?)
|
||||
}
|
||||
|
||||
pub async fn get_user_key(
|
||||
api_key: &str,
|
||||
username: String,
|
||||
password: String,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
pub async fn get_user_key(api_key: &str, username: String, password: String) -> Result<String> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
@@ -86,3 +98,31 @@ pub async fn get_user_key(
|
||||
println!("{}", req.text().await?);
|
||||
panic!("Error logging in.");
|
||||
}
|
||||
|
||||
pub async fn list_pastes(api_key: &str, user_key: &str, max_results: u16) -> Result<Vec<Paste>> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let mut map = HashMap::new();
|
||||
map.insert("api_dev_key", api_key.trim().to_owned());
|
||||
map.insert("api_user_key", user_key.trim().to_owned());
|
||||
map.insert("api_option", "list".to_owned());
|
||||
map.insert("api_results_limit", max_results.to_string());
|
||||
|
||||
let req = client
|
||||
.post("https://pastebin.com/api/api_post.php")
|
||||
.form(&map)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if req.status() == 200 {
|
||||
let data = req.text().await?;
|
||||
let pastes: Vec<Paste> = quick_xml::de::from_str(&data).unwrap();
|
||||
return Ok(pastes);
|
||||
}
|
||||
|
||||
return Err(anyhow::format_err!(
|
||||
"{}\n{}",
|
||||
req.status().to_string(),
|
||||
req.text().await?
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user