Skip to content

Commit

Permalink
update structure & read config
Browse files Browse the repository at this point in the history
  • Loading branch information
wznmickey committed Apr 19, 2024
1 parent 53c88de commit dd4ef63
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ target/
# These are backup files generated by rustfmt
**/*.rs.bk
# for test, store the key temporarily
my_key.rs
my_key.rs

*.json
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ edition = "2021"
reqwest = { version = "0.12", features = ["json","blocking"] }
futures = "0.3"
serde_json = "1.0.115"
lazy_static = "1.4.0"
serde = {version = "1.0.150", features = ["derive"]}
file = "1.1.2"
24 changes: 24 additions & 0 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::config::*;
use crate::course::*;
use crate::download::*;
pub struct Account {
config: Config,
remote_data: RemoteData,
course: Vec<Course>,
downloadfolders: Vec<DownloadFolder>
}

impl Account {
pub fn new(st: &str) -> Self {
let config = Config::read_file(st);
let remote_data = RemoteData::new(&config.key, &config.canvas_url);
let course = remote_data.get_course_list();
Self {
config,
remote_data,
course,
downloadfolders:Vec::new()
}
}

}
33 changes: 33 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};
use std::fs::*;
use std::io::*;
#[derive(Serialize, Deserialize)]

pub struct Config {
pub key: String,
pub local_place: String,
pub canvas_url: String,
}


impl Config {
pub fn print(&self) -> () {
println!(
"local_place={local_place},canvas_url={canvas_url}",
local_place = self.local_place,
canvas_url = self.canvas_url
)
}
pub fn read_file(s:&str) -> Self {
let file = File::open(s).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).expect("Error while reading config file")
}
pub fn new() -> Self {
Self {
key: "".to_string(),
local_place: "".to_string(),
canvas_url: "".to_string(),
}
}
}
46 changes: 7 additions & 39 deletions src/course.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Course {
name: String,
course_code: String,
term_id: i64,
term_name: String,
term_name: String
}
pub fn get_course_from_json(x: &Value) -> Option<Course> {
Some(Course {
Expand All @@ -19,42 +19,10 @@ pub fn get_course_from_json(x: &Value) -> Option<Course> {
term_name: x["term"]["name"].as_str()?.to_string(),
})
}
// impl Course {
// pub fn download_files(&self) -> DownloadFolder {
// DownloadFolder {}
// }
// }

// pub struct DownloadFile {
// size: i64,
// remote_url: String,
// local_path: String,
// need_update: bool,
// local_temp_path: String,
// }

// impl DownloadFile {
// pub fn new(
// size: i64,
// remote_url: String,
// local_path: String,
// need_update: bool,
// local_temp_path: String,
// ) -> Self {
// Self {
// size,
// remote_url,
// local_path,
// need_update,
// local_temp_path,
// }
// }
// }
// pub struct DownloadFolder {
// files: Vec<DownloadFile>,
// name: String,
// local_path: String,
// }
// impl DownloadFolder {
// pub fn new(id: i64) -> Self {}
// }
#[derive(Debug)]
pub struct DownloadFolder {
name: String,
local_path: String,
remote_url: String,
}
21 changes: 11 additions & 10 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
use crate::course::*;
use crate::my_key::*;
use reqwest::blocking;
use reqwest::header;
use reqwest::{self};
use serde_json::Value;
pub struct RemoteData {
client: reqwest::blocking::Client,
url: String,
}

impl RemoteData {
pub fn new() -> Self {
pub fn new(key: &str, url: &str) -> Self {
let mut header = header::HeaderMap::new();
header.insert(
"Authorization",
header::HeaderValue::from_str(my_key()).unwrap(),
);
header.insert("Authorization", header::HeaderValue::from_str(key).unwrap());
Self {
client: blocking::ClientBuilder::new()
.default_headers(header)
.build()
.unwrap(),
url: url.to_string(),
}
}
pub fn get_course_list(self) -> Vec<Course> {
pub fn get_course_list(&self) -> Vec<Course> {
let mut ans: Vec<Course> = Vec::new();
let mut page = 1;
loop {
let _ = match || -> Option<()> {
let body = self
.client
.get(format!(
"https://jicanvas.com/api/v1/courses?include[]=term&page={}",
page
"{}/api/v1/courses?include[]=term&page={}",
self.url, page
))
.send()
.ok()?;
let result: Value = body.json().ok()?;
let result: &Vec<Value> = result.as_array()?;
for i in result {
let course = get_course_from_json(i)?;
println!("course={course:?}");
// println!("course={course:?}");
ans.push(course);
}
page += 1;
Expand All @@ -53,4 +51,7 @@ impl RemoteData {
}
ans
}
// pub fn get_folder_list(self,id:i64)->String{

// }
}
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
mod download;
mod my_key;
mod course;
// use futures::executor::block_on;
use crate::download::RemoteData;
mod config;
mod account;

fn main() {
// println!("Hello, world!");
let x = RemoteData::new();
let mycourses=x.get_course_list();
// for course in mycourses{
// course.download_files();
// }

let x = account::Account::new("/home/wznmickey/github/canvas_syncer/src/config.json");
}

0 comments on commit dd4ef63

Please sign in to comment.