summaryrefslogtreecommitdiff
path: root/examples/playlists/main.rs
blob: 81d3cf28109b87b7e2cc5c999c0e663a956fe898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::error::Error;

use anyhow::Context;
use config::Config;
use deez::auth::AuthPromptHandler;
use deez::client::DeezerClient;
use serde::Deserialize;

#[derive(Deserialize)]
struct Settings
{
    app_id: u32,
    secret_key: String,
    address: String,
    port: u16,
    uri_scheme: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>>
{
    let settings = Config::builder()
        .add_source(config::File::with_name("examples/playlists/Settings"))
        .build()
        .with_context(|| "Failed to read settings")?
        .try_deserialize::<Settings>()
        .with_context(|| "Invalid settings")?;

    let auth_code_prompt_info = AuthPromptHandler::run(
        settings.app_id,
        settings.address,
        settings.port,
        settings.uri_scheme,
    )
    .await?;

    println!("{}", auth_code_prompt_info.auth_prompt_url);

    let auth_code = auth_code_prompt_info.handler.await??;

    println!("Retrieved authentication code '{}'", auth_code);

    let client = DeezerClient::new();

    let access_token = client
        .get_access_token(settings.app_id, settings.secret_key, auth_code)
        .await?;

    println!(
        "Retrieved access token '{}' which expires in {} seconds",
        access_token.access_token,
        access_token.expires.as_secs()
    );

    let me = client.get_me(access_token.clone()).await?;

    println!("{:#?}", me);

    let playlists = client.get_user_playlists(me.id, access_token).await?;

    println!("Playlists: {:#?}", playlists);

    Ok(())
}