mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-11 12:23:20 +00:00
cleanup
This commit is contained in:
1 parent
27b400724c
commit
ad7f7928f5
6 files changed
+45
-19
No files matched your search
@@ -3,6 +3,8 @@ use crate::error::grok_generic_api_error::GrokGenericApiError;
|
||||
use crate::requests::index_page::get_index_page_and_scripts::{get_index_page_and_scripts, GetIndexPageAndScriptsArgs};
|
||||
use crate::requests::index_page::index_parsers::parse_index_baggage::parse_index_baggage;
|
||||
use crate::requests::index_page::index_parsers::parse_index_sentry_trace::parse_index_sentry_trace;
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::{parse_index_verification_token, VerificationToken};
|
||||
use crate::requests::index_page::utils::find_script_actions_and_xsid_script_path::find_script_actions_and_xsid_script_path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct RequestClientSecretsArgs<'a> {
|
||||
@@ -20,6 +22,9 @@ pub struct ClientSecrets {
|
||||
/// From the index HTML meta tag
|
||||
pub sentry_trace: String,
|
||||
|
||||
/// From the index HTML meta tag
|
||||
pub verification_token: VerificationToken,
|
||||
|
||||
/// Scripts (for later use)
|
||||
pub scripts: HashMap<String, String>,
|
||||
}
|
||||
@@ -41,6 +46,20 @@ pub async fn request_client_secrets(args: RequestClientSecretsArgs<'_>) -> Resul
|
||||
message: "Index did not include sentry trace.".to_string()
|
||||
})?;
|
||||
|
||||
let verification_token = parse_index_verification_token(&payloads.index_body_html)
|
||||
.ok_or_else(|| GrokGenericApiError::IndexHtmlDidNotIncludeExpectedData {
|
||||
message: "Index did not include verification token.".to_string()
|
||||
})?;
|
||||
|
||||
let actions_and_xsid_script = find_script_actions_and_xsid_script_path(&payloads.scripts)?;
|
||||
|
||||
// self.svg_data, self.numbers = Parser.parse_values(c_request.text, self.anim, self.xsid_script)
|
||||
|
||||
// TODO: SvgData
|
||||
// TODO: Numbers
|
||||
// Do we need "anim" ?
|
||||
// TODO: xsid
|
||||
|
||||
// xsid: str = Signature.generate_sign(
|
||||
// '/rest/app-chat/conversations/new',
|
||||
// 'POST',
|
||||
@@ -52,6 +71,7 @@ pub async fn request_client_secrets(args: RequestClientSecretsArgs<'_>) -> Resul
|
||||
Ok(ClientSecrets {
|
||||
baggage,
|
||||
sentry_trace,
|
||||
verification_token,
|
||||
scripts: payloads.scripts,
|
||||
})
|
||||
}
|
||||
@@ -73,6 +93,7 @@ mod tests {
|
||||
|
||||
println!("Baggage: {:?}", secrets.baggage);
|
||||
println!("Sentry trace: {:?}", secrets.sentry_trace);
|
||||
println!("Verification token: {:?}", secrets.verification_token);
|
||||
|
||||
assert_eq!(1, 2);
|
||||
Ok(())
|
||||
|
||||
@@ -64,8 +64,8 @@ pub async fn get_numbers(args: GetNumbersArgs<'_>) -> Result<NumbersAndSvg, Grok
|
||||
mod tests {
|
||||
use crate::requests::index_page::get_index_page_and_scripts::{get_index_page_and_scripts, GetIndexPageAndScriptsArgs};
|
||||
use crate::requests::index_page::get_numbers::{get_numbers, GetNumbersArgs};
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::parse_verification_token_from_index_html;
|
||||
use crate::requests::index_page::utils::find_script_actions_and_xsid::find_script_actions_and_xsid;
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::parse_index_verification_token;
|
||||
use crate::requests::index_page::utils::find_script_actions_and_xsid_script_path::find_script_actions_and_xsid_script_path;
|
||||
use crate::requests::index_page::utils::verification_token_to_loading_anim::verification_token_to_loading_anim;
|
||||
use crate::test_utils::get_test_cookies::get_test_cookies;
|
||||
use errors::AnyhowResult;
|
||||
@@ -79,16 +79,16 @@ mod tests {
|
||||
cookie: &cookie,
|
||||
}).await?;
|
||||
|
||||
let verification_token = parse_verification_token_from_index_html(&page_and_scripts.index_body_html)
|
||||
let verification_token = parse_index_verification_token(&page_and_scripts.index_body_html)
|
||||
.expect("expected verification token");
|
||||
|
||||
println!("Verification Token: {:?}", verification_token);
|
||||
|
||||
let loading_anim = verification_token_to_loading_anim(&verification_token.0)?;
|
||||
let loading_anim = verification_token_to_loading_anim(&verification_token)?;
|
||||
|
||||
println!("Loading Animation: {:?}", loading_anim);
|
||||
|
||||
let actions_and_xsid_script = find_script_actions_and_xsid(&page_and_scripts.scripts)?;
|
||||
let actions_and_xsid_script = find_script_actions_and_xsid_script_path(&page_and_scripts.scripts)?;
|
||||
|
||||
println!("Actions and XSID: {:?}", actions_and_xsid_script);
|
||||
|
||||
|
||||
+3
-3
@@ -26,7 +26,7 @@ static JSON_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||
pub struct VerificationToken(pub(crate) String);
|
||||
|
||||
/// Parse the verification token from the root HTML
|
||||
pub fn parse_verification_token_from_index_html(html: &str) -> Option<VerificationToken> {
|
||||
pub fn parse_index_verification_token(html: &str) -> Option<VerificationToken> {
|
||||
let maybe_meta = scrape_meta_tag_via_parsing(html);
|
||||
|
||||
if let Some(meta) = maybe_meta {
|
||||
@@ -61,7 +61,7 @@ fn scrape_meta_tag_via_parsing(html: &str) -> Option<String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::requests::index_page::get_index_page::{get_index, GetIndexPageArgs};
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::parse_verification_token_from_index_html;
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::parse_index_verification_token;
|
||||
use crate::test_utils::get_test_cookies::get_test_cookies;
|
||||
use errors::AnyhowResult;
|
||||
|
||||
@@ -73,7 +73,7 @@ mod tests {
|
||||
cookie: &cookie,
|
||||
}).await?;
|
||||
|
||||
let result = parse_verification_token_from_index_html(&index.body);
|
||||
let result = parse_index_verification_token(&index.body);
|
||||
println!("{:?}", result);
|
||||
|
||||
assert_eq!(1, 2);
|
||||
|
||||
+3
-3
@@ -23,7 +23,7 @@ pub struct ActionsAndXsid {
|
||||
pub xsid_script_path: String,
|
||||
}
|
||||
|
||||
pub fn find_script_actions_and_xsid(scripts: &HashMap<String, String>) -> Result<ActionsAndXsid, GrokClientError> {
|
||||
pub fn find_script_actions_and_xsid_script_path(scripts: &HashMap<String, String>) -> Result<ActionsAndXsid, GrokClientError> {
|
||||
let mut action_script_path = None;
|
||||
let mut script_content_1 = None;
|
||||
let mut script_content_2 = None;
|
||||
@@ -74,7 +74,7 @@ pub fn find_script_actions_and_xsid(scripts: &HashMap<String, String>) -> Result
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::requests::index_page::get_index_page_and_scripts::{get_index_page_and_scripts, GetIndexPageAndScriptsArgs};
|
||||
use crate::requests::index_page::utils::find_script_actions_and_xsid::find_script_actions_and_xsid;
|
||||
use crate::requests::index_page::utils::find_script_actions_and_xsid_script_path::find_script_actions_and_xsid_script_path;
|
||||
use crate::test_utils::get_test_cookies::get_test_cookies;
|
||||
use errors::AnyhowResult;
|
||||
|
||||
@@ -87,7 +87,7 @@ mod tests {
|
||||
cookie: &cookie,
|
||||
}).await?;
|
||||
|
||||
let result = find_script_actions_and_xsid(&page_and_scripts.scripts)?;
|
||||
let result = find_script_actions_and_xsid_script_path(&page_and_scripts.scripts)?;
|
||||
|
||||
println!("{:?}", result);
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
pub mod find_script_actions_and_xsid;
|
||||
pub mod find_script_actions_and_xsid_script_path;
|
||||
pub mod parse_numbers_from_xsid_script;
|
||||
pub mod verification_token_to_loading_anim;
|
||||
+12
-7
@@ -1,4 +1,5 @@
|
||||
use crate::error::grok_client_error::GrokClientError;
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::VerificationToken;
|
||||
use base64::prelude::BASE64_STANDARD;
|
||||
use base64::Engine;
|
||||
use log::error;
|
||||
@@ -12,21 +13,23 @@ impl LoadingAnim {
|
||||
}
|
||||
}
|
||||
|
||||
/// This arbitrary algorithm is used by Grok.
|
||||
/// See https://github.com/realasfngl/Grok-Api
|
||||
/// The verification token comes from the index HTML.
|
||||
pub fn verification_token_to_loading_anim(verification_token: &str) -> Result<LoadingAnim, GrokClientError> {
|
||||
let decoded_bytes = BASE64_STANDARD.decode(verification_token)
|
||||
pub fn verification_token_to_loading_anim(verification_token: &VerificationToken) -> Result<LoadingAnim, GrokClientError> {
|
||||
// array: list = list(b64decode(verification_token)) -- note: we don't need to convert to a list
|
||||
let decoded_bytes = BASE64_STANDARD.decode(&verification_token.0)
|
||||
.map_err(|err| {
|
||||
error!("Failed to decode verification token: {} ; err = {:?}", verification_token, err);
|
||||
error!("Failed to decode verification token: {} ; err = {:?}", verification_token.0, err);
|
||||
GrokClientError::FailedToDecodeVerificationToken(err)
|
||||
})?;
|
||||
|
||||
// This arbitrary algorithm is used by Grok.
|
||||
// See https://github.com/realasfngl/Grok-Api
|
||||
|
||||
// anim: str = "loading-x-anim-" + str(array[5] % 4)
|
||||
let byte = decoded_bytes.get(5)
|
||||
.map(|byte| *byte)
|
||||
.ok_or(GrokClientError::InvalidVerificationTokenBytes)?;
|
||||
|
||||
// note, we'll put the string construction in the `to_id()` method since we need the raw integer again
|
||||
let anim = (byte % 4) as usize;
|
||||
|
||||
Ok(LoadingAnim(anim))
|
||||
@@ -37,6 +40,7 @@ mod tests {
|
||||
use crate::error::grok_client_error::GrokClientError;
|
||||
use crate::requests::index_page::utils::verification_token_to_loading_anim::verification_token_to_loading_anim;
|
||||
use errors::AnyhowResult;
|
||||
use crate::requests::index_page::index_parsers::parse_index_verification_token::VerificationToken;
|
||||
|
||||
#[test]
|
||||
fn test() -> AnyhowResult<()> {
|
||||
@@ -47,7 +51,8 @@ mod tests {
|
||||
}
|
||||
|
||||
fn to_id(verification_token: &str) -> Result<String, GrokClientError> {
|
||||
let anim = verification_token_to_loading_anim(verification_token)?;
|
||||
let verification_token = VerificationToken(verification_token.to_string());
|
||||
let anim = verification_token_to_loading_anim(&verification_token)?;
|
||||
Ok(anim.to_id())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user