Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon-ius committed Jun 30, 2024
1 parent 6ea1548 commit c3f6e7a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 6 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: weekly
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: weekly
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: ci

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_TERM_COLOR: always
CARGO_LOCA: "Cargo.toml"
CARGO_BIN: "ksc"

permissions:
contents: write

on:
workflow_dispatch:
push:
branches:
- "main"
paths:
- "Cargo.toml"
- ".github/workflows/ci.yml"

jobs:
pre:
runs-on: ubuntu-latest
outputs:
cargo_loc: ${{ env.CARGO_LOCA }}
cargo_bin: ${{ env.CARGO_BIN }}
steps:
- run: echo "Bypass GitHub Action Restriction"

call:
needs:
- pre
uses: AUTOM77/Rust-Actions/.github/workflows/ci.yml@main
with:
cargo_loc: ${{ needs.pre.outputs.cargo_loc }}
cargo_bin: ${{ needs.pre.outputs.cargo_bin }}
secrets: inherit
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
[package]
name = "kingschip"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

[dependencies]
clap = { version= "4.5.7", features=["derive"] }
tokio = { version = "1.38.0", default-features = false, features = ["rt-multi-thread", "fs"] }
reqwest = { version = "0.12.5", default-features = false, features = ["http2", "json", "rustls-tls"] }
fantoccini = { version = "0.19.3", default-features = false, features = ["rustls-tls"] }
serde_json = { version = "1.0.117", default-features = false, features = ["alloc"] }
ring = "0.17.8"

[[bin]]
name = "kc"
name = "ksc"
path = "src/cli.rs"

[profile.release]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# KingsChip

## Compile

```dash
sudo apt install firefox-esr
geckodriver --log debug
geckodriver -b /usr/bin/firefox-developer-edition --log debug
```
## Run
`./kc $code`
6 changes: 3 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
mod cipher;
mod ui;
use clap::Parser;

#[derive(Parser)]
struct Cli {
code: String,
}


fn main() -> Result<(), Box<dyn std::error::Error>> {
let start_time = std::time::Instant::now();
let cli = Cli::parse();

let host = cipher::get_host();
println!("{:#?}", host);
println!("{:#?}", cli.code);
let drive = "http://127.0.0.1:4444";
let _ = ui::interface(drive, &host, &cli.code);

println!("Processing time: {:?}", start_time.elapsed());
Ok(())
Expand Down
98 changes: 98 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use fantoccini::{ClientBuilder, Client, Locator};
use serde_json::{json, Value};

async fn _answer(client: &Client, questions: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
client.wait().for_element(Locator::Css(".QuestionBody")).await?;
for q in questions{
let qid = &format!("#QID{}-label", q);
// println!("{:?}", qid);
client.wait().for_element(Locator::Css(qid)).await?;
if let Ok(elem) = client.find(Locator::Css(qid)).await {
let _ = client.execute(
"arguments[0].click();",
vec![serde_json::to_value(elem)?],
).await;
}
}

client.wait().for_element(Locator::Css("#NextButton")).await?;
client.find(Locator::Css("#NextButton")).await?.click().await?;
Ok(())
}

async fn call(drive: &str, url: &str, _code: &str) -> Result<(), Box<dyn std::error::Error>> {
let client = ClientBuilder::rustls()
.capabilities(
serde_json::json!({
"moz:firefoxOptions": {
"args": ["--headless"]
}
})
.as_object()
.unwrap()
.clone(),
)
.connect(drive).await.expect("failed to connect to WebDriver");
client.goto(url).await?;
client.find(Locator::Css(".Home_iframe__T3nfU")).await?.enter_frame().await?;

client.wait().for_element(Locator::Css("#Questions")).await?;
let f = client.form(Locator::Css("#Questions")).await?;
f.set_by_name("QR~QID65~6~TEXT", _code).await?;

if let Ok(elem) = client.find(Locator::Css("#NextButton")).await {
let _ = client.execute(
"arguments[0].click();",
vec![serde_json::to_value(elem)?],
).await;
}

let data: Value = json!(
[
["5-2"],
["7-2"],
["10-2"],
["60-1-2-col", "60-2-2-col", "60-3-2-col"],
["62-1-2-col", "62-2-2-col", "62-4-2-col"],
["63-2", "64-2"],
["30-2"],
["36-2"],
["71-1"],
["38-390"],
]
);

if let Some(array) = data.as_array() {
for item in array.iter() {
if let Some(inner_array) = item.as_array() {
let items: Vec<String> = inner_array.iter()
.map(|x| x.as_str().unwrap_or("Invalid item").to_string())
.collect();
_answer(&client, items).await?;
}
}
}

client.wait().for_element(Locator::Css("#QID45")).await?;
client.wait().for_element(Locator::Css("#NextButton")).await?;
if let Ok(elem) = client.find(Locator::Css("#NextButton")).await {
let _ = client.execute(
"arguments[0].click();",
vec![serde_json::to_value(elem)?],
).await?;
}

client.wait().for_element(Locator::Css("#EndOfSurvey")).await?;
let code = client.find(Locator::Css("strong")).await?.text().await?;

println!("{:#?}", code);

client.close().await?;
Ok(())
}

pub fn interface(drive: &str, url: &str, code: &str) -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?;
let _ = rt.block_on(call(drive, url, code));
Ok(())
}

0 comments on commit c3f6e7a

Please sign in to comment.