Skip to content

Commit

Permalink
id uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
mbund committed Jan 14, 2024
1 parent 10076c7 commit 028883b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create ${{ steps.tag-name.outputs.tagname }} -t "Linter" -n "Binary release (${{ steps.tag-name.outputs.tagname }})"
gh release create ${{ steps.tag-name.outputs.tagname }} -t "canvas-cli" -n "Binary release (${{ steps.tag-name.outputs.tagname }})"
gh release upload ${{ steps.tag-name.outputs.tagname }} canvas-cli-x86_64-unknown-linux-musl
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

![canvas-cli submit --help](./docs/submit-help.gif)
![canvas-cli submit upload-test.pdf](./docs/submit.gif)
![canvas-cli submit -c 162316 -a 3936973 upload-test-1.pdf](./docs/submit-ids.gif)
Binary file added docs/submit-ids.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions docs/submit-ids.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Output docs/submit-ids.gif

Require canvas-cli

Set Shell fish
Set FontSize 32
Set Width 1500
Set Height 800
Set LoopOffset 50%
Set Framerate 24

Type "canvas-cli submit -c 162316 -a 3936973 upload-test-1.pdf" Sleep 500ms Enter

Sleep 8s
218 changes: 147 additions & 71 deletions src/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ impl Display for Assignment {
pub struct SubmitCommand {
/// File(s)
files: Vec<String>,

/// Course ID.
/// If not specified, will prompt for course
#[clap(long, short)]
course: Option<u32>,

/// Assignment ID.
/// If not specified, will prompt for assignment
#[clap(long, short)]
assignment: Option<u32>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -135,85 +145,151 @@ impl SubmitCommand {
.build()
.unwrap();

let courses_response = client
.get(format!(
"{}/api/v1/courses?per_page=1000&include[]=favorites&include[]=concluded",
url
))
.send()
.await?
.json::<Vec<CourseResponse>>()
.await?;
log::info!("Made REST request to get favorite courses");

let course_colors: HashMap<u32, String> = client
.get(format!("{}/api/v1/users/self/colors", url))
.send()
.await?
.json::<ColorsResponse>()
.await?
.custom_colors
.into_iter()
.filter(|(k, _)| k.starts_with("course_"))
.map(|(k, v)| (k.trim_start_matches("course_").parse::<u32>().unwrap(), v))
.collect();
log::info!("Made REST request to get course colors");
let course = if let Some(course_id) = self.course {
let course_response = client
.get(format!(
"{}/api/v1/courses/{}?include[]=favorites&include[]=concluded",
url, course_id
))
.send()
.await?
.json::<CourseResponse>()
.await?;
log::info!("Made REST request to get course information");

let course_colors: HashMap<u32, String> = client
.get(format!("{}/api/v1/users/self/colors", url))
.send()
.await?
.json::<ColorsResponse>()
.await?
.custom_colors
.into_iter()
.filter(|(k, _)| k.starts_with("course_"))
.map(|(k, v)| (k.trim_start_matches("course_").parse::<u32>().unwrap(), v))
.collect();
log::info!("Made REST request to get course colors");

println!("✓ Queried course information");

let course = Course {
name: course_response.name,
id: course_response.id,
is_favorite: course_response.is_favorite,
css_color: course_colors.get(&course_response.id).cloned(),
created_at: course_response.created_at,
};

println!("✓ Found {course}");
course
} else {
let courses_response = client
.get(format!(
"{}/api/v1/courses?per_page=1000&include[]=favorites&include[]=concluded",
url
))
.send()
.await?
.json::<Vec<CourseResponse>>()
.await?;
log::info!("Made REST request to get favorite courses");

let course_colors: HashMap<u32, String> = client
.get(format!("{}/api/v1/users/self/colors", url))
.send()
.await?
.json::<ColorsResponse>()
.await?
.custom_colors
.into_iter()
.filter(|(k, _)| k.starts_with("course_"))
.map(|(k, v)| (k.trim_start_matches("course_").parse::<u32>().unwrap(), v))
.collect();
log::info!("Made REST request to get course colors");

println!("✓ Queried assignment information");
println!("✓ Queried course information");

let mut courses: Vec<Course> = courses_response
.into_iter()
.filter(|course| !course.concluded)
.map(|course| Course {
name: course.name.clone(),
id: course.id,
is_favorite: course.is_favorite,
css_color: course_colors.get(&course.id).cloned(),
created_at: course.created_at,
})
.collect();
let mut courses: Vec<Course> = courses_response
.into_iter()
.filter(|course| !course.concluded)
.map(|course| Course {
name: course.name.clone(),
id: course.id,
is_favorite: course.is_favorite,
css_color: course_colors.get(&course.id).cloned(),
created_at: course.created_at,
})
.collect();

courses.sort_by(|a, b| {
b.is_favorite
.cmp(&a.is_favorite)
.then(a.created_at.cmp(&b.created_at))
});
Select::new("Course?", courses).prompt()?
};

courses.sort_by(|a, b| {
b.is_favorite
.cmp(&a.is_favorite)
.then(a.created_at.cmp(&b.created_at))
});
let course = Select::new("Course?", courses).prompt()?;
log::info!("Selected course {}", course.id);

let mut assignments: Vec<Assignment> = client
.get(format!(
"{}/api/v1/courses/{}/assignments?per_page=1000",
url, course.id
))
.send()
.await?
.json::<Vec<AssignmentResponse>>()
.await?
.into_iter()
.filter(|assignment| {
!assignment.locked_for_user && assignment.submission_types[0] == "online_upload"
})
.map(|assignment| Assignment {
name: assignment.name,
id: assignment.id,
due_at: assignment.due_at,
is_graded: assignment.graded_submissions_exist,
})
.collect();

assignments.sort_by(|a, b| a.is_graded.cmp(&b.is_graded).then(a.due_at.cmp(&b.due_at)));
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default();
let assignment = Select::new("Assignment?", assignments)
.with_filter(&|input, _, string_value, _| {
matcher.fuzzy_match(string_value, input).is_some()
})
.prompt()?;
let assignment = if let Some(assignment_id) = self.assignment {
let assignment_response = client
.get(format!(
"{}/api/v1/courses/{}/assignments/{}",
url, course.id, assignment_id
))
.send()
.await?
.json::<AssignmentResponse>()
.await?;
log::info!("Made REST request to get assignment information");

let assignment = Assignment {
name: assignment_response.name,
id: assignment_response.id,
due_at: assignment_response.due_at,
is_graded: assignment_response.graded_submissions_exist,
};

println!("✓ Found {assignment}");

assignment
} else {
let mut assignments: Vec<Assignment> = client
.get(format!(
"{}/api/v1/courses/{}/assignments?per_page=1000",
url, course.id
))
.send()
.await?
.json::<Vec<AssignmentResponse>>()
.await?
.into_iter()
.filter(|assignment| {
!assignment.locked_for_user && assignment.submission_types[0] == "online_upload"
})
.map(|assignment| Assignment {
name: assignment.name,
id: assignment.id,
due_at: assignment.due_at,
is_graded: assignment.graded_submissions_exist,
})
.collect();
log::info!("Made REST request to get assignment information");
println!("✓ Queried assignment information");

assignments.sort_by(|a, b| a.is_graded.cmp(&b.is_graded).then(a.due_at.cmp(&b.due_at)));
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default();
Select::new("Assignment?", assignments)
.with_filter(&|input, _, string_value, _| {
matcher.fuzzy_match(string_value, input).is_some()
})
.prompt()?
};

log::info!("Selected assignment {}", assignment.id);

let multi_progress = MultiProgress::new();
let futures = self.files.iter().map(|filepath| {
let future_files = self.files.iter().map(|filepath| {
upload_file(
&url,
&course,
Expand All @@ -224,7 +300,7 @@ impl SubmitCommand {
)
});

let uploaded_files = futures::future::join_all(futures).await;
let uploaded_files = futures::future::join_all(future_files).await;
let mut params: Vec<(String, String)> = uploaded_files
.into_iter()
.map(|f| {
Expand Down

0 comments on commit 028883b

Please sign in to comment.