Code Monkey home page Code Monkey logo

jw-scraper-rs's Introduction

ZJU-JWB scraper

Build status Coverage Status Crate version License: MIT Rust Docs

service

#[async_trait]
pub trait JWService {
    type Err;
    async fn login(&self, stu_id: &str, password: &str) -> Result<String, Self::Err>;
    async fn get_course_info(&self, code: &str) -> Result<CourseInfo, Self::Err>;
    async fn get_courses(
        &self,
        stu_id: &str,
        school_year: SchoolYear,
        semester: CourseSemester,
        cookie: &str,
    ) -> Result<Vec<Course>, Self::Err>;
    async fn get_exams(
        &self,
        stu_id: &str,
        school_year: SchoolYear,
        semester: ExamSemester,
        cookie: &str,
    ) -> Result<Vec<Exam>, Self::Err>;
    async fn get_scores(&self, stu_id: &str, cookie: &str) -> Result<Vec<Score>, Self::Err>;
    async fn get_major_scores(
        &self,
        stu_id: &str,
        cookie: &str,
    ) -> Result<Vec<MajorScore>, Self::Err>;
    async fn get_total_credit(&self, stu_id: &str, cookie: &str) -> Result<f32, Self::Err>;
}

The JWService is implemented for all types which implements interfacer_http::HttpClient.

You can refer to src/test.rs for all use cases.

client

You can use this crate with client feature, like this:

zju-jw-scraper = { version = "0.2", features = ["client"] }

Then, you can use default client provided by interfacer-http-hyper:

use zju_jw_scraper::{client::client, JWService};

#[tokio::test]
async fn test_login() -> Result<(), Box<dyn std::error::Error>> {
    let service = client("http://jwbinfosys.zju.edu.cn".parse()?);
    let cookie = service.login("319000000", "test").await?;
    assert!(!cookie.is_empty());
    Ok(())
}

However, the default client can only handle requests on HTTP, if you want to use other protocol like HTTPS, you need other Connect.

As the example connector:

use hyper_tls::HttpsConnector;
use zju_jw_scraper::{client::client_on, JWService};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let service = client_on("https://jw.zjuqsc.com".parse()?, HttpsConnector::new()?);
    let cookie = service.login("319000000", "test").await?;
    assert!(!cookie.is_empty());
    Ok(())
}

test

Run basic tests:

cargo test

To test this crate fully, create a config file test-settings.toml, and fill it referring to test-settings.toml.sample. Then run tests:

cargo test --all --all-features

As an alternative, you can configure tests using environment variables with prefix TEST_.

TEST_stu_id=3190000000 cargo test --all --all-features

Environment variables always have higher priority.

jw-scraper-rs's People

Contributors

hexilee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.