Skip to content

Commit

Permalink
twiq-light: Remove TWIQ_LIGHT_TWITTER_BEARER_TOKEN env
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Dec 28, 2022
1 parent 36f398c commit beab579
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
53 changes: 50 additions & 3 deletions twiq-light/src/fetch.rs
@@ -1,15 +1,62 @@
use anyhow::bail;
use time::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
use tracing::{debug, instrument};

use crate::{
domain::MyTweet,
store::TweetQueueStore,
token::Token,
tweet_store::TweetStore,
twitter::{
self, GetUsersIdTweetsPathParams, GetUsersIdTweetsQueryParams, TweetResponseDataItem,
},
};

// TODO: duplicate
async fn ensure_token(
store: &TweetQueueStore,
client_id: &str,
client_secret: &str,
) -> anyhow::Result<Token> {
let token = store.read_token().await?;
match token {
Some(token) => {
let expires = OffsetDateTime::parse(&token.expires, &Rfc3339)?;
if OffsetDateTime::now_utc() < expires - Duration::seconds(10) {
Ok(token)
} else {
// use refresh token
let access_token_response = twitter::refresh_access_token(
client_id,
client_secret,
token.refresh_token.as_str(),
)
.await?;
debug!("{:?}", access_token_response);

let token = Token::try_from(
access_token_response,
OffsetDateTime::now_utc().unix_timestamp(),
)?;

store.write_token(&token).await?;

Ok(token)
}
}
None => bail!("Use `twiq-light queue authorize`"),
}
}

#[instrument(skip_all)]
pub async fn run(store: TweetStore, bearer_token: String) -> anyhow::Result<()> {
pub async fn run(
store: TweetStore,
tweet_queue_store: TweetQueueStore,
client_id: String,
client_secret: String,
) -> anyhow::Result<()> {
let token = ensure_token(&tweet_queue_store, &client_id, &client_secret).await?;
debug!("{:?}", token);
let mut data = store.read_all().await?;
let last_id_str = {
let mut at_id = data
Expand All @@ -26,7 +73,7 @@ pub async fn run(store: TweetStore, bearer_token: String) -> anyhow::Result<()>
};
let mut tweets = vec![];
let mut response = twitter::get_users_id_tweets(
&bearer_token,
&token.access_token,
&path_params,
&GetUsersIdTweetsQueryParams {
max_results: Some(100),
Expand All @@ -42,7 +89,7 @@ pub async fn run(store: TweetStore, bearer_token: String) -> anyhow::Result<()>
}
tweets.extend(response.data);
response = twitter::get_users_id_tweets(
&bearer_token,
&token.access_token,
&path_params,
&GetUsersIdTweetsQueryParams {
max_results: Some(100),
Expand Down
22 changes: 19 additions & 3 deletions twiq-light/src/main.rs
Expand Up @@ -87,8 +87,12 @@ enum QueueSubcommand {
#[derive(Clone, Debug, clap::Subcommand)]
enum TweetSubcommand {
Fetch {
#[arg(long, env = "TWIQ_LIGHT_TWITTER_BEARER_TOKEN")]
bearer_token: String,
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_ID")]
client_id: String,
#[arg(long, env = "TWIQ_LIGHT_TWITTER_CLIENT_SECRET")]
client_secret: String,
#[command(flatten)]
config: ConfigOptions,
},
Import {
file: String,
Expand Down Expand Up @@ -147,7 +151,19 @@ async fn main() -> anyhow::Result<()> {
Resource::Tweet(command) => {
let store = TweetStore::default();
match command {
TweetSubcommand::Fetch { bearer_token } => fetch::run(store, bearer_token).await,
TweetSubcommand::Fetch {
client_id,
client_secret,
config,
} => {
fetch::run(
store,
tweet_queue_store(config).await?,
client_id,
client_secret,
)
.await
}
TweetSubcommand::Import { file } => import::run(store, file).await,
TweetSubcommand::Search { query } => search::run(store, query).await,
}
Expand Down

0 comments on commit beab579

Please sign in to comment.