rustfm_scraper/models/user.rs
1use num_format::ToFormattedString;
2use serde::Deserialize;
3
4use crate::utils;
5
6#[derive(Deserialize)]
7pub struct UserResponse {
8 pub user: User,
9}
10
11#[derive(Deserialize)]
12pub struct Registered {
13 pub unixtime: String,
14}
15
16/// Response to a `user.getInfo` request
17///
18/// Returns information about a user's profile. Certain fields, such as `bootstrap` and `images`,
19/// have been omitted from deserialization since they are not used in this application
20#[derive(Deserialize)]
21pub struct User {
22 /// The total number of playlists the user has created
23 playlists: String,
24
25 /// The total number of tracks scrobbled by the user
26 #[serde(rename = "playcount")]
27 play_count: String,
28
29 /// The user's gender
30 pub gender: String,
31
32 /// The user's username
33 pub name: String,
34
35 /// Indicates if the user is a subscriber to Last.fm
36 pub subscriber: String,
37
38 /// The user's profile URL
39 pub url: String,
40
41 /// The user's country
42 pub country: String,
43
44 /// The date and time the user registered their profile, represented as a unix timestamp
45 ///
46 /// See [Registered](struct.Registered.html)
47 pub registered: Registered,
48
49 /// The user's profile type. Could be a normal user or a staff user.
50 #[serde(rename = "type")]
51 pub user_type: String,
52
53 /// The user's age
54 pub age: String,
55 // pub bootstrap: String,
56 /// The user's real name, if provided
57 #[serde(rename = "realname")]
58 pub real_name: String,
59}
60
61impl User {
62 /// Get the number of playlists created by the user
63 pub fn playlists(&self) -> i32 {
64 self.playlists.parse().unwrap()
65 }
66
67 /// Get the number of playlists created by the user, formatted according to the user's system locale
68 pub fn playlists_formatted(&self) -> String {
69 self.playlists().to_formatted_string(&utils::get_locale())
70 }
71
72 /// Get the total number of scrobbles by the user
73 pub fn play_count(&self) -> i32 {
74 self.play_count.parse().unwrap()
75 }
76
77 pub fn play_count_formatted(&self) -> String {
78 let locale = utils::get_locale();
79 self.play_count().to_formatted_string(&locale)
80 }
81}