Remove twitter features
parent
c8c4c12717
commit
988f012a3e
@ -1,42 +0,0 @@
|
|||||||
use egg_mode::Token;
|
|
||||||
use hydrus_api::Hydrus;
|
|
||||||
|
|
||||||
use crate::config::TwitterConfig;
|
|
||||||
use crate::error::Result;
|
|
||||||
use crate::utils::twitter::{get_token, get_tweet_media};
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(hydrus))]
|
|
||||||
pub async fn find_and_send_twitter_posts(
|
|
||||||
hydrus: &Hydrus,
|
|
||||||
twitter_cfg: TwitterConfig,
|
|
||||||
post_urls: Vec<String>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let token = get_token(twitter_cfg).await?;
|
|
||||||
let total_posts = post_urls.len();
|
|
||||||
|
|
||||||
for (index, post) in post_urls.into_iter().enumerate() {
|
|
||||||
tracing::info!("Importing post {} of {}", index + 1, total_posts);
|
|
||||||
if let Err(e) = import_post(&post, hydrus, &token).await {
|
|
||||||
tracing::error!("Failed to import {}: {}", post, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(hydrus))]
|
|
||||||
async fn import_post(post_url: &str, hydrus: &Hydrus, token: &Token) -> Result<()> {
|
|
||||||
tracing::debug!("Tweet {}", post_url);
|
|
||||||
let images = get_tweet_media(post_url, token).await?;
|
|
||||||
tracing::info!("Found {} images for tweet {}", images.len(), post_url);
|
|
||||||
|
|
||||||
for url in images {
|
|
||||||
let mut entry = hydrus.import().url(url).run().await?;
|
|
||||||
let files = entry.files().await?;
|
|
||||||
|
|
||||||
for mut file in files {
|
|
||||||
file.associate_urls(vec![post_url.to_string()]).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -1,4 +1,3 @@
|
|||||||
pub mod find_and_send_reddit_posts;
|
pub mod find_and_send_reddit_posts;
|
||||||
pub mod find_and_send_tags;
|
pub mod find_and_send_tags;
|
||||||
pub mod find_and_send_twitter_posts;
|
|
||||||
pub mod find_and_send_urls;
|
pub mod find_and_send_urls;
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use crate::{config::TwitterConfig, error::Result};
|
|
||||||
use egg_mode::auth::Token;
|
|
||||||
|
|
||||||
/// Returns the token that
|
|
||||||
/// can be used for twitter api requests
|
|
||||||
#[tracing::instrument(level = "debug")]
|
|
||||||
pub async fn get_token(config: TwitterConfig) -> Result<Token> {
|
|
||||||
let con_token = egg_mode::KeyPair::new(
|
|
||||||
Cow::from(config.consumer_key),
|
|
||||||
Cow::from(config.consumer_secret),
|
|
||||||
);
|
|
||||||
let token = egg_mode::auth::bearer_token(&con_token).await?;
|
|
||||||
|
|
||||||
Ok(token)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the media urls for a given tweet
|
|
||||||
#[tracing::instrument(level = "debug", skip(token))]
|
|
||||||
pub async fn get_tweet_media(url: &str, token: &Token) -> Result<Vec<String>> {
|
|
||||||
let id = get_tweet_id(url)?;
|
|
||||||
let tweet = egg_mode::tweet::show(id, token).await?.response;
|
|
||||||
|
|
||||||
if let Some(entities) = tweet.extended_entities {
|
|
||||||
let media = entities.media;
|
|
||||||
let urls: Vec<String> = media
|
|
||||||
.into_iter()
|
|
||||||
.map(|m| {
|
|
||||||
if let Some(video_info) = m.video_info {
|
|
||||||
video_info.variants.into_iter().next().unwrap().url
|
|
||||||
} else {
|
|
||||||
m.media_url_https
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
Ok(urls)
|
|
||||||
} else {
|
|
||||||
Ok(Vec::new())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the tweet ID for a given twitter url
|
|
||||||
#[tracing::instrument(level = "debug")]
|
|
||||||
fn get_tweet_id(url: &str) -> Result<u64> {
|
|
||||||
let mut url = url;
|
|
||||||
if let Some((left, _right)) = url.split_once('?') {
|
|
||||||
url = left;
|
|
||||||
}
|
|
||||||
let id = url
|
|
||||||
.rsplit('/')
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
.next()
|
|
||||||
.ok_or("No Tweet ID in twitter url")?;
|
|
||||||
let id = id
|
|
||||||
.parse::<u64>()
|
|
||||||
.map_err(|_| "Tweet ID cannot be parsed as u64")?;
|
|
||||||
|
|
||||||
Ok(id)
|
|
||||||
}
|
|
Loading…
Reference in New Issue