Skip to content

Commit 6ddf8ac

Browse files
committedApr 14, 2020
Sixth Pre-Release. Still a long way...
- Implemented Tag Methods. - Fixed Tag attributes. - Updated the changelog. - Added tag example. - Updated the README.
1 parent cb72fd2 commit 6ddf8ac

File tree

6 files changed

+273
-8
lines changed

6 files changed

+273
-8
lines changed
 

‎CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 0.1.5
2+
#### Sixth Pre-Release. Still a long way...
3+
4+
- Implemented Tag Methods.
5+
- Fixed Tag attributes.
6+
- Updated the changelog.
7+
- Added tag example.
8+
- Updated the README.
9+
110
## 0.1.4
211
#### Fifth Pre-Release. Still a long way...
312

‎example/tag_methods_example.dart

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import 'package:scrobblenaut/lastfm.dart';
2+
import 'package:scrobblenaut/scrobblenaut.dart';
3+
4+
import 'api_values.dart';
5+
6+
// Just an example of use.
7+
void main() async {
8+
print('####################################################################');
9+
10+
final lastFMAuth = await LastFM.authenticate(
11+
apiKey: APIValues.API,
12+
apiSecret: APIValues.secret,
13+
username: APIValues.username,
14+
password: APIValues.password,
15+
sessionKey: APIValues.sessionKey,
16+
);
17+
18+
final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);
19+
20+
print('############################tag.getInfo#############################');
21+
22+
// tag.getInfo
23+
final tagGetInfo = (await scrobblenaut.tag.getInfo(tag: 'anime'));
24+
print('Tag Name: ${tagGetInfo.name} | Tag total: ${tagGetInfo.total}');
25+
26+
print('############################tag.getSimilar##########################');
27+
28+
// tag.getSimilar
29+
(await scrobblenaut.tag.getSimilar(tag: 'anime'))?.forEach((Tag tag) {
30+
print('Similar Tag Name: ${tag.name}');
31+
});
32+
33+
print('##########################tag.getTopAlbums##########################');
34+
35+
// tag.getTopAlbums
36+
(await scrobblenaut.tag.getTopAlbums(tag: 'anime'))?.forEach((Album album) {
37+
print('Top Album Name: ${album.name}');
38+
});
39+
40+
print('########################tag.getTopArtists###########################');
41+
42+
// tag.getTopArtists
43+
(await scrobblenaut.tag.getTopArtists(tag: 'anime'))
44+
?.forEach((Artist artist) {
45+
print('Top Artist Name: ${artist.name}');
46+
});
47+
48+
print('##########################tag.getTopTags############################');
49+
50+
// tag.getTopTags
51+
(await scrobblenaut.tag.getTopTags())?.forEach((Tag tag) {
52+
print('Top Tag Name: ${tag.name}');
53+
});
54+
55+
print('########################tag.getTopTracks############################');
56+
57+
// tag.getTopTracks
58+
(await scrobblenaut.tag.getTopTracks(tag: 'anime'))?.forEach((Track track) {
59+
print('Top Track Name: ${track.name} | Top Track URL: ${track.url}');
60+
});
61+
62+
print('#######################tag.getWeeklyChartList#######################');
63+
64+
// tag.getWeeklyChartList
65+
(await scrobblenaut.tag.getWeeklyChartList(tag: 'anime'))
66+
?.forEach((Chart chart) {
67+
print('Chart FromDate: ${chart.fromDate} | Chart ToDate: ${chart.toDate}');
68+
});
69+
70+
print('####################################################################');
71+
}

