aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-23 13:58:03 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-23 13:58:03 +0200
commit960007b06e2b47d41f88365c26f043f61c817f08 (patch)
tree165dd8deaa294ed2d4a9719c41dd66a02261c1f2
parent5d2145406c4f2e11b0cde06f5f12c9d16ab51ded (diff)
downloadjellything-960007b06e2b47d41f88365c26f043f61c817f08.tar
jellything-960007b06e2b47d41f88365c26f043f61c817f08.tar.bz2
jellything-960007b06e2b47d41f88365c26f043f61c817f08.tar.zst
import: send user-agent header to all apis
-rw-r--r--import/src/acoustid.rs15
-rw-r--r--import/src/lib.rs7
-rw-r--r--import/src/musicbrainz.rs44
-rw-r--r--import/src/tmdb.rs15
-rw-r--r--import/src/trakt.rs5
5 files changed, 78 insertions, 8 deletions
diff --git a/import/src/acoustid.rs b/import/src/acoustid.rs
index 49c5e38..5692674 100644
--- a/import/src/acoustid.rs
+++ b/import/src/acoustid.rs
@@ -3,6 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
+use crate::USER_AGENT;
use anyhow::Result;
use bincode::{Decode, Encode};
use jellybase::cache::async_cache_memory;
@@ -56,10 +57,16 @@ pub(crate) struct AcoustIDLookupResponse {
impl AcoustID {
pub fn new(api_key: &str) -> Self {
let client = ClientBuilder::new()
- .default_headers(HeaderMap::from_iter([(
- HeaderName::from_static("accept"),
- HeaderValue::from_static("application/json"),
- )]))
+ .default_headers(HeaderMap::from_iter([
+ (
+ HeaderName::from_static("accept"),
+ HeaderValue::from_static("application/json"),
+ ),
+ (
+ HeaderName::from_static("user-agent"),
+ HeaderValue::from_static(USER_AGENT),
+ ),
+ ]))
.build()
.unwrap();
Self {
diff --git a/import/src/lib.rs b/import/src/lib.rs
index bd01fc9..b45361a 100644
--- a/import/src/lib.rs
+++ b/import/src/lib.rs
@@ -38,9 +38,16 @@ use trakt::Trakt;
pub mod acoustid;
pub mod infojson;
+pub mod musicbrainz;
pub mod tmdb;
pub mod trakt;
+pub const USER_AGENT: &'static str = concat!(
+ "jellything/",
+ env!("CARGO_PKG_VERSION"),
+ " ( https://codeberg.org/metamuffin/jellything )"
+);
+
static IMPORT_SEM: LazyLock<Semaphore> = LazyLock::new(|| Semaphore::new(1));
pub static IMPORT_ERRORS: RwLock<Vec<String>> = RwLock::const_new(Vec::new());
diff --git a/import/src/musicbrainz.rs b/import/src/musicbrainz.rs
new file mode 100644
index 0000000..059b8f5
--- /dev/null
+++ b/import/src/musicbrainz.rs
@@ -0,0 +1,44 @@
+/*
+ This file is part of jellything (https://codeberg.org/metamuffin/jellything)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2025 metamuffin <metamuffin.org>
+*/
+
+use reqwest::{
+ header::{HeaderMap, HeaderName, HeaderValue},
+ Client, ClientBuilder,
+};
+use std::sync::Arc;
+use tokio::sync::Semaphore;
+use crate::USER_AGENT;
+
+pub struct MusicBrainz {
+ client: Client,
+ key: String,
+ rate_limit: Arc<Semaphore>,
+}
+
+impl MusicBrainz {
+ pub fn new(api_key: &str) -> Self {
+ let client = ClientBuilder::new()
+ .default_headers(HeaderMap::from_iter([
+ (
+ HeaderName::from_static("accept"),
+ HeaderValue::from_static("application/json"),
+ ),
+ (
+ HeaderName::from_static("user-agent"),
+ HeaderValue::from_static(USER_AGENT),
+ ),
+ ]))
+ .build()
+ .unwrap();
+ Self {
+ client,
+ // send at most 1 req/s according to musicbrainz docs, each lock is held for 10s
+ // this implementation also never sends more than 10 requests in-flight.
+ rate_limit: Arc::new(Semaphore::new(10)),
+ key: api_key.to_owned(),
+ }
+ }
+}
diff --git a/import/src/tmdb.rs b/import/src/tmdb.rs
index 3b3e7ed..51748f8 100644
--- a/import/src/tmdb.rs
+++ b/import/src/tmdb.rs
@@ -3,6 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
+use crate::USER_AGENT;
use anyhow::{anyhow, bail, Context};
use bincode::{Decode, Encode};
use jellybase::{
@@ -30,10 +31,16 @@ pub struct Tmdb {
impl Tmdb {
pub fn new(api_key: &str) -> Self {
let client = ClientBuilder::new()
- .default_headers(HeaderMap::from_iter([(
- HeaderName::from_static("accept"),
- HeaderValue::from_static("application/json"),
- )]))
+ .default_headers(HeaderMap::from_iter([
+ (
+ HeaderName::from_static("accept"),
+ HeaderValue::from_static("application/json"),
+ ),
+ (
+ HeaderName::from_static("user-agent"),
+ HeaderValue::from_static(USER_AGENT),
+ ),
+ ]))
.build()
.unwrap();
let image_client = ClientBuilder::new().build().unwrap();
diff --git a/import/src/trakt.rs b/import/src/trakt.rs
index 2f8618d..86f2f42 100644
--- a/import/src/trakt.rs
+++ b/import/src/trakt.rs
@@ -3,6 +3,7 @@
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
+use crate::USER_AGENT;
use anyhow::Context;
use bincode::{Decode, Encode};
use jellybase::{
@@ -37,6 +38,10 @@ impl Trakt {
HeaderName::from_static("content-type"),
HeaderValue::from_static("application/json"),
),
+ (
+ HeaderName::from_static("user-agent"),
+ HeaderValue::from_static(USER_AGENT),
+ ),
]))
.build()
.unwrap();