Skip to content

Commit 0212767

Browse files
committed
Merge branch 'apple-music' of https://github.com/statsfm/web into apple-music
2 parents ae8d9ea + f85dd58 commit 0212767

File tree

3 files changed

+61
-26
lines changed

3 files changed

+61
-26
lines changed

src/pages/import.tsx

+46-19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Dropzone from 'react-dropzone';
2222
import { MdFileUpload, MdWarning } from 'react-icons/md';
2323
import { BlobReader, TextWriter, ZipReader } from '@zip.js/zip.js';
2424
import { Button } from '@/components/Button';
25+
import { Platform } from '@/utils/statsfm';
2526

2627
type Props = {};
2728

@@ -36,7 +37,7 @@ export const getServerSideProps: GetServerSideProps<SSRProps> = async (ctx) => {
3637
};
3738

3839
interface ImportService {
39-
id: string;
40+
id: Platform;
4041
name: string;
4142
enabled: boolean;
4243
acceptFiles: Accept;
@@ -58,7 +59,7 @@ const ImportPage: NextPage<Props> = () => {
5859

5960
const services: ImportService[] = [
6061
{
61-
id: 'spotify',
62+
id: Platform.SPOTIFY,
6263
name: 'Spotify',
6364
enabled: true,
6465
acceptFiles: {
@@ -137,15 +138,32 @@ const ImportPage: NextPage<Props> = () => {
137138
}
138139
},
139140
},
140-
// {
141-
// id: 'applemusic',
142-
// name: 'Apple Music',
143-
// enabled: false,
144-
// acceptFiles: {
145-
// 'text/csv': ['.csv'],
146-
// },
147-
// handleFileUpload: async () => {},
148-
// },
141+
{
142+
id: Platform.APPLEMUSIC,
143+
name: 'Apple Music',
144+
enabled: true,
145+
acceptFiles: {
146+
'text/csv': ['.csv'],
147+
},
148+
handleFileUpload: async (files) => {
149+
toaster.message('Processing files...');
150+
// eslint-disable-next-line no-restricted-syntax
151+
for await (const file of files) {
152+
const content = await file.text();
153+
setUploadedFiles((oldList) => [
154+
...oldList,
155+
{
156+
name: file.name,
157+
addedAt: new Date(),
158+
contentType: file.type,
159+
status: UploadedFilesStatus.Ready,
160+
data: content,
161+
service: Platform.APPLEMUSIC,
162+
},
163+
]);
164+
}
165+
},
166+
},
149167
];
150168

151169
const uploadFiles = async () => {
@@ -167,16 +185,25 @@ const ImportPage: NextPage<Props> = () => {
167185
uploadedFile.status = UploadedFilesStatus.Uploading;
168186
const oldUrl = api.options.http.apiUrl;
169187
try {
170-
api.options.http.apiUrl = 'https://import.stats.fm/api';
188+
api.options.http.apiUrl =
189+
process.env.NEXT_PUBLIC_API_URL_IMPORT ??
190+
'https://import.stats.fm/api';
171191
// eslint-disable-next-line no-await-in-loop
172-
await api.me.import({
173-
name: uploadedFile.name,
174-
contentType: 'application/json',
175-
data: JSON.stringify(uploadedFile.data),
176-
key: 'files',
177-
});
192+
await api.me.import(
193+
{
194+
name: uploadedFile.name,
195+
contentType: uploadedFile.contentType,
196+
data:
197+
uploadedFile.service === Platform.SPOTIFY
198+
? JSON.stringify(uploadedFile.data)
199+
: uploadedFile.data,
200+
key: 'files',
201+
},
202+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
203+
importService.id
204+
);
178205

179-
event('IMPORT_upload_file');
206+
event(`IMPORT_${uploadedFile.service}_upload_file`);
180207
uploadedFile.status = UploadedFilesStatus.Uploaded;
181208
} catch (e: any) {
182209
uploadedFile.status = UploadedFilesStatus.Failed;

src/utils/imports.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from 'zod';
22
import type { event } from 'nextjs-google-analytics';
33
import type { SetStateAction } from 'react';
4+
import { Platform } from '@statsfm/statsfm.js';
45

56
export const IMPORT_STATUS = {
67
'-1': 'Errored!',
@@ -108,15 +109,17 @@ export function isJSONParsable(input: string) {
108109
}
109110
}
110111

111-
export type UploadedImportFile<T extends any[] = any[]> = {
112+
export type UploadedImportFile = {
112113
name: string;
113114
addedAt: Date;
114115
status: UploadedFilesStatus;
116+
service: Platform;
115117
} & (
116118
| { status: UploadedFilesStatus.Error; error: string }
117119
| {
118120
status: Exclude<UploadedFilesStatus, UploadedFilesStatus.Error>;
119-
data: T;
121+
data: any;
122+
contentType: string;
120123
}
121124
);
122125

@@ -132,7 +135,7 @@ export const SpotifyImport = {
132135
file.name.match(/StreamingHistory_music_[0-9][0-9]?.json/g) ||
133136
file.name.match(/StreamingHistory[0-9][0-9]?.json/g)
134137
) {
135-
utils.event('IMPORT_selected_spotify_account_data');
138+
utils.event('IMPORT_SPOTIFY_selected_spotify_account_data');
136139
utils.setUploadedFiles((oldList) => [
137140
...oldList,
138141
{
@@ -142,12 +145,13 @@ export const SpotifyImport = {
142145
status: UploadedFilesStatus.Error,
143146
error:
144147
'You are trying to upload the streaming history files from the "Account data" package, we do not support these files.',
148+
service: Platform.SPOTIFY,
145149
},
146150
]);
147151
} else {
148152
const fileText = file.content;
149153
if (!isJSONParsable(fileText)) {
150-
utils.event('IMPORT_selected_invalid_file');
154+
utils.event('IMPORT_SPOTIFY_selected_invalid_file');
151155
utils.setUploadedFiles((oldList) => [
152156
...oldList,
153157
{
@@ -156,6 +160,7 @@ export const SpotifyImport = {
156160
uploaded: false,
157161
status: UploadedFilesStatus.Error,
158162
error: 'The file you selected is not a valid json file.',
163+
service: Platform.SPOTIFY,
159164
},
160165
]);
161166
return;
@@ -165,7 +170,7 @@ export const SpotifyImport = {
165170
try {
166171
spotifyStreams = await spotifyImportFileSchema.parseAsync(jsonStreams);
167172
} catch (e: any) {
168-
utils.event('IMPORT_selected_invalid_file');
173+
utils.event('IMPORT_SPOTIFY_selected_invalid_file');
169174
utils.setUploadedFiles((oldList) => [
170175
...oldList,
171176
{
@@ -174,6 +179,7 @@ export const SpotifyImport = {
174179
uploaded: false,
175180
status: UploadedFilesStatus.Error,
176181
error: 'The file you selected does not contain valid data.',
182+
service: Platform.SPOTIFY,
177183
},
178184
]);
179185
return;
@@ -201,6 +207,8 @@ export const SpotifyImport = {
201207
uploaded: false,
202208
status: UploadedFilesStatus.Ready,
203209
data: validStreams,
210+
service: Platform.SPOTIFY,
211+
contentType: 'application/json',
204212
},
205213
]);
206214
}

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,8 @@
854854
integrity sha512-LwzQKA4vzIct1zNZzBmRKI9QuNpLgTQMEjsQLf3BXuGYb3QPTP4Yjf6mkdX+X1mYttZ808QpOwAzZjv28kq7DA==
855855

856856
"@statsfm/statsfm.js@https://github.com/statsfm/statsfm.js#apple-music-build":
857-
version "2.1.8"
858-
resolved "https://github.com/statsfm/statsfm.js#538f0e7291f04cd2fe0921a85ef5c86a916dfce8"
857+
version "2.1.11"
858+
resolved "https://github.com/statsfm/statsfm.js#176b64d2b2f1f3d5a8cdc567342f4bd41cbfe0d6"
859859
dependencies:
860860
file-type "16"
861861
isomorphic-unfetch "^3.1.0"

0 commit comments

Comments
 (0)