mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 09:28:11 +00:00
path and data
This commit is contained in:
1 parent
4453b1a774
commit
10428cd182
2 files changed
+32
-66
No files matched your search
@@ -1,10 +1,11 @@
|
||||
use crate::error::grok_client_error::GrokClientError;
|
||||
use crate::error::grok_error::GrokError;
|
||||
use crate::requests::index_page::requests::get_index_page_script_with_client::{get_index_page_script_with_client, GetIndexPageScriptArgs};
|
||||
use crate::requests::index_page::utils::parse_numbers_from_xsid_script::parse_numbers_from_xsid_script;
|
||||
use crate::requests::index_page::utils::parse_on_demand_script_from_index_html::parse_on_demand_script_from_index_html;
|
||||
use crate::requests::index_page::utils::parse_svg_paths_from_index_html::parse_svg_paths_from_index_html;
|
||||
use crate::requests::index_page::utils::verification_token_to_loading_anim::LoadingAnim;
|
||||
use wreq::Client;
|
||||
use crate::requests::index_page::utils::parse_svg_paths_from_index_html::parse_svg_paths_from_index_html;
|
||||
// self.svg_data, self.numbers = Parser.parse_values(c_request.text, self.anim, self.xsid_script)
|
||||
|
||||
|
||||
@@ -19,6 +20,9 @@ pub struct GetNumbersArgs<'a> {
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NumbersAndSvg {
|
||||
pub numbers: Vec<u32>,
|
||||
|
||||
/// We selected one of four possible SVG paths (of length >= 200) by the animation index.
|
||||
pub svg_data: String,
|
||||
}
|
||||
|
||||
pub async fn get_numbers(args: GetNumbersArgs<'_>) -> Result<NumbersAndSvg, GrokError> {
|
||||
@@ -33,65 +37,26 @@ pub async fn get_numbers(args: GetNumbersArgs<'_>) -> Result<NumbersAndSvg, Grok
|
||||
_ => format!("https://grok.com/_next/{}", args.xsid_script_id),
|
||||
};
|
||||
|
||||
println!("Script link: `{}`", script_link);
|
||||
|
||||
let xsid_script_body = get_index_page_script_with_client(GetIndexPageScriptArgs {
|
||||
client: &args.client,
|
||||
cookie: args.cookie,
|
||||
script_url: &script_link,
|
||||
}).await?;
|
||||
|
||||
println!("Xsid Script Body:\n\n");
|
||||
println!("{}", xsid_script_body);
|
||||
println!("\n\n");
|
||||
|
||||
let numbers = parse_numbers_from_xsid_script(&xsid_script_body);
|
||||
|
||||
println!("Numbers:\n\n");
|
||||
println!("{:?}", numbers);
|
||||
println!("\n\n");
|
||||
|
||||
let paths = parse_svg_paths_from_index_html(&args.html);
|
||||
|
||||
println!("Paths:\n\n");
|
||||
println!("{:?}", paths);
|
||||
println!("\n\n");
|
||||
|
||||
/*
|
||||
@staticmethod
|
||||
def parse_values(html: str, loading: str = "loading-x-anim-0", scriptId: str = "") -> tuple[str, Optional[str]]:
|
||||
|
||||
Parser._load__xsid_mapping()
|
||||
|
||||
all_d_values = findall(r'"d":"(M[^"]{200,})"', html)
|
||||
svg_data = all_d_values[int(loading.split("loading-x-anim-")[1])]
|
||||
|
||||
if scriptId:
|
||||
|
||||
if scriptId == "ondemand.s":
|
||||
script_link: str = 'https://abs.twimg.com/responsive-web/client-web/ondemand.s.' + Utils.between(html, f'"{scriptId}":"', '"') + 'a.js'
|
||||
else:
|
||||
script_link: str = f'https://grok.com/_next/{scriptId}'
|
||||
|
||||
if script_link in Parser.mapping:
|
||||
numbers: list = Parser.mapping[script_link]
|
||||
|
||||
else:
|
||||
script_content: str = requests.get(script_link, impersonate="chrome136").text
|
||||
numbers: list = [int(x) for x in findall(r'x\[(\d+)\]\s*,\s*16', script_content)]
|
||||
Parser.mapping[script_link] = numbers
|
||||
with open('core/mapping.json', 'w') as f:
|
||||
dump(Parser.mapping, f)
|
||||
|
||||
return svg_data, numbers
|
||||
|
||||
else:
|
||||
return svg_data
|
||||
*/
|
||||
let svg_data = paths.get(args.loading_anim.0);
|
||||
|
||||
let svg_data = match svg_data {
|
||||
Some(s) => s.to_string(),
|
||||
None => return Err(GrokClientError::ScriptLogicOutOfDate.into()),
|
||||
};
|
||||
|
||||
Ok(NumbersAndSvg {
|
||||
numbers,
|
||||
svg_data,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -137,19 +102,6 @@ mod tests {
|
||||
|
||||
println!("Final Numbers: {:?}", result);
|
||||
|
||||
println!("{:?}", result);
|
||||
|
||||
println!("Body:\n\n");
|
||||
println!("{}", page_and_scripts.body);
|
||||
println!("\n\n");
|
||||
|
||||
/*
|
||||
ActionsAndXsid {
|
||||
actions: ["7f09b4b48933c332d0ed2689be265eb736e75c76d9", "7fa957f192ada06e391443f7bcee666006d78d916f", "7f605b51836e8b9088638bb9991ed75227bdedefd2"],
|
||||
xsid_script_path: "static/chunks/0db7b1a346e93e5b.js"
|
||||
}
|
||||
*/
|
||||
|
||||
assert_eq!(1, 2);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+21
-7
@@ -4,7 +4,13 @@ use base64::Engine;
|
||||
use log::error;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LoadingAnim(pub(crate) String);
|
||||
pub struct LoadingAnim(pub(crate) usize);
|
||||
|
||||
impl LoadingAnim {
|
||||
pub fn to_id(&self) -> String {
|
||||
format!("loading-x-anim-{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// The verification token comes from the index HTML.
|
||||
pub fn verification_token_to_loading_anim(verification_token: &str) -> Result<LoadingAnim, GrokClientError> {
|
||||
@@ -21,19 +27,27 @@ pub fn verification_token_to_loading_anim(verification_token: &str) -> Result<Lo
|
||||
.map(|byte| *byte)
|
||||
.ok_or(GrokClientError::InvalidVerificationTokenBytes)?;
|
||||
|
||||
let anim = byte % 4;
|
||||
let anim = (byte % 4) as usize;
|
||||
|
||||
Ok(LoadingAnim(format!("loading-x-anim-{}", anim)))
|
||||
Ok(LoadingAnim(anim))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
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;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
assert_eq!(&verification_token_to_loading_anim("aezWdJW6+WJhvtMA1faxopO11Q1JRcM6j7JcW/R0gO09CQhO7GzIs760yo1A0MnF").unwrap().0, "loading-x-anim-2");
|
||||
assert_eq!(&verification_token_to_loading_anim("wROL4+VivbPuVc37N+4VNq0K3cEMW0bPKuToO6JWxJ9fzsP3Rsn2eC8DDDZFFlov").unwrap().0, "loading-x-anim-2");
|
||||
assert_eq!(&verification_token_to_loading_anim("ckWUvquxT8AdPRpdjXTeNkkXRfKiGGnQFH3Hq4aznsUrSsjtDSI4JcO59j7U/+VQ").unwrap().0, "loading-x-anim-1");
|
||||
fn test() -> AnyhowResult<()> {
|
||||
assert_eq!(&to_id("aezWdJW6+WJhvtMA1faxopO11Q1JRcM6j7JcW/R0gO09CQhO7GzIs760yo1A0MnF")?, "loading-x-anim-2");
|
||||
assert_eq!(&to_id("wROL4+VivbPuVc37N+4VNq0K3cEMW0bPKuToO6JWxJ9fzsP3Rsn2eC8DDDZFFlov")?, "loading-x-anim-2");
|
||||
assert_eq!(&to_id("ckWUvquxT8AdPRpdjXTeNkkXRfKiGGnQFH3Hq4aznsUrSsjtDSI4JcO59j7U/+VQ")?, "loading-x-anim-1");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn to_id(verification_token: &str) -> Result<String, GrokClientError> {
|
||||
let anim = verification_token_to_loading_anim(verification_token)?;
|
||||
Ok(anim.to_id())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user