78 lines
3.5 KiB
Rust
78 lines
3.5 KiB
Rust
use log::{debug, info};
|
|
use redis::{Client, Commands};
|
|
use std::{thread::{self, sleep}, time::{Duration, Instant}};
|
|
use serde_json::Value;
|
|
use serde::{Deserialize, Serialize};
|
|
use urlencoding::*;
|
|
use chrono::Local;
|
|
pub const NEWS_API_KEY: &str = "QdRyR4ztkKKeBq8btsOW0QVpiHWPQDnCRa6rBUkG";
|
|
fn main() {
|
|
println!("Hello, world!");
|
|
simple_logger::init().unwrap();
|
|
info!("Logging operational");
|
|
// program the publpisher here
|
|
|
|
let client = Client::open("redis://192.168.1.23/").unwrap();
|
|
|
|
let mut connection = client.get_connection().unwrap();
|
|
|
|
loop{
|
|
let query_url_gte: String = format!("https://api.marketaux.com/v1/news/all/?api_token={}&countries={}&sentiment_gte={}&language={}&published_after={}&limit={}", NEWS_API_KEY.clone(), encode("us,ca").into_owned(),encode(0.5f32.to_string().as_str()).into_owned(), "en",
|
|
encode((Local::now() - Duration::from_secs(20*60)).format("%Y-%m-%dT%H:%M:%S").to_string().as_str()).into_owned(), 10);
|
|
let query_url_lte: String = format!("https://api.marketaux.com/v1/news/all/?api_token={}&countries={}&sentiment_lte={}&language={}&published_after={}&limit={}", NEWS_API_KEY.clone(), encode("us,ca").into_owned(), encode((-0.5f32).to_string().as_str()).into_owned(), "en",
|
|
encode((Local::now() - Duration::from_secs(20*60)).format("%Y-%m-%dT%H:%M:%S").to_string().as_str()).into_owned(), 10);
|
|
info!(" Query url: {}", query_url_gte);
|
|
let response_batch_gte = reqwest::blocking::get(query_url_gte.as_str()).unwrap().text().unwrap();
|
|
debug!("Query has returned: {}", response_batch_gte);
|
|
info!(" Query url: {}", query_url_lte);
|
|
let response_batch_lte = reqwest::blocking::get(query_url_lte.as_str()).unwrap().text().unwrap();
|
|
debug!("Query has returned: {}", response_batch_lte);
|
|
|
|
let returned_gte: Value = serde_json::from_str(response_batch_gte.as_str()).unwrap();
|
|
let returned_lte: Value = serde_json::from_str(response_batch_lte.as_str()).unwrap();
|
|
|
|
let mut articles: Vec<Value> = returned_gte.get("data").unwrap().as_array().unwrap().to_vec();
|
|
articles.append(&mut returned_lte.get("data").unwrap().as_array().unwrap().to_vec());
|
|
let bytes_to_publish: String = serde_json::to_string(&strip_and_construct(articles)).unwrap();
|
|
|
|
let _: () = connection.publish("notifications", bytes_to_publish).unwrap();
|
|
|
|
sleep(Duration::from_secs(60*30));
|
|
}
|
|
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
|
|
struct Notification{
|
|
title: String,
|
|
description: String,
|
|
url: String,
|
|
image_url: String,
|
|
tickers: Vec<String>
|
|
}
|
|
|
|
fn strip_and_construct(articles: Vec<Value>) -> Vec<Notification>{
|
|
let mut returnable: Vec<Notification> = Vec::new();
|
|
|
|
for object in articles{
|
|
let title: String = object.get("title").unwrap().as_str().unwrap().to_string();
|
|
let description: String = object.get("description").unwrap().as_str().unwrap().to_string();
|
|
let url: String = object.get("url").unwrap().as_str().unwrap().to_string();
|
|
let image_url: String = object.get("image_url").unwrap().as_str().unwrap().to_string();
|
|
|
|
let entities: Vec<Value> = object.get("entities").unwrap().as_array().unwrap().to_vec();
|
|
|
|
let mut tickers: Vec<String> = Vec::new();
|
|
|
|
for entity in entities{
|
|
tickers.push(entity.get("symbol").unwrap().as_str().unwrap().to_string());
|
|
}
|
|
|
|
returnable.push(Notification { title, description, url, image_url, tickers});
|
|
}
|
|
|
|
|
|
|
|
return returnable;
|
|
}
|