Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created April 17, 2021 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/d7049de36296c7f7fccd44620a74283e to your computer and use it in GitHub Desktop.
Save cuongld2/d7049de36296c7f7fccd44620a74283e to your computer and use it in GitHub Desktop.
Code demonstration for getting good stock
#[path="models/cafef_liveboard.rs"]
pub mod cafef_liveboard;
use reqwest::header::CONTENT_TYPE;
use reqwest::header::USER_AGENT;
use regex::Regex;
use chrono::prelude::*;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use crate::cafef_liveboard::*;
use std::env;
use std::fs;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let now: DateTime<Local> = Local::now();
let three_months_ago_timestamp = (now - chrono::Duration::days(90)).date().and_hms(9, 10, 30).timestamp();
let current_timestamp = now.timestamp();
let client = reqwest::Client::new();
if let Ok(lines) = read_lines("./resources/list_stocks.txt") {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let mut stack: Vec<f32> = Vec::new();
let url =format!("https://api.vietstock.vn/ta/history?symbol={ip}&resolution=D&from={start_time}&to={end_time}",ip=ip,start_time=three_months_ago_timestamp,end_time=current_timestamp);
let res = client.get(url)
.header(CONTENT_TYPE,"application/x-www-form-urlencoded; charset=UTF-8")
.header(USER_AGENT,"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")
.send()
.await?;
let re = Regex::new("\"c.*\"v").unwrap();
let re_array = Regex::new(r"\[.*\]").unwrap();
let text = res.text().await?;
for cap in re.captures_iter(&text){
let local_text = &cap[0];
for local_cap in re_array.captures_iter(&local_text){
let test = &local_cap[0];
for (_, n) in test
.trim_matches(|c| c == '[' || c == ']')
.split(',')
.enumerate() {
stack.push(n.trim().parse::<f32>().unwrap());
}
}
}
let last_price = stack.last().ok_or("no item1")?;
for price in stack.iter(){
if last_price/price < 0.7{
println!("{}",ip);
println!("{}",last_price);
println!("{}",price);
break;
}
}
}
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment