now pulls at the start and pushes at the end of the program

This commit is contained in:
Suguivy 2023-02-22 20:08:45 +01:00
parent 5859359f12
commit dcee9c1473
1 changed files with 122 additions and 41 deletions

View File

@ -1,91 +1,172 @@
use std::{process::Command, io::{BufRead, Write}};
use gelbooru_api::{Client, posts};
use gelbooru_api::{posts, Client};
use std::collections::HashSet;
use std::{
io::{BufRead, Write},
process::Command,
};
const LEGEND: &str = "[q]uit, [d]elete, [a]ccept";
const PENDING: &str = "./pending.csv";
const REJECTED: &str = "./rejected.csv";
const VERIFIED: &str = "./verified.csv";
const TAGS: [&str; 2] = ["2girls", "sleeping"];
#[tokio::main]
async fn main(){
async fn main() {
pull();
let mut arg = std::env::args().skip(1);
match &arg.next().expect("Not enough arguments, please use -f or -v")[..] {
"-f" => {fetch_mode(arg.collect::<Vec<_>>()).await},
"-v" => {verify_mode()},
_ => {},
match &arg
.next()
.expect("Not enough arguments, please use -f or -v")[..]
{
"-f" => fetch_mode(Vec::from(TAGS)).await,
"-v" => verify_mode(),
_ => {}
}
push();
}
async fn fetch_mode(tags: Vec<String>) {
async fn fetch_mode(tags: Vec<&str>) {
let client = Client::public();
let posts = posts()
.random(true)
.tags(&tags)
.send(&client);
let posts = posts().random(true).tags(&tags).send(&client);
let image_set1 = std::fs::read(PENDING).expect("File not found").iter().map(|c| *c as char).collect::<String>();
let image_set2 = std::fs::read(VERIFIED).expect("File not found").iter().map(|c| *c as char).collect::<String>();
let image_set3 = std::fs::read(REJECTED).expect("File not found").iter().map(|c| *c as char).collect::<String>();
let image_set1 = std::fs::read(PENDING)
.expect("File not found")
.iter()
.map(|c| *c as char)
.collect::<String>();
let image_set2 = std::fs::read(VERIFIED)
.expect("File not found")
.iter()
.map(|c| *c as char)
.collect::<String>();
let image_set3 = std::fs::read(REJECTED)
.expect("File not found")
.iter()
.map(|c| *c as char)
.collect::<String>();
let image_set = image_set1.lines().chain(image_set2.lines()).chain(image_set3.lines()).collect::<HashSet<_>>();
let image_set = image_set1
.lines()
.chain(image_set2.lines())
.chain(image_set3.lines())
.collect::<HashSet<_>>();
let posts = posts.await.unwrap();
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(PENDING)
.unwrap();
.write(true)
.append(true) // This is needed to append to file
.open(PENDING)
.unwrap();
for post in posts.posts {
let imgurl = post.image_url();
if !image_set.contains(imgurl){
if !image_set.contains(imgurl) {
write!(file, "{}\n", imgurl).unwrap();
}
}
}
fn verify_mode() {
let file: String = std::fs::read(PENDING).expect("File not found").iter().map(|c| *c as char).collect();
let file: String = std::fs::read(PENDING)
.expect("File not found")
.iter()
.map(|c| *c as char)
.collect();
let mut file = file.lines();
while let Some(image_uri) = file.next(){
while let Some(image_uri) = file.next() {
//Open image
Command::new("xdg-open")
.arg(image_uri)
.spawn()
.expect("open command failed to start");
.arg(image_uri)
.spawn()
.expect("open command failed to start");
//Print Legend
println!("{}", LEGEND);
// Wait for input
if input_parse(image_uri){
std::fs::write(PENDING, format!("{}\n{}", image_uri, file.map(|s| s.to_owned() + "\n").collect::<String>())).unwrap();
std::process::exit(0);
if input_parse(image_uri) {
std::fs::write(
PENDING,
format!(
"{}\n{}",
image_uri,
file.map(|s| s.to_owned() + "\n").collect::<String>()
),
)
.unwrap();
return;
}
}
std::fs::write(PENDING, "").unwrap();
}
fn pull() {
Command::new("scp")
.arg("bot:/home/bot/sleepinggaysbot/urls.csv")
.arg("verified.csv")
.spawn()
.expect("open command failed to start");
Command::new("scp")
.arg("bot:/home/bot/sleepinggaysbot/rejected.csv")
.arg("rejected.csv")
.spawn()
.expect("open command failed to start");
Command::new("scp")
.arg("bot:/home/bot/sleepinggaysbot/pending.csv")
.arg("pending.csv")
.spawn()
.expect("open command failed to start");
}
fn push() {
Command::new("scp")
.arg("verified.csv")
.arg("bot:/home/bot/sleepinggaysbot/urls.csv")
.spawn()
.expect("open command failed to start");
Command::new("scp")
.arg("rejected.csv")
.arg("bot:/home/bot/sleepinggaysbot/rejected.csv")
.spawn()
.expect("open command failed to start");
Command::new("scp")
.arg("pending.csv")
.arg("bot:/home/bot/sleepinggaysbot/pending.csv")
.spawn()
.expect("open command failed to start");
}
fn input_parse(image_uri: &str) -> bool {
let key = std::io::stdin().lock().lines().next().unwrap().unwrap().chars().next().unwrap_or('n');
let key = std::io::stdin()
.lock()
.lines()
.next()
.unwrap()
.unwrap()
.chars()
.next()
.unwrap_or('n');
match key {
'a' => {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(VERIFIED)
.unwrap();
.write(true)
.append(true) // This is needed to append to file
.open(VERIFIED)
.unwrap();
write!(file, "{}\n", image_uri).unwrap();
},
}
'q' => return true,
'd' => {
let mut file = std::fs::OpenOptions::new()
.write(true)
.append(true) // This is needed to append to file
.open(REJECTED)
.unwrap();
.write(true)
.append(true) // This is needed to append to file
.open(REJECTED)
.unwrap();
write!(file, "{}\n", image_uri).unwrap();
},
}
_ => return input_parse(image_uri),
};
false
}
}