‎lib/src/lastfm/tag.dart

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@ class Tag {
2020
@JsonKey(name: 'count')
2121
int count;
2222

23-
// TODO: what's this?
24-
/// The total number of usage of this tag.
23+
/// The total number of usage of this tag from a user.
2524
@JsonKey(name: 'total', fromJson: LastFMValueNormalizer.NumberToInt)
2625
int total;
2726

28-
// TODO: what's this?
27+
/// The total number of usage of this tag.
2928
@JsonKey(name: 'reach', fromJson: LastFMValueNormalizer.NumberToInt)
3029
int reach;
3130

32-
// TODO: what's this?
3331
/// The number of usage applied from a user.
3432
@JsonKey(name: 'taggings', fromJson: LastFMValueNormalizer.NumberToInt)
3533
int taggings;
3634

37-
// TODO: another incoherency? is bool? 1 / 0
38-
@JsonKey(name: 'streamable')
39-
String streamable;
35+
/// If True, this tag can be used as a Radio Station.
36+
@JsonKey(name: 'streamable', fromJson: LastFMValueNormalizer.NumberToBool)
37+
bool streamable;
4038

4139
/// The wiki of the tag.
4240
@JsonKey(name: 'wiki')

‎lib/src/methods/tag_methods.dart

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/**
2+
* Scrobblenaut - A deadly simple Last.FM API Wrapper for Dart.
3+
* Copyright (c) 2020 Nebulino
4+
*/
5+
6+
import 'package:meta/meta.dart';
7+
import 'package:scrobblenaut/lastfm.dart';
8+
import 'package:scrobblenaut/src/core/lastfm.dart';
9+
import 'package:scrobblenaut/src/core/request.dart';
10+
import 'package:scrobblenaut/src/core/request_mode.dart';
11+
12+
/// This contains all the methods about a [Tag].
13+
class TagMethods {
14+
final LastFM _api;
15+
16+
TagMethods(this._api);
17+
18+
/// Get the metadata for a tag.
19+
///
20+
/// https://www.last.fm/api/show/tag.getInfo
21+
Future<Tag> getInfo({
22+
@required String tag,
23+
Language lang = Language.en,
24+
}) async {
25+
final parameters = {
26+
'tag': tag,
27+
'lang': lang?.code,
28+
};
29+
30+
final request =
31+
Request(api: _api, method: 'tag.getInfo', parameters: parameters);
32+
33+
return (Tag.fromJson((await request.send(mode: RequestMode.GET))['tag']));
34+
}
35+
36+
/// Search for tags similar to this one.
37+
/// Returns tags ranked by similarity, based on listening data.
38+
///
39+
/// https://www.last.fm/api/show/tag.getSimilar
40+
Future<List<Tag>> getSimilar({
41+
@required String tag,
42+
}) async {
43+
final parameters = {
44+
'tag': tag,
45+
};
46+
47+
final request =
48+
Request(api: _api, method: 'tag.getSimilar', parameters: parameters);
49+
50+
final response = await request.send(mode: RequestMode.GET);
51+
52+
final similarTags = response['similartags']['tag'];
53+
54+
return similarTags == null
55+
? null
56+
: List.generate(
57+
(similarTags as List).length, (i) => Tag.fromJson(similarTags[i]));
58+
}
59+
60+
/// Get the top albums tagged by this tag, ordered by tag count.
61+
///
62+
/// https://www.last.fm/api/show/tag.getTopAlbums
63+
Future<List<Album>> getTopAlbums({
64+
@required String tag,
65+
int page = 1,
66+
int limit = 50,
67+
}) async {
68+
final parameters = {
69+
'tag': tag,
70+
'page': page,
71+
'limit': limit,
72+
};
73+
74+
final request =
75+
Request(api: _api, method: 'tag.getTopAlbums', parameters: parameters);
76+
77+
final response = await request.send(mode: RequestMode.GET);
78+
79+
final topAlbums = response['albums']['album'];
80+
81+
return topAlbums == null
82+
? null
83+
: List.generate(
84+
(topAlbums as List).length, (i) => Album.fromJson(topAlbums[i]));
85+
}
86+
87+
/// Get the top artists tagged by this tag, ordered by tag count.
88+
///
89+
/// https://www.last.fm/api/show/tag.getTopArtists
90+
Future<List<Artist>> getTopArtists({
91+
@required String tag,
92+
int page = 1,
93+
int limit = 50,
94+
}) async {
95+
final parameters = {
96+
'tag': tag,
97+
'page': page,
98+
'limit': limit,
99+
};
100+
101+
final request =
102+
Request(api: _api, method: 'tag.getTopArtists', parameters: parameters);
103+
104+
final response = await request.send(mode: RequestMode.GET);
105+
106+
final topArtists = response['topartists']['artist'];
107+
108+
return topArtists == null
109+
? null
110+
: List.generate(
111+
(topArtists as List).length, (i) => Artist.fromJson(topArtists[i]));
112+
}
113+
114+
/// Fetches the top global tags on Last.fm,
115+
/// sorted by popularity (number of times used).
116+
///
117+
/// https://www.last.fm/api/show/tag.getTopTags
118+
Future<List<Tag>> getTopTags() async {
119+
final request = Request(api: _api, method: 'tag.getTopTags');
120+
121+
final response = await request.send(mode: RequestMode.GET);
122+
123+
final topTags = response['toptags']['tag'];
124+
125+
return topTags == null
126+
? null
127+
: List.generate(
128+
(topTags as List).length, (i) => Tag.fromJson(topTags[i]));
129+
}
130+
131+
/// Get the top tracks tagged by this tag, ordered by tag count.
132+
///
133+
/// https://www.last.fm/api/show/tag.getTopTracks
134+
Future<List<Track>> getTopTracks({
135+
@required String tag,
136+
int page = 1,
137+
int limit = 50,
138+
}) async {
139+
final parameters = {
140+
'tag': tag,
141+
'page': page,
142+
'limit': limit,
143+
};
144+
145+
final request =
146+
Request(api: _api, method: 'tag.getTopTracks', parameters: parameters);
147+
148+
final response = await request.send(mode: RequestMode.GET);
149+
150+
final topTracks = response['tracks']['track'];
151+
152+
return topTracks == null
153+
? null
154+
: List.generate(
155+
(topTracks as List).length, (i) => Track.fromJson(topTracks[i]));
156+
}
157+
158+
/// Get a list of available charts for this tag,
159+
/// expressed as date ranges which can be sent to the chart services.
160+
///
161+
/// https://www.last.fm/api/show/tag.getWeeklyChartList.
162+
Future<List<Chart>> getWeeklyChartList({
163+
@required String tag,
164+
}) async {
165+
final parameters = {
166+
'tag': tag,
167+
};
168+
169+
final request = Request(
170+
api: _api, method: 'tag.getWeeklyChartList', parameters: parameters);
171+
172+
final response = await request.send(mode: RequestMode.GET);
173+
174+
final chartList = response['weeklychartlist']['chart'];
175+
176+
return chartList == null
177+
? null
178+
: List.generate(
179+
(chartList as List).length, (i) => Chart.fromJson(chartList[i]));
180+
}
181+
}

‎lib/src/scrobblenaut.dart

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:scrobblenaut/src/methods/artist_methods.dart';
1010
import 'package:scrobblenaut/src/methods/chart_methods.dart';
1111
import 'package:scrobblenaut/src/methods/geo_methods.dart';
1212
import 'package:scrobblenaut/src/methods/library_methods.dart';
13+
import 'package:scrobblenaut/src/methods/tag_methods.dart';
1314

1415
/// This connects all the various methods [LastFM] can provide.
1516
///
@@ -22,13 +23,15 @@ class Scrobblenaut {
2223
ChartMethods _chartMethods;
2324
GeoMethods _geoMethods;
2425
LibraryMethods _libraryMethods;
26+
TagMethods _tagMethods;
2527

2628
Scrobblenaut._(this._api) {
2729
_albumMethods = AlbumMethods(_api);
2830
_artistMethods = ArtistMethods(_api);
2931
_chartMethods = ChartMethods(_api);
3032
_geoMethods = GeoMethods(_api);
3133
_libraryMethods = LibraryMethods(_api);
34+
_tagMethods = TagMethods(_api);
3235
}
3336

3437
/// It creates a Scrobblenaut Session using a LastFM object.
@@ -51,4 +54,7 @@ class Scrobblenaut {
5154

5255
/// Use this to use [Library]'s methods.
5356
LibraryMethods get library => _libraryMethods;
57+
58+
/// Use this to use [Tag]'s methods.
59+
TagMethods get tag => _tagMethods;
5460
}

‎pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: scrobblenaut
22
description: A deadly simple Last.FM API Wrapper for Dart.
3-
version: 0.1.4
3+
version: 0.1.5
44
homepage: https://github.com/Nebulino/Scrobblenaut
55
issue_tracker: https://github.com/Nebulino/Scrobblenaut/issues
66

0 commit comments

Comments
 (0)
Please sign in to comment.