Compare commits

..

No commits in common. "b3bf3f62a7f180c639cf32ee4b0013958c298c50" and "b2a72dd067786d58212a46c744043010866f29c4" have entirely different histories.

6 changed files with 5 additions and 110 deletions

View file

@ -2,7 +2,6 @@
name = "codegen" name = "codegen"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
publish = false
[dependencies] [dependencies]
reqwest = "0.11.11" reqwest = "0.11.11"

View file

@ -12,12 +12,6 @@ pub enum Error {
/// Website parsing error /// Website parsing error
#[error("website parsing error: {0}")] #[error("website parsing error: {0}")]
WebsiteParsing(Cow<'static, str>), WebsiteParsing(Cow<'static, str>),
/// Item not found on Bandcamp
#[error("{0}")]
NotFound(Cow<'static, str>),
/// Other error from Bandcamp
#[error("{0}")]
Bandcamp(Cow<'static, str>),
#[error("invalid bandcamp url")] #[error("invalid bandcamp url")]
InvalidUrl, InvalidUrl,
#[error("invalid album uid")] #[error("invalid album uid")]

View file

@ -64,13 +64,6 @@ pub struct Track {
pub band_name: Option<String>, pub band_name: Option<String>,
/// Duration in seconds /// Duration in seconds
pub duration: f32, pub duration: f32,
/// Map of audio formats and URLs.
///
/// 128kbit/s MP3 is currently the only publicly available format.
///
/// **Example:**
///
/// `mp3-128` => `https://bandcamp.com/stream_redirect?enc=mp3-128&track_id=4173325157&ts=1686871135&t=a1f7904205e6caca970009ca6744ef0b512f353a`
pub streaming_url: BTreeMap<String, String>, pub streaming_url: BTreeMap<String, String>,
pub track_num: u16, pub track_num: u16,
} }

View file

@ -38,28 +38,6 @@ struct SearchResultWrapper {
auto: SearchResult, auto: SearchResult,
} }
#[derive(Deserialize)]
#[serde(untagged)]
enum ResponseWrapper<T> {
Ok(T),
Error { error_message: String },
}
impl<T> ResponseWrapper<T> {
fn convert(self) -> Result<T> {
match self {
ResponseWrapper::Ok(data) => Ok(data),
ResponseWrapper::Error { error_message } => {
if error_message.starts_with("No such ") || error_message.ends_with(" not found") {
Err(Error::NotFound(error_message.into()))
} else {
Err(Error::Bandcamp(error_message.into()))
}
}
}
}
}
impl Bandcamp { impl Bandcamp {
pub async fn band(&self, band_id: u64) -> Result<Band> { pub async fn band(&self, band_id: u64) -> Result<Band> {
let req = BandRequest { band_id }; let req = BandRequest { band_id };
@ -72,15 +50,9 @@ impl Bandcamp {
.await? .await?
.error_for_status()?; .error_for_status()?;
resp.json::<ResponseWrapper<Band>>().await?.convert() Ok(resp.json().await?)
} }
/// Get a album (or a track) from the Bandcamp API
///
/// # Parameters
/// - `band_id` The Band ID of the album/track
/// - `tralbum_id` Album/Track ID
/// - `tralbum_type` Track/Album
pub async fn album( pub async fn album(
&self, &self,
band_id: u64, band_id: u64,
@ -101,7 +73,7 @@ impl Bandcamp {
.await? .await?
.error_for_status()?; .error_for_status()?;
resp.json::<ResponseWrapper<Album>>().await?.convert() Ok(resp.json().await?)
} }
pub async fn album_uid<S: AsRef<str>>(&self, uid: S) -> Result<Album> { pub async fn album_uid<S: AsRef<str>>(&self, uid: S) -> Result<Album> {
@ -131,11 +103,7 @@ impl Bandcamp {
.await? .await?
.error_for_status()?; .error_for_status()?;
Ok(resp Ok(resp.json::<SearchResultWrapper>().await?.auto)
.json::<ResponseWrapper<SearchResultWrapper>>()
.await?
.convert()?
.auto)
} }
pub async fn feed(&self) -> Result<Feed> { pub async fn feed(&self) -> Result<Feed> {
@ -149,7 +117,7 @@ impl Bandcamp {
.await? .await?
.error_for_status()?; .error_for_status()?;
resp.json::<ResponseWrapper<Feed>>().await?.convert() Ok(resp.json().await?)
} }
pub async fn feed_cont<S: AsRef<str>>(&self, token: S) -> Result<Feed> { pub async fn feed_cont<S: AsRef<str>>(&self, token: S) -> Result<Feed> {
@ -166,7 +134,7 @@ impl Bandcamp {
.await? .await?
.error_for_status()?; .error_for_status()?;
resp.json::<ResponseWrapper<Feed>>().await?.convert() Ok(resp.json().await?)
} }
pub async fn band_id_from_url<U: IntoUrl>(&self, url: U) -> Result<u64> { pub async fn band_id_from_url<U: IntoUrl>(&self, url: U) -> Result<u64> {

View file

@ -1,44 +0,0 @@
---
source: tests/tests.rs
assertion_line: 69
expression: "&album"
---
Album(
id: 716010980,
title: "All Was Well",
art_id: Some(2367528067),
type: t,
tralbum_artist: "Wintergatan",
band: BandInfo(
band_id: 2464198920,
name: "Wintergatan",
bio: "",
image_id: Some(1354613),
location: "Gothenburg, Sweden",
),
bandcamp_url: "https://wintergatan.bandcamp.com/track/all-was-well",
credits: None,
about: None,
featured_track_id: 716010980,
release_date: 1366761600,
tags: [
Tag(
name: "Pop",
norm_name: "pop",
isloc: false,
),
],
tracks: [
Track(
track_id: 716010980,
title: "All Was Well",
album_id: 2572654767,
album_title: None,
band_id: 2464198920,
band_name: Some("Wintergatan"),
duration: 183.224,
streaming_url: "[streaming_url]",
track_num: 8,
),
],
)

View file

@ -58,21 +58,6 @@ async fn album2() {
check_image(&album.img_url().unwrap()).await; check_image(&album.img_url().unwrap()).await;
} }
#[tokio::test]
async fn album_track() {
let bc = Bandcamp::new();
let album = bc
.album_uid(format!("{}t{}", BAND_WINTERGATAN, 716010980))
.await
.unwrap();
insta::assert_ron_snapshot!(&album, {
".tracks[].streaming_url" => "[streaming_url]"
});
check_stream_urls(&album.tracks);
check_image(&album.img_url().unwrap()).await;
}
#[tokio::test] #[tokio::test]
async fn search() { async fn search() {
let bc = Bandcamp::new(); let bc = Bandcamp::new();