mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 01:18:34 +00:00
self-managing twitter client
This commit is contained in:
1 parent
2787fa7d67
commit
7b9e122eb0
3 files changed
+55
-1
No files matched your search
+2
-1
@@ -1 +1,2 @@
|
||||
pub mod redis_client;
|
||||
pub mod redis_client;
|
||||
pub mod twitter_client;
|
||||
@@ -0,0 +1,46 @@
|
||||
use crate::AnyhowResult;
|
||||
use log::{info, debug, warn};
|
||||
use futures::TryStreamExt;
|
||||
use egg_mode::stream::StreamMessage;
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
|
||||
pub struct TwitterClient {
|
||||
access_token: egg_mode::Token,
|
||||
}
|
||||
|
||||
impl TwitterClient {
|
||||
|
||||
pub fn new(access_token: egg_mode::Token) -> Self {
|
||||
Self {
|
||||
access_token,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn main_loop(&self) {
|
||||
loop {
|
||||
if let Err(e) = self.run_stream().await {
|
||||
warn!("Disconnected from stream; error = {:?}", e);
|
||||
}
|
||||
info!("Reconnecting after timeout...");
|
||||
thread::sleep(Duration::from_secs(30));
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_stream(&self) -> AnyhowResult<()> {
|
||||
let stream = egg_mode::stream::filter()
|
||||
.follow(&[1297106371238932481]) // vocodes
|
||||
.track(&["vocodes"])
|
||||
.start(&self.access_token)
|
||||
.try_for_each(|m| {
|
||||
if let StreamMessage::Tweet(tweet) = m {
|
||||
info!("Tweet: {:?}", &tweet);
|
||||
} else {
|
||||
info!("Other (non-Tweet) message: {:?}", &m);
|
||||
}
|
||||
futures::future::ok(())
|
||||
});
|
||||
|
||||
Ok(stream.await?)
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ use egg_mode::stream::StreamMessage;
|
||||
use crate::secrets::Secrets;
|
||||
use crate::clients::redis_client::RedisClient;
|
||||
use log::{info, warn, debug};
|
||||
use crate::clients::twitter_client::TwitterClient;
|
||||
|
||||
pub type AnyhowResult<T> = anyhow::Result<T>;
|
||||
|
||||
@@ -146,6 +147,7 @@ async fn main() -> anyhow::Result<()>
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
let stream = egg_mode::stream::filter()
|
||||
.follow(&[1297106371238932481]) // vocodes
|
||||
.track(&["vocodes"])
|
||||
@@ -164,5 +166,10 @@ async fn main() -> anyhow::Result<()>
|
||||
warn!("Disconnected. Stream error: {:?}", e);
|
||||
}
|
||||
|
||||
Ok(())*/
|
||||
|
||||
let twitter_client = TwitterClient::new(twitter_access_token);
|
||||
|
||||
twitter_client.main_loop().await;
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user