Use a proper user agent string
This commit is contained in:
parent
17941d90be
commit
26e00bae9f
34
src/main.rs
34
src/main.rs
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
use reqwest::StatusCode;
|
use reqwest::{Client, StatusCode};
|
||||||
use rocket::response::content::RawHtml;
|
use rocket::response::content::RawHtml;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
@ -30,6 +30,15 @@ fn default_base_url() -> String {
|
||||||
"/".into()
|
"/".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn user_agent() -> String {
|
||||||
|
format!(
|
||||||
|
"{}/{} (RSS feed converter) on {}",
|
||||||
|
env!("CARGO_PKG_NAME"),
|
||||||
|
env!("CARGO_PKG_VERSION"),
|
||||||
|
&ARGS.domain
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
static ARGS: once_cell::sync::Lazy<Args> = once_cell::sync::Lazy::new(|| Args::parse());
|
static ARGS: once_cell::sync::Lazy<Args> = once_cell::sync::Lazy::new(|| Args::parse());
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -61,8 +70,17 @@ async fn syndication_rss_route(
|
||||||
let project_url = format!("{}{}", COHOST_ACCOUNT_API_URL, project);
|
let project_url = format!("{}{}", COHOST_ACCOUNT_API_URL, project);
|
||||||
let posts_url = cohost_posts_api_url(project, page);
|
let posts_url = cohost_posts_api_url(project, page);
|
||||||
|
|
||||||
|
let client = match Client::builder().user_agent(user_agent()).build() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
let err = format!("Couldn't build a reqwest client: {:?}", e);
|
||||||
|
eprintln!("{}", err);
|
||||||
|
return Err(ErrorResponse::InternalError(err));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
eprintln!("making request to {}", project_url);
|
eprintln!("making request to {}", project_url);
|
||||||
let project_data: CohostAccount = match reqwest::get(project_url).await {
|
let project_data: CohostAccount = match client.get(project_url).send().await {
|
||||||
Ok(v) => match v.status() {
|
Ok(v) => match v.status() {
|
||||||
StatusCode::OK => match v.json::<CohostAccount>().await {
|
StatusCode::OK => match v.json::<CohostAccount>().await {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
|
@ -93,7 +111,7 @@ async fn syndication_rss_route(
|
||||||
};
|
};
|
||||||
|
|
||||||
eprintln!("making request to {}", posts_url);
|
eprintln!("making request to {}", posts_url);
|
||||||
match reqwest::get(posts_url).await {
|
match client.get(posts_url).send().await {
|
||||||
Ok(v) => match v.status() {
|
Ok(v) => match v.status() {
|
||||||
StatusCode::OK => match v.json::<CohostPostsPage>().await {
|
StatusCode::OK => match v.json::<CohostPostsPage>().await {
|
||||||
Ok(page_data) => {
|
Ok(page_data) => {
|
||||||
|
@ -143,10 +161,18 @@ async fn webfinger_route(params: HashMap<String, String>) -> Option<Json<CohostW
|
||||||
);
|
);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
let client = match Client::builder().user_agent(user_agent()).build() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
let err = format!("Couldn't build a reqwest client: {:?}", e);
|
||||||
|
eprintln!("{}", err);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
if let Some(param) = params.iter().next() {
|
if let Some(param) = params.iter().next() {
|
||||||
let url = format!("{}{}", COHOST_ACCOUNT_API_URL, param.0);
|
let url = format!("{}{}", COHOST_ACCOUNT_API_URL, param.0);
|
||||||
eprintln!("making request to {}", url);
|
eprintln!("making request to {}", url);
|
||||||
match reqwest::get(url).await {
|
match client.get(url).send().await {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
match v.status() {
|
match v.status() {
|
||||||
StatusCode::OK => match v.json::<CohostAccount>().await {
|
StatusCode::OK => match v.json::<CohostAccount>().await {
|
||||||
|
|
|
@ -1,12 +1,21 @@
|
||||||
use crate::cohost_account::CohostAccount;
|
use crate::cohost_account::CohostAccount;
|
||||||
use crate::cohost_posts::*;
|
use crate::cohost_posts::*;
|
||||||
use crate::ARGS;
|
use crate::ARGS;
|
||||||
use rss::Channel;
|
use atom_syndication::LinkBuilder;
|
||||||
use rss::extension::atom::{AtomExtensionBuilder, Link};
|
use rss::extension::atom::{AtomExtensionBuilder, Link};
|
||||||
use atom_syndication::LinkBuilder;
|
use rss::Channel;
|
||||||
|
|
||||||
fn rel_link_for(rel: &str, project_name: &str, page_number: u64) -> Link {
|
fn rel_link_for(rel: &str, project_name: &str, page_number: u64) -> Link {
|
||||||
LinkBuilder::default().rel(rel).href(format!("https://{}{}{}/feed.rss?page={}", &ARGS.domain, &ARGS.base_url, project_name.clone(), page_number)).build()
|
LinkBuilder::default()
|
||||||
|
.rel(rel)
|
||||||
|
.href(format!(
|
||||||
|
"https://{}{}{}/feed.rss?page={}",
|
||||||
|
&ARGS.domain,
|
||||||
|
&ARGS.base_url,
|
||||||
|
project_name.clone(),
|
||||||
|
page_number
|
||||||
|
))
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn channel_for_posts_page(
|
pub fn channel_for_posts_page(
|
||||||
|
@ -27,12 +36,14 @@ pub fn channel_for_posts_page(
|
||||||
)))
|
)))
|
||||||
.link(format!(
|
.link(format!(
|
||||||
"https://cohost.org/{}?page={}",
|
"https://cohost.org/{}?page={}",
|
||||||
project_name,
|
project_name, page_number
|
||||||
page_number
|
|
||||||
));
|
));
|
||||||
|
|
||||||
let mut atom = AtomExtensionBuilder::default();
|
let mut atom = AtomExtensionBuilder::default();
|
||||||
let mut links = vec![rel_link_for("self", project_name, page_number), rel_link_for("first", project_name, 0)];
|
let mut links = vec![
|
||||||
|
rel_link_for("self", project_name, page_number),
|
||||||
|
rel_link_for("first", project_name, 0),
|
||||||
|
];
|
||||||
if page_number > 0 {
|
if page_number > 0 {
|
||||||
links.push(rel_link_for("prev", project_name, page_number - 1));
|
links.push(rel_link_for("prev", project_name, page_number - 1));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue