backend is done
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
use log::info;
|
||||
use log::{debug, info};
|
||||
use redis::{Client, Commands};
|
||||
use std::thread;
|
||||
|
||||
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!");
|
||||
@@ -9,14 +12,59 @@ fn main() {
|
||||
info!("Logging operational");
|
||||
// program the publpisher here
|
||||
|
||||
let client = Client::open("redis::/192.168.1.23/").unwrap();
|
||||
let client = Client::open("redis://192.168.1.23/").unwrap();
|
||||
|
||||
let mut connection = client.get_connection().unwrap();
|
||||
|
||||
loop{
|
||||
let query_url: String = format!("https://api.marketaux.com/v1/news/all/?api_token={}&countries={}&sentiment_gte={}&sentiment_lte={}&language={}&published_after={}&limit={}", NEWS_API_KEY.clone(), encode("us,ca").into_owned(),encode(0.25f32.to_string().as_str()).into_owned(), encode((-0.25f32).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);
|
||||
let response_batch = reqwest::blocking::get(query_url.as_str()).unwrap().text().unwrap();
|
||||
debug!("Query has returned: {}", response_batch);
|
||||
let returned: Value = serde_json::from_str(response_batch.as_str()).unwrap();
|
||||
let articles: Vec<Value> = returned.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*20));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user