feat: app 2.0
Some checks failed
FanslySync Build & Test / FanslySync Test Runner (push) Has been cancelled
Some checks failed
FanslySync Build & Test / FanslySync Test Runner (push) Has been cancelled
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
use lazy_static::lazy::Lazy;
|
||||
use lazy_static::lazy_static;
|
||||
// Create a simple module for handling the Fansly API, using reqwest to make requests to the API.
|
||||
// This module will contain a struct Fansly, which will have a method to get the user's profile information.
|
||||
@ -9,8 +8,8 @@ use crate::structs::{
|
||||
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use tokio::sync::Mutex;
|
||||
use thiserror::Error;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
// Create a PROGRESS mutex to hold the current sync progress, lazy initialized
|
||||
lazy_static! {
|
||||
@ -39,6 +38,11 @@ pub struct PasteResponse {
|
||||
payload: PasteData,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
struct PasteRequest {
|
||||
content: String,
|
||||
}
|
||||
|
||||
pub struct Fansly {
|
||||
client: reqwest::Client,
|
||||
token: Option<String>,
|
||||
@ -48,9 +52,6 @@ pub struct Fansly {
|
||||
pub enum UploadError {
|
||||
#[error("HTTP error: {0}")]
|
||||
Http(#[from] reqwest::Error),
|
||||
|
||||
#[error("Failed to get UUID from paste.hep.gg URL")]
|
||||
MissingUuid,
|
||||
}
|
||||
|
||||
impl Fansly {
|
||||
@ -230,44 +231,58 @@ impl Fansly {
|
||||
p.complete = complete;
|
||||
}
|
||||
|
||||
|
||||
async fn upload_sync_data(&self, data: SyncDataResponse) -> Result<String, UploadError> {
|
||||
let url = "https://paste.hep.gg/";
|
||||
let url = "https://paste.hep.gg/api/";
|
||||
|
||||
// Convert passed data to bytes
|
||||
let json_string = serde_json::to_string(&data).unwrap();
|
||||
// Make an JSON object with our raw data
|
||||
let paste_data = PasteRequest {
|
||||
content: serde_json::to_string(&data).unwrap(),
|
||||
};
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.text("content", json_string);
|
||||
let paste_data_str = serde_json::to_string(&paste_data).unwrap();
|
||||
let est_upload_size = paste_data_str.len() / 1024; // in KB
|
||||
|
||||
log::info!(
|
||||
"Uploading sync data to paste.hep.gg (size: {} KB)",
|
||||
est_upload_size
|
||||
);
|
||||
|
||||
// Create a new client and POST
|
||||
let response = self
|
||||
.client
|
||||
.post(url)
|
||||
.multipart(form)
|
||||
.body(paste_data_str)
|
||||
.header("Content-Type", "application/json")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
log::error!("Failed to upload sync data...");
|
||||
log::info!("Response: {:?}", response);
|
||||
return Err(UploadError::from(response.error_for_status().unwrap_err()));
|
||||
let status_code = response.status();
|
||||
let err = response.error_for_status_ref().unwrap_err();
|
||||
let response_text = response
|
||||
.text()
|
||||
.await
|
||||
.unwrap_or_else(|_| "Unknown error".to_string());
|
||||
|
||||
log::error!(
|
||||
"Failed to upload sync data to paste.hep.gg. Status code: {}, Response: {}",
|
||||
status_code,
|
||||
response_text
|
||||
);
|
||||
|
||||
return Err(UploadError::Http(err));
|
||||
}
|
||||
|
||||
log::info!("Uploaded sync data successfully.");
|
||||
|
||||
// Get the response URL from the response
|
||||
let url = response.url();
|
||||
// Parse the response
|
||||
let paste_response: PasteResponse = response.json().await?;
|
||||
|
||||
// Grab the UUID from the URL
|
||||
let uuid = url.path_segments()
|
||||
.and_then(|segments| segments.last())
|
||||
.ok_or(UploadError::MissingUuid)?;
|
||||
// Return the paste URL
|
||||
let paste_url = format!("https://paste.hep.gg/api/{}/raw", paste_response.payload.id);
|
||||
log::info!("Paste URL: {}", paste_url);
|
||||
|
||||
log::info!("Sync data uploaded to paste.hep.gg with UUID: {}", uuid);
|
||||
|
||||
// Return the URL of the uploaded data
|
||||
Ok(format!("https://paste.hep.gg/api/{}/raw", uuid))
|
||||
Ok(paste_url)
|
||||
}
|
||||
|
||||
pub async fn upload_auto_sync_data(
|
||||
@ -341,7 +356,8 @@ impl Fansly {
|
||||
|
||||
pub async fn sync(&mut self, auto: bool) -> Result<SyncDataResponse, String> {
|
||||
// Reset progress
|
||||
self.update_progress("Starting Sync".to_string(), 0, 100, false).await;
|
||||
self.update_progress("Starting Sync".to_string(), 0, 100, false)
|
||||
.await;
|
||||
|
||||
// Fetch profile
|
||||
log::info!("[sync::process] Fetching profile...");
|
||||
@ -357,10 +373,14 @@ impl Fansly {
|
||||
let total_followers = account.follow_count;
|
||||
let total_subscribers = account.subscriber_count;
|
||||
|
||||
log::info!("[sync::process] Account ID: {}, Followers: {}, Subscribers: {}",
|
||||
account.id, total_followers, total_subscribers);
|
||||
log::info!(
|
||||
"[sync::process] Account ID: {}, Followers: {}, Subscribers: {}",
|
||||
account.id,
|
||||
total_followers,
|
||||
total_subscribers
|
||||
);
|
||||
|
||||
let mut followers: Vec<FanslyFollowersResponse> = Vec::new();
|
||||
let mut followers: Vec<String> = Vec::new();
|
||||
let mut subscribers: Vec<Subscription> = Vec::new();
|
||||
|
||||
log::info!("[sync::process] Fetching followers...");
|
||||
@ -371,7 +391,9 @@ impl Fansly {
|
||||
while followers.len() < total_followers as usize {
|
||||
log::info!(
|
||||
"[sync::process] Fetching followers for account {} with offset {} (total: {})",
|
||||
account.id, offset, total_followers
|
||||
account.id,
|
||||
offset,
|
||||
total_followers
|
||||
);
|
||||
let response = self
|
||||
.fetch_followers(&account.id, &self.token.as_ref().unwrap(), offset)
|
||||
@ -382,7 +404,13 @@ impl Fansly {
|
||||
"[sync::process] Got {} followers from API.",
|
||||
response.response.len()
|
||||
);
|
||||
followers.extend(response.response.clone());
|
||||
|
||||
|
||||
// Collect followers
|
||||
for follower in response.response.clone() {
|
||||
followers.push(follower.follower_id);
|
||||
}
|
||||
|
||||
offset += 100;
|
||||
total_requests += 1;
|
||||
|
||||
@ -391,8 +419,9 @@ impl Fansly {
|
||||
"Fetching Followers".to_string(),
|
||||
followers.len() as u32,
|
||||
total_followers as u32,
|
||||
false
|
||||
).await;
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Every 10 requests, sleep for a bit to avoid rate limiting
|
||||
if total_requests % 10 == 0 {
|
||||
@ -400,7 +429,7 @@ impl Fansly {
|
||||
}
|
||||
|
||||
// If we've received no followers, break the loop
|
||||
if response.response.is_empty() {
|
||||
if response.clone().response.is_empty() {
|
||||
log::info!("[sync::process] No more followers found, breaking the loop.");
|
||||
break;
|
||||
}
|
||||
@ -411,7 +440,9 @@ impl Fansly {
|
||||
while subscribers.len() < total_subscribers as usize {
|
||||
log::info!(
|
||||
"[sync::process] Fetching subscribers with offset {} for account {} (total: {})",
|
||||
offset, account.id, total_subscribers
|
||||
offset,
|
||||
account.id,
|
||||
total_subscribers
|
||||
);
|
||||
|
||||
let response = self
|
||||
@ -428,8 +459,9 @@ impl Fansly {
|
||||
"Fetching Subscribers".to_string(),
|
||||
subscribers.len() as u32,
|
||||
total_subscribers as u32,
|
||||
false
|
||||
).await;
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Every 10 requests, sleep for a bit to avoid rate limiting
|
||||
if total_requests % 10 == 0 {
|
||||
@ -452,7 +484,8 @@ impl Fansly {
|
||||
log::info!("[sync::process] Sync complete.");
|
||||
|
||||
// Reset progress
|
||||
self.update_progress("Sync Complete".to_string(), 100, 100, true).await;
|
||||
self.update_progress("Sync Complete".to_string(), 100, 100, true)
|
||||
.await;
|
||||
|
||||
log::info!("[sync::process] Uploading sync data to paste.hep.gg for processing...");
|
||||
|
||||
|
Reference in New Issue
Block a user