Skip to content

Commit ae7250d

Browse files
committed
[Twitter] Send requests with mocked user agent
1 parent fdbb209 commit ae7250d

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/helper.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{convert::identity, path::Path, time::Duration};
1+
use std::{path::Path, time::Duration};
22

33
use anyhow::{anyhow, ensure};
44
use humantime_serde::re::humantime;
@@ -8,20 +8,21 @@ use tokio::process::Command;
88
use crate::prop;
99

1010
pub fn reqwest_client() -> anyhow::Result<reqwest::Client> {
11-
reqwest_client_with(identity)
11+
reqwest_client_with(|b, _| b)
1212
}
1313

1414
pub fn reqwest_client_with(
15-
configure: impl FnOnce(reqwest::ClientBuilder) -> reqwest::ClientBuilder,
15+
configure: impl FnOnce(reqwest::ClientBuilder, &mut HeaderMap) -> reqwest::ClientBuilder,
1616
) -> anyhow::Result<reqwest::Client> {
17+
let mut headers = HeaderMap::from_iter([(
18+
header::USER_AGENT,
19+
HeaderValue::from_str(&prop::UserAgent::Logo.as_str()).unwrap(),
20+
)]);
1721
configure(
18-
reqwest::ClientBuilder::new()
19-
.timeout(Duration::from_secs(60) * 3)
20-
.default_headers(HeaderMap::from_iter([(
21-
header::USER_AGENT,
22-
HeaderValue::from_str(&prop::UserAgent::Logo.as_str()).unwrap(),
23-
)])),
22+
reqwest::ClientBuilder::new().timeout(Duration::from_secs(60) * 3),
23+
&mut headers,
2424
)
25+
.default_headers(headers)
2526
.build()
2627
.map_err(|err| anyhow!("failed to build reqwest client: {err}"))
2728
}

src/platform/bilibili/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod source;
22

3-
use reqwest::header::{self, HeaderMap, HeaderValue};
3+
use reqwest::header::{self, HeaderValue};
44
use serde::Deserialize;
55

66
use crate::{
@@ -37,11 +37,12 @@ fn upgrade_to_https(url: &str) -> String {
3737
}
3838

3939
fn bilibili_request_builder() -> anyhow::Result<reqwest::Client> {
40-
helper::reqwest_client_with(|builder| {
41-
builder.default_headers(HeaderMap::from_iter([(
40+
helper::reqwest_client_with(|builder, headers| {
41+
headers.insert(
4242
header::USER_AGENT,
4343
HeaderValue::from_str(&prop::UserAgent::LogoDynamic.as_str()).unwrap(),
44-
)]))
44+
);
45+
builder
4546
})
4647
}
4748

src/platform/twitter/request.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use std::str::FromStr;
22

33
use anyhow::{anyhow, ensure};
4-
use reqwest::{header::COOKIE, Url};
4+
use reqwest::{
5+
header::{HeaderValue, COOKIE, USER_AGENT},
6+
Url,
7+
};
58
use serde_json::{self as json, json};
69

7-
use crate::helper;
10+
use crate::{helper, prop};
811

912
pub struct TwitterCookies {
1013
pub raw: String,
@@ -145,7 +148,7 @@ impl TwitterRequester {
145148
}
146149

147150
async fn request(&self, url: impl AsRef<str>) -> anyhow::Result<reqwest::Response> {
148-
let resp = helper::reqwest_client()?
151+
let resp = twitter_request_builder()?
149152
.get(url.as_ref())
150153
.bearer_auth(BEARER_TOKEN)
151154
.header(COOKIE, &self.cookies.raw)
@@ -164,4 +167,14 @@ impl TwitterRequester {
164167
}
165168
}
166169

170+
fn twitter_request_builder() -> anyhow::Result<reqwest::Client> {
171+
helper::reqwest_client_with(|builder, headers| {
172+
headers.insert(
173+
USER_AGENT,
174+
HeaderValue::from_str(&prop::UserAgent::Mocked.as_str()).unwrap(),
175+
);
176+
builder
177+
})
178+
}
179+
167180
const BEARER_TOKEN: &str = "AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA";

0 commit comments

Comments
 (0)