From 269e718880afd784a063eb513e39931d818a9ccd Mon Sep 17 00:00:00 2001 From: Nick Parker Date: Sat, 19 Jun 2021 01:42:22 +1200 Subject: [PATCH] Fix compile, still TODOs left --- Cargo.lock | 1 + Cargo.toml | 1 + src/http.rs | 38 ++++++++++++++++++++++++++++---------- src/twitch.rs | 5 ----- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8be196e..287b4b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1849,6 +1849,7 @@ name = "twitch-rss" version = "0.1.0" dependencies = [ "anyhow", + "async-lock", "async-std", "async-trait", "rss", diff --git a/Cargo.toml b/Cargo.toml index 212cbc0..2a6792a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ async-std = "1.8" surf = { version = "2.2", default-features = false, features = ["h1-client-rustls"] } serde = "1.0" # http server +async-lock = "2.3" async-trait = "0.1" tide = { version = "0.16", default-features = false, features = ["h1-server"] } # logging diff --git a/src/http.rs b/src/http.rs index 6aece17..5d791af 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Context, Result}; +use async_lock::Mutex; use async_std::task; use rss::{ChannelBuilder, ImageBuilder, ItemBuilder}; use tide::Request; @@ -10,13 +11,13 @@ use crate::twitch; #[derive(Clone)] struct State { - twitch_auth: Arc, + twitch_auth: Arc>, account_cache: HashMap, // TODO use a TTL cache here } pub fn run(listen_endpoint: String, twitch_auth: twitch::TwitchAuth) -> Result<()> { let state = State { - twitch_auth: Arc::new(twitch_auth), + twitch_auth: Arc::new(Mutex::new(twitch_auth)), account_cache: HashMap::new() }; @@ -46,22 +47,39 @@ async fn handle_index(req: Request) -> tide::Result { async fn handle_rss(req: Request) -> tide::Result { let mut query_account = None; - let mut include_live = false; for (key, val) in req.url().query_pairs() { if key == "account" { query_account = Some(val.to_string()); } - if key == "live" { - include_live = true; - } } match query_account { Some(account) => { // TODO check userid cache, then try get_user_id // TODO check video cache, then try get_videos - let user = twitch::get_user(&mut req.state().twitch_auth, account).await?; - let videos = twitch::get_videos(&mut req.state().twitch_auth, user.id).await?; + let user: twitch::GetUsersEntry; + let videos: Vec; + { + let mut auth = req.state().twitch_auth.lock().await; + match twitch::get_user(&mut auth, account).await { + Ok(u) => { user = u; } + Err(e) => { + return Ok(tide::Response::builder(400) + .body(format!("400 Bad Request: {}", e)) + .content_type(tide::http::mime::PLAIN) + .build()); + } + }; + match twitch::get_videos(&mut auth, user.id).await { + Ok(v) => { videos = v; } + Err(e) => { + return Ok(tide::Response::builder(400) + .body(format!("400 Bad Request: {}", e)) + .content_type(tide::http::mime::PLAIN) + .build()); + } + }; + } let mut items = vec![]; for video in videos { items.push( @@ -69,8 +87,8 @@ async fn handle_rss(req: Request) -> tide::Result { .title(format!("{} [{}]", video.title, video.duration)) // TODO embed player in content? .content(format!("", video.thumbnail_url)) - .link(video.url) - .description(video.description) + .link(video.url.clone()) // TODO TEMP: clone to be removed + .description(format!("{:?}", video)) // TODO TEMP .pub_date(video.published_at) .build() .map_err(|e| anyhow!("Error when rendering RSS item: {}", e))? diff --git a/src/twitch.rs b/src/twitch.rs index 7426cec..077be9a 100644 --- a/src/twitch.rs +++ b/src/twitch.rs @@ -1,11 +1,6 @@ use anyhow::{anyhow, bail, Result}; use serde::{Deserialize, Serialize}; -struct AccountInfo { - name: String, - id: usize, -} - // Request parameters for GET id.twitch.tv/oauth2/token #[derive(Debug, Serialize)] struct LoginQuery { -- 2.38.5