-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.html
238 lines (197 loc) · 7.77 KB
/
index.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<html>
<head>
<title>Last.fm to csv</title>
<link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.css">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<a href="https://github.com/benfoxall/lastfm-to-csv" target="_blank"><img style="position: absolute; top: 0; right: 0; border: 0;" src="gh.png" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
<div class="container">
<div class="page-header">
<h1>
Last.fm to csv
<small>
<a href="#settings" data-toggle="modal" data-target="#settings">
<span class="glyphicon glyphicon-cog"></span>
</a>
</small>
</h1>
</div>
<p class="lead">This fetches data from the lastfm api, and formats it as a csv document</p>
<div class="alert alert-warning" style="display:none" rv-show="user_error">
Couldn't find user.
This error may also occur if you have AdBlock enabled,
or if you've chosen to "Hide recent listening information" in the Last.fm privacy settings.
</div>
<form class="form-inline">
<div class="form-group" rv-class-has-error="user_error">
<input type="text" class="form-control input-lg" placeholder="lastfm username" name="lastfm-user" required rv-disabled="submitted">
</div>
<button type="submit" class="btn btn-primary btn-lg" rv-disabled="submitted">Fetch tracks</button>
</form>
<section id="results" rv-show="submitted" style="display:none">
<h1>{ status }</h1>
<h2><small>{dates | timerange}</small></h2>
<!--p>
<span class="label label-warning">
retries: <span data-bind="retries">-</span>
</span>
-->
<p>
<span class="label label-danger" rv-show="errors">
errors: <span data-bind="errors">{ errors }</span>
</span>
</p>
<p>
<a class="btn" href="#download" rv-class-btn-success="complete" rv-class-btn-info="incomplete < complete">
Save
<small>{ kb } KB</small>
</a>
<a href="#cancel" rv-show="incomplete < complete">cancel</a>
<span class="help-block">a partial file can be saved while fetching more data</span>
</p>
</section>
<!-- settings modal -->
<div class="modal fade" id="settings">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Extra Settings</h4>
</div>
<div class="modal-body">
<p class="lead">
Requests to the lastfm api can be rate limited or fail, use these settings if you start having problems
</p>
<div class="form-group">
<label for="api-key">
alternate api key
</label>
<input type="text" class="form-control input-md" id="api-key" placeholder="0123456789abcdef0123456789abcdef" rv-value="api_key">
<p class="help-block">Use an alternative api key - generate one at <a href="http://www.last.fm/api">last.fm/api</a></p>
</div>
<div class="form-group">
<label for="request-delay">
request delay
</label>
<input type="text" class="form-control input-md" id="request-delay" placeholder="0" rv-value="request_delay">
<p class="help-block">(milliseconds, default 0) how long to wait between making requests</p>
</div>
</div>
</div>
</div>
</div>
<script src="bower_components/reqwest/reqwest.js"></script>
<script src="bower_components/FileSaver/FileSaver.js"></script>
<script src="bower_components/async/lib/async.js"></script>
<script src="bower_components/comma-separated-values/csv.js"></script>
<script src="bower_components/rivets/dist/rivets.js"></script>
<script src="js/lastfm-export.js"></script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script type="text/javascript">
//hook up the export to the page
// the state of the page
var state = {
status: '-',
kb: 0,
submitted: false,
user_error: false,
complete: false,
errors: 0,
dates: [null, null],
// settings
api_key:'',
request_delay:'',
cancelled: false,
incomplete: function(){
return !this.complete
}
};
function extend([min, max], date_str){
if(!date_str) return [min,max]
const date = new Date(date_str)
return [
date < min || min === null ? date : min,
date > max || max === null ? date : max
]
}
let formatter = new Intl.DateTimeFormat(navigator.language, { month: 'short', year: 'numeric' });
rivets.formatters.timerange = function([from, to]) {
if(!(from || to)) return '-'
return formatter.formatRange(from, to)
}
rivets.bind(document, state)
var data = [], bytes = 0;
$('[href=#download]').on('click', function(e){
e.preventDefault();
var b = new Blob(data, {type: 'text/csv'})
saveAs(b, ($('[name=lastfm-user]').val()||"lastFM") + '.csv')
})
$('[href=#cancel]').on('click', function(e){
e.preventDefault();
state.cancelled = true;
})
$('form').on('submit', function(e){
e.preventDefault();
state.submitted = true;
state.user_error = false;
state.errors = 0;
// the data that will be filled with csv
data = [];
var delay = $('request-delay').val() || 0,
key = $('api-key').val() || '974a5ebc077564f72bd639d122479d4b',
user = $('[name=lastfm-user]').val();
lastFM(requestData(key, user))
.fail(function (err, msg) {
state.submitted = false;
state.user_error = true;
})
.then(extractPageCount)
.then(function(page_count){
state.status = "fetching page 1/" + page_count;
var current = 0;
var requests = requestList(key, user, page_count)
// .slice(0,5)
.map(function(r, i){
return {
data:r,
i:i
}
})
async.eachSeries(requests, function(item, callback){
if(state.cancelled) return callback(false);
state.status = "fetching page " + (item.i) + "/" + page_count;
lastFM(item.data)
.then(extractTracks)
.then(function(tracks){
var blb = new Blob([
tracks.map(function(d){
return row(['artist', 'album', 'name', 'date'], d)
})
.map(csv).join('\n') + '\n']);
data[item.i] = blb;
bytes+= blb.size;
state.kb = Math.round(bytes/1024);
state.dates = extend(state.dates, tracks[0].date)
state.dates = extend(state.dates, tracks[tracks.length - 1].date)
})
.fail(function(error){
console.error(error);
state.errors++;
})
.always(function(){
setTimeout(callback, parseInt(state.request_delay || '0', 10), false);
})
}, function(){
state.status = "finished";
state.complete = true;
})
})
});
</script>
</div>
</body>
</html>