7 Commits

Author SHA1 Message Date
VC
3fdd81df50 Merge branch 'feat_links' into 'master'
feat: fields_attributes and note inside copyprofile

See merge request veretcle/scootaloo!50
2022-12-12 14:35:42 +00:00
VC
90f47079d9 fix: fields_attribute is busted so 🤷 2022-12-12 15:25:12 +01:00
VC
88b73f4bc5 chore: bump version 2022-12-12 15:25:12 +01:00
VC
87797c7ab0 feat: note inside copyprofile 2022-12-12 15:25:06 +01:00
VC
3645728ddf Merge branch 'fix_copy_profile' into 'master'
Fix copy profile

See merge request veretcle/scootaloo!49
2022-12-04 08:04:45 +00:00
VC
69648728d7 chore: bump version 2022-12-03 17:27:51 +01:00
VC
6af1e4c55a fix: encode display_name as utf16 2022-12-03 17:27:18 +01:00
4 changed files with 111 additions and 8 deletions

2
Cargo.lock generated
View File

@@ -1342,7 +1342,7 @@ dependencies = [
[[package]]
name = "scootaloo"
version = "1.1.0"
version = "1.1.2"
dependencies = [
"base64",
"clap",

View File

@@ -1,6 +1,6 @@
[package]
name = "scootaloo"
version = "1.1.0"
version = "1.1.2"
authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021"

View File

@@ -263,8 +263,20 @@ pub async fn profile(config: Config, bot: Option<bool>) {
.user
.ok_or_else(|| ScootalooError::new("No user in Tweet!"))?;
let mut display_name = twitter_user.name.clone();
display_name.truncate(30);
let note = get_note_from_description(
&twitter_user.description,
&twitter_user.entities.description.urls,
);
// let fields_attributes = get_attribute_from_url(&twitter_user.entities.url);
let display_name = Some(String::from_utf16_lossy(
&twitter_user
.name
.encode_utf16()
.take(30)
.collect::<Vec<u16>>(),
));
let header = match twitter_user.profile_banner_url {
Some(h) => Some(base64_media(&h).await?),
@@ -274,8 +286,8 @@ pub async fn profile(config: Config, bot: Option<bool>) {
let update_creds = UpdateCredentialsInputOptions {
discoverable: None,
bot,
display_name: Some(display_name),
note: twitter_user.description,
display_name,
note,
avatar: Some(
base64_media(&twitter_user.profile_image_url_https.replace("_normal", ""))
.await?,

View File

@@ -1,7 +1,14 @@
use crate::config::MastodonConfig;
use egg_mode::entities::{MentionEntity, UrlEntity};
use megalodon::{generator, mastodon::Mastodon, megalodon::AppInputOptions};
use egg_mode::{
entities::{MentionEntity, UrlEntity},
user::UserEntityDetail,
};
use megalodon::{
generator,
mastodon::Mastodon,
megalodon::{AppInputOptions, CredentialsFieldAttribute},
};
use regex::Regex;
use std::{collections::HashMap, io::stdin};
@@ -123,6 +130,33 @@ pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
Mastodon::new(masto.base.to_string(), Some(masto.token.to_string()), None)
}
/// Gets note from twitter_user description
pub fn get_note_from_description(t: &Option<String>, urls: &[UrlEntity]) -> Option<String> {
t.as_ref().map(|d| {
let mut n = d.to_owned();
let a_urls = associate_urls(urls, &None);
decode_urls(&mut n, &a_urls);
n
})
}
/// Gets fields_attribute from UserEntityDetail
#[allow(dead_code)]
pub fn get_attribute_from_url(
user_entity_detail: &Option<UserEntityDetail>,
) -> Option<Vec<CredentialsFieldAttribute>> {
user_entity_detail.as_ref().and_then(|u| {
u.urls.first().and_then(|v| {
v.expanded_url.as_ref().map(|e| {
vec![CredentialsFieldAttribute {
name: v.display_url.to_string(),
value: e.to_string(),
}]
})
})
})
}
/// Generic register function
/// As this function is supposed to be run only once, it will panic for every error it encounters
/// Most of this function is a direct copy/paste of the official `elefren` crate
@@ -207,6 +241,63 @@ token = "{}""#,
mod tests {
use super::*;
#[test]
fn test_get_attribute_from_url() {
let expected_credentials_field_attribute = CredentialsFieldAttribute {
name: "Nintendojo.fr".to_string(),
value: "https://www.nintendojo.fr".to_string(),
};
let true_urls = vec![UrlEntity {
display_url: "Nintendojo.fr".to_string(),
expanded_url: Some("https://www.nintendojo.fr".to_string()),
range: (1, 3),
url: "https://t.me/balek".to_string(),
}];
let false_urls = vec![UrlEntity {
display_url: "Nintendojo.fr".to_string(),
expanded_url: None,
range: (1, 3),
url: "https://t.me/balek".to_string(),
}];
assert!(get_attribute_from_url(&None).is_none());
assert!(get_attribute_from_url(&Some(UserEntityDetail { urls: false_urls })).is_none());
let binding = get_attribute_from_url(&Some(UserEntityDetail { urls: true_urls })).unwrap();
let result_credentials_field_attribute = binding.first().unwrap();
assert_eq!(
result_credentials_field_attribute.name,
expected_credentials_field_attribute.name
);
assert_eq!(
result_credentials_field_attribute.value,
expected_credentials_field_attribute.value
);
}
#[test]
fn test_get_note_from_description() {
let urls = vec![UrlEntity {
display_url: "tamerelol".to_string(),
expanded_url: Some("https://www.nintendojo.fr/dojobar".to_string()),
range: (1, 3),
url: "https://t.me/tamerelol".to_string(),
}];
let some_description = Some("Youpi | https://t.me/tamerelol".to_string());
let none_description = None;
assert_eq!(
get_note_from_description(&some_description, &urls),
Some("Youpi | https://www.nintendojo.fr/dojobar".to_string())
);
assert_eq!(get_note_from_description(&none_description, &urls), None);
}
#[test]
fn test_replace_tweet_by_toot() {
let mut associated_urls = HashMap::from([