~misterio/sistemer-bot

ae0c8f9cc67d7e65c1016bd9799736282a0b940d — Gabriel Fontes 2 years ago 94a58b9
adicionado comando de horarios
4 files changed, 69 insertions(+), 27 deletions(-)

M src/disciplina.rs
A src/horarios.rs
M src/lib.rs
M src/main.rs
M src/disciplina.rs => src/disciplina.rs +4 -26
@@ 1,50 1,28 @@
use anyhow::Result;
use regex::Regex;

fn sanitize_line(line: &str) -> Result<String> {
    log::info!("Transformando {:?}", line);

    let line = format!("{}\n", line);
    // Replace headers with <b>
    let line = Regex::new("h\\d")?.replace_all(&line, "b");
    // Remove <ul> entire lines
    let line = Regex::new("^ *</?ul>\n")?.replace_all(&line, "");
    let line = Regex::new("</?ul>")?.replace_all(&line, "");
    // Remove <li>
    let line = Regex::new("^ *</?li>\n")?.replace_all(&line, "");
    let line = Regex::new("</?li>")?.replace_all(&line, "");
    // Remove <p>
    let line = Regex::new("</?p>")?.replace_all(&line, "");
    // Remove email links
    let line = Regex::new("<a href=\"mailto:.*>(.*)</a>")?.replace(&line, "$1");

    log::info!("Em: {:?}", line);

    Ok(line.into())
}

pub async fn get_disciplina(disciplina: &str) -> Result<String> {
    let fulltext = reqwest::get("https://misterio.me/notes/bsi/disciplinas-2021-2.html")
        .await?
        .text()
        .await?;

    let start_pattern = Regex::new(&format!("<h2.*>.*{}.*</h2>", disciplina))?;
    let start_pattern = Regex::new(&format!("<h2.*>.*{}.*</h2>", disciplina.to_lowercase()))?;
    let stop_pattern = Regex::new("<hr />")?;

    let mut output: String = "".into();
    let mut adding = false;
    for line in fulltext.lines() {
        if start_pattern.is_match(line) {
        if start_pattern.is_match(&line.to_lowercase()) {
            log::info!("Iniciando escrita");
            output.push_str(&sanitize_line(line)?);
            output.push_str(&crate::sanitize_line(line)?);
            adding = true;
        } else if adding {
            if stop_pattern.is_match(line) {
                log::info!("Finalizando escrita:\n{}", output);
                return Ok(output);
            } else {
                output.push_str(&sanitize_line(line)?);
                output.push_str(&crate::sanitize_line(line)?);
            }
        }
    }

A src/horarios.rs => src/horarios.rs +30 -0
@@ 0,0 1,30 @@
use anyhow::Result;
use regex::Regex;

pub async fn get_horarios() -> Result<String> {
    let fulltext = reqwest::get("https://misterio.me/notes/bsi/disciplinas-2021-2.html")
        .await?
        .text()
        .await?;

    let start_pattern = Regex::new("<h2 id=\"horários\">")?;
    let stop_pattern = Regex::new("<hr />")?;

    let mut output: String = "".into();
    let mut adding = false;
    for line in fulltext.lines() {
        if start_pattern.is_match(&line.to_lowercase()) {
            log::info!("Iniciando escrita");
            output.push_str(&crate::sanitize_line(line)?);
            adding = true;
        } else if adding {
            if stop_pattern.is_match(line) {
                log::info!("Finalizando escrita:\n{}", output);
                return Ok(output);
            } else {
                output.push_str(&crate::sanitize_line(line)?);
            }
        }
    }
    return Ok(output);
}

M src/lib.rs => src/lib.rs +27 -0
@@ 1,1 1,28 @@
pub mod disciplina;
pub mod horarios;

use anyhow::Result;
use regex::Regex;

fn sanitize_line(line: &str) -> Result<String> {
    log::info!("Transformando {:?}", line);

    let line = format!("{}\n", line);
    // Replace headers with <b>
    let line = Regex::new("h\\d")?.replace_all(&line, "b");
    // Remove <ul> entire lines
    let line = Regex::new("^ *</?ul>\n")?.replace_all(&line, "");
    let line = Regex::new("</?ul>")?.replace_all(&line, "");
    // Remove <li>
    let line = Regex::new("^ *</?li>\n")?.replace_all(&line, "");
    let line = Regex::new("</?li>")?.replace_all(&line, "");
    // Remove <p>
    let line = Regex::new("</?p>")?.replace_all(&line, "");
    // Remove email links
    let line = Regex::new("<a href=\"mailto:.*>(.*)</a>")?.replace(&line, "$1");

    log::info!("Em: {:?}", line);

    Ok(line.into())
}


M src/main.rs => src/main.rs +8 -1
@@ 5,7 5,7 @@ use teloxide::utils::command::BotCommand;
use anyhow::Result;
use dotenv::dotenv;

use sistemer_bot::disciplina;
use sistemer_bot::{horarios, disciplina};

#[derive(BotCommand)]
#[command(rename = "lowercase", description = "These commands are supported:")]


@@ 14,6 14,8 @@ enum Command {
    Help,
    #[command(description = "mostra info de uma disciplina.")]
    Disciplina(String),
    #[command(description = "lista os horários.")]
    Horarios,
}

async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> Result<()> {


@@ 24,6 26,11 @@ async fn answer(cx: UpdateWithCx<AutoSend<Bot>, Message>, command: Command) -> R
                .parse_mode(Html)
                .await?
        }
        Command::Horarios => {
            cx.answer(horarios::get_horarios().await?)
                .parse_mode(Html)
                .await?
        }
    };

    Ok(())