|
|
|
@ -17,6 +17,9 @@ use scraper::Node; |
|
|
|
use ego_tree::NodeRef;
|
|
|
|
|
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
use json::JsonValue;
|
|
|
|
use json::stringify;
|
|
|
|
use json::object;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Question {
|
|
|
|
@ -24,6 +27,16 @@ struct Question { |
|
|
|
answer: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Question {
|
|
|
|
fn to_json(&self) -> JsonValue {
|
|
|
|
//TODO: fix this syntax to match the actual cards
|
|
|
|
object!{
|
|
|
|
question: self.question.clone(),
|
|
|
|
answer: self.answer.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Anki {
|
|
|
|
uri: Uri,
|
|
|
|
deck_id: String,
|
|
|
|
@ -40,10 +53,11 @@ fn main() { |
|
|
|
let rt = Runtime::new().unwrap();
|
|
|
|
// the window of available data is like today - 15 days or something
|
|
|
|
let for_date = NaiveDate::from_ymd_opt(2023, 7, 27).unwrap(); // TODO: replace with today
|
|
|
|
let questions = rt.block_on(scrape_questions(for_date));
|
|
|
|
//let questions = rt.block_on(scrape_questions(for_date));
|
|
|
|
let anki = Anki{ uri: Uri::from_static("http://localhost:8765"), deck_id: "lol".to_string()};
|
|
|
|
println!("Questions: {:?}", questions);
|
|
|
|
rt.block_on(ensure_in_anki(&anki, questions.unwrap()));
|
|
|
|
//println!("Questions: {:?}", questions);
|
|
|
|
rt.block_on(ensure_in_anki(&anki, vec![]));
|
|
|
|
//rt.block_on(ensure_in_anki(&anki, questions.unwrap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn scrape_questions(date: NaiveDate) -> Option<Vec<Question>> {
|
|
|
|
@ -91,17 +105,34 @@ fn strip_number_prefix(question: String) -> String { |
|
|
|
|
|
|
|
|
|
|
|
async fn ensure_in_anki(anki: &Anki, questions: Vec<Question>) -> Result<(), String> {
|
|
|
|
upload_to_anki(anki, questions.iter().filter(not_in_anki).collect::<Vec<_>>()).await
|
|
|
|
upload_to_anki(anki, questions.into_iter().filter(not_in_anki).collect::<Vec<_>>()).await
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: implement this
|
|
|
|
fn not_in_anki(_question: &&Question) -> bool {
|
|
|
|
fn not_in_anki(_question: &Question) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn upload_to_anki(anki: &Anki, questions: Vec<&Question>) -> Result<(), String> {
|
|
|
|
async fn upload_to_anki(anki: &Anki, questions: Vec<Question>) -> Result<(), String> {
|
|
|
|
let notes = JsonValue::Array(questions.iter().map(Question::to_json).collect());
|
|
|
|
let payload = object!{
|
|
|
|
//action: "addNotes",
|
|
|
|
action: "deckNamesAndIds",
|
|
|
|
version: 6,
|
|
|
|
params: {
|
|
|
|
//notes: notes,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let client = Client::new();
|
|
|
|
let request = Request::builder()
|
|
|
|
.uri(Uri::from_static("http://localhost:8765"))
|
|
|
|
.body(Body::from(""));
|
|
|
|
.body(Body::from(json::stringify(payload))).unwrap();
|
|
|
|
let response = client.request(request).await.unwrap();
|
|
|
|
let body_bytes = hyper::body::to_bytes(response).await.unwrap().to_vec();
|
|
|
|
let payload = json::parse(std::str::from_utf8(&body_bytes).unwrap()).unwrap();
|
|
|
|
println!("Data: {:?}", payload["result"]);
|
|
|
|
for (deckName, id) in payload["result"].entries() {
|
|
|
|
println!("Deck: {} {}", deckName, id.as_u64().unwrap());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|