-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
519 additions
and
48 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE processes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Your SQL goes here | ||
|
||
CREATE TABLE processes ( | ||
id serial PRIMARY KEY, | ||
code text NOT NULL, | ||
telegram_user_id TEXT NOT NULL, | ||
status text NOT NULL, | ||
created_at timestamp NOT NULL DEFAULT NOW(), | ||
updated_at timestamp NOT NULL DEFAULT NOW(), | ||
|
||
UNIQUE(telegram_user_Id, code) | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
extern crate dotenv; | ||
|
||
use diesel::pg::PgConnection; | ||
use diesel::prelude::*; | ||
use dotenv::dotenv; | ||
use std::env; | ||
|
||
pub fn establish_connection() -> PgConnection { | ||
dotenv().ok(); | ||
|
||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); | ||
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use chrono::NaiveDateTime; | ||
|
||
#[derive(Queryable, Debug)] | ||
pub struct Process { | ||
pub id: i32, | ||
pub code: String, | ||
pub telegram_user_id: String, | ||
pub status: String, | ||
pub created_at: NaiveDateTime, | ||
pub updated_at: NaiveDateTime, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use diesel::prelude::*; | ||
use regex::Regex; | ||
use reqwest; | ||
use select::document::Document; | ||
use select::predicate::{Class, Name, Predicate}; | ||
use std::error; | ||
|
||
use crate::database; | ||
use crate::models::*; | ||
use crate::schema::processes::dsl::*; | ||
|
||
#[derive(Debug)] | ||
pub struct ProcessResponse { | ||
pub status: String, | ||
pub info: String, | ||
} | ||
|
||
pub async fn start( | ||
telegram_id: String, | ||
process_code: String, | ||
) -> Result<ProcessResponse, Box<dyn error::Error>> { | ||
let p = fetch_for_one(&process_code).await?; | ||
save(&telegram_id, &process_code, &p.status).await; | ||
|
||
Ok(p) | ||
} | ||
|
||
async fn fetch_for_one(process_code: &str) -> Result<ProcessResponse, Box<dyn error::Error>> { | ||
Ok(fetch_citizenship_status(&process_code).await?) | ||
} | ||
|
||
async fn save(telegram_id: &str, process_code: &str, process_status: &str) { | ||
let connection = database::establish_connection(); | ||
diesel::insert_into(processes) | ||
.values(( | ||
telegram_user_id.eq(telegram_id), | ||
code.eq(process_code), | ||
status.eq(process_status.to_lowercase()), | ||
)) | ||
.on_conflict((telegram_user_id, code)) | ||
.do_update() | ||
.set(status.eq(process_status.to_lowercase())) | ||
.execute(&connection) | ||
.expect("error while inserting process"); | ||
} | ||
|
||
pub async fn fetch_for_all() -> Result<Vec<(String, ProcessResponse)>, Box<dyn error::Error>> { | ||
let connection = database::establish_connection(); | ||
let user_processes = processes | ||
.filter(status.ne("finished")) | ||
.load::<Process>(&connection) | ||
.expect("Error loading processes"); | ||
|
||
let mut updated_processes = Vec::new(); | ||
for process in user_processes { | ||
let process_response = fetch_for_one(&process.code).await?; | ||
if process_response.status.to_lowercase() != process.status { | ||
save( | ||
&process.telegram_user_id, | ||
&process.code, | ||
&process_response.status, | ||
) | ||
.await; | ||
updated_processes.push((process.telegram_user_id, process_response)); | ||
} | ||
} | ||
|
||
Ok(updated_processes) | ||
} | ||
|
||
async fn fetch_citizenship_status(process_code: &str) -> Result<ProcessResponse, reqwest::Error> { | ||
let res = reqwest::Client::new() | ||
.post("https://nacionalidade.justica.gov.pt/Home/GetEstadoProcessoAjax") | ||
.form(&[("SenhaAcesso", process_code)]) | ||
.send() | ||
.await?; | ||
let body = res.text().await?; | ||
|
||
let document = Document::from(body.as_str()); | ||
|
||
let mut p = ProcessResponse { | ||
status: String::from("unknown"), | ||
info: String::from(""), | ||
}; | ||
|
||
if let Some(st) = document.find(Class("active1").descendant(Name("p"))).last() { | ||
p.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("active2").descendant(Name("p"))).last() { | ||
p.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("active3").descendant(Name("p"))).last() { | ||
p.status = st.text(); | ||
} | ||
|
||
if let Some(st) = document.find(Class("container")).last() { | ||
let re = Regex::new(r"\s+").unwrap(); | ||
|
||
p.info = re.replace_all(&st.text(), " ").trim().to_string(); | ||
} | ||
|
||
Ok(p) | ||
} |
Oops, something went wrong.