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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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-unused-vars */
/* global getParameterByName forge */
/* global isElectron cookieSet cookieGet cookieRemove async */
class netease {
  static _create_secret_key(size) {
    const result = [];
    const choice = '012345679abcdef'.split('');
    for (let i = 0; i < size; i += 1) {
      const index = Math.floor(Math.random() * choice.length);
      result.push(choice[index]);
    }
    return result.join('');
  }

  static _aes_encrypt(text, sec_key, algo) {
    const cipher = forge.cipher.createCipher(algo, sec_key);
    cipher.start({ iv: '0102030405060708' });
    cipher.update(forge.util.createBuffer(text));
    cipher.finish();

    return cipher.output;
  }

  static _rsa_encrypt(text, pubKey, modulus) {
    text = text.split('').reverse().join(''); // eslint-disable-line no-param-reassign
    const n = new forge.jsbn.BigInteger(modulus, 16);
    const e = new forge.jsbn.BigInteger(pubKey, 16);
    const b = new forge.jsbn.BigInteger(forge.util.bytesToHex(text), 16);
    const enc = b.modPow(e, n).toString(16).padStart(256, '0');
    return enc;
  }

  static weapi(text) {
    const modulus =
      '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b72' +
      '5152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbd' +
      'a92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe48' +
      '75d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
    const nonce = '0CoJUm6Qyw8W8jud';
    const pubKey = '010001';
    text = JSON.stringify(text); // eslint-disable-line no-param-reassign
    const sec_key = this._create_secret_key(16);
    const enc_text = btoa(
      this._aes_encrypt(
        btoa(this._aes_encrypt(text, nonce, 'AES-CBC').data),
        sec_key,
        'AES-CBC'
      ).data
    );
    const enc_sec_key = this._rsa_encrypt(sec_key, pubKey, modulus);
    const data = {
      params: enc_text,
      encSecKey: enc_sec_key,
    };

    return data;
  }

  // refer to https://github.com/Binaryify/NeteaseCloudMusicApi/blob/master/util/crypto.js
  static eapi(url, object) {
    const eapiKey = 'e82ckenh8dichen8';

    const text = typeof object === 'object' ? JSON.stringify(object) : object;
    const message = `nobody${url}use${text}md5forencrypt`;
    const digest = forge.md5
      .create()
      .update(forge.util.encodeUtf8(message))
      .digest()
      .toHex();
    const data = `${url}-36cd479b6b5-${text}-36cd479b6b5-${digest}`;

    return {
      params: this._aes_encrypt(data, eapiKey, 'AES-ECB').toHex().toUpperCase(),
    };
  }

  static ne_show_toplist(offset) {
    if (offset !== undefined && offset > 0) {
      return {
        success: (fn) => fn({ result: [] }),
      };
    }
    const url = 'https://music.163.com/weapi/toplist/detail';
    const data = this.weapi({});
    return {
      success: (fn) => {
        axios.post(url, new URLSearchParams(data)).then((response) => {
          const result = [];
          response.data.list.forEach((item) => {
            const playlist = {
              cover_img_url: item.coverImgUrl,
              id: `neplaylist_${item.id}`,
              source_url: `https://music.163.com/#/playlist?id=${item.id}`,
              title: item.name,
            };
            result.push(playlist);
          });
          return fn({ result });
        });
      },
    };
  }

  static show_playlist(url) {
    const order = 'hot';
    const offset = getParameterByName('offset', url);
    const filterId = getParameterByName('filter_id', url);

    if (filterId === 'toplist') {
      return this.ne_show_toplist(offset);
    }

    let filter = '';
    if (filterId !== '') {
      filter = `&cat=${filterId}`;
    }
    let target_url = '';
    if (offset != null) {
      target_url = `https://music.163.com/discover/playlist/?order=${order}${filter}&limit=35&offset=${offset}`;
    } else {
      target_url = `https://music.163.com/discover/playlist/?order=${order}${filter}`;
    }

    return {
      success: (fn) => {
        axios.get(target_url).then((response) => {
          const { data } = response;
          const list_elements = Array.from(
            new DOMParser()
              .parseFromString(data, 'text/html')
              .getElementsByClassName('m-cvrlst')[0].children
          );
          const result = list_elements.map((item) => ({
            cover_img_url: item.getElementsByTagName('img')[0].src,
            title: item
              .getElementsByTagName('div')[0]
              .getElementsByTagName('a')[0].title,
            id: `neplaylist_${getParameterByName(
              'id',
              item.getElementsByTagName('div')[0].getElementsByTagName('a')[0]
                .href
            )}`,
            source_url: `https://music.163.com/#/playlist?id=${getParameterByName(
              'id',
              item.getElementsByTagName('div')[0].getElementsByTagName('a')[0]
                .href
            )}`,
          }));
          return fn({
            result,
          });
        });
      },
    };
  }

  static ne_ensure_cookie(callback) {
    const domain = 'https://music.163.com';
    const nuidName = '_ntes_nuid';
    const nnidName = '_ntes_nnid';

    cookieGet(
      {
        url: domain,
        name: nuidName,
      },
      (cookie) => {
        if (cookie == null) {
          const nuidValue = this._create_secret_key(32);
          const nnidValue = `${nuidValue},${new Date().getTime()}`;
          // netease default cookie expire time: 100 years
          const expire =
            (new Date().getTime() + 1e3 * 60 * 60 * 24 * 365 * 100) / 1000;

          cookieSet(
            {
              url: domain,
              name: nuidName,
              value: nuidValue,
              expirationDate: expire,
            },
            () => {
              cookieSet(
                {
                  url: domain,
                  name: nnidName,
                  value: nnidValue,
                  expirationDate: expire,
                },
                () => {
                  callback(null);
                }
              );
            }
          );
        } else {
          callback(null);
        }
      }
    );
  }

  static async_process_list(
    data_list,
    handler,
    handler_extra_param_list,
    callback
  ) {
    const fnDict = {};
    data_list.forEach((item, index) => {
      fnDict[index] = (cb) =>
        handler(index, item, handler_extra_param_list, cb);
    });
    async.parallel(fnDict, (err, results) =>
      callback(
        null,
        data_list.map((item, index) => results[index])
      )
    );
  }

  static ng_render_playlist_result_item(index, item, callback) {
    const target_url = 'https://music.163.com/weapi/v3/song/detail';
    const queryIds = [item.id];
    const d = {
      c: `[${queryIds.map((id) => `{"id":${id}}`).join(',')}]`,
      ids: `[${queryIds.join(',')}]`,
    };
    const data = this.weapi(d);
    axios
      .post(target_url, new URLSearchParams(data).toString())
      .then((response) => {
        const track_json = response.data.songs[0];
        const track = {
          id: `netrack_${track_json.id}`,
          title: track_json.name,
          artist: track_json.ar[0].name,
          artist_id: `neartist_${track_json.ar[0].id}`,
          album: track_json.al.name,
          album_id: `nealbum_${track_json.al.id}`,
          source: 'netease',
          source_url: `https://music.163.com/#/song?id=${track_json.id}`,
          img_url: track_json.al.picUrl,
          // url: `netrack_${track_json.id}`,
        };
        return callback(null, track);
      });
  }

  static ng_parse_playlist_tracks(playlist_tracks, callback) {
    const target_url = 'https://music.163.com/weapi/v3/song/detail';
    const track_ids = playlist_tracks.map((i) => i.id);
    const d = {
      c: `[${track_ids.map((id) => `{"id":${id}}`).join(',')}]`,
      ids: `[${track_ids.join(',')}]`,
    };
    const data = this.weapi(d);
    axios.post(target_url, new URLSearchParams(data)).then((response) => {
      const tracks = response.data.songs.map((track_json) => ({
        id: `netrack_${track_json.id}`,
        title: track_json.name,
        artist: track_json.ar[0].name,
        artist_id: `neartist_${track_json.ar[0].id}`,
        album: track_json.al.name,
        album_id: `nealbum_${track_json.al.id}`,
        source: 'netease',
        source_url: `https://music.163.com/#/song?id=${track_json.id}`,
        img_url: track_json.al.picUrl,
        // url: `netrack_${track_json.id}`,
      }));

      return callback(null, tracks);
    });
  }

  static split_array(myarray, size) {
    const count = Math.ceil(myarray.length / size);
    const result = [];
    for (let i = 0; i < count; i += 1) {
      result.push(myarray.slice(i * size, (i + 1) * size));
    }
    return result;
  }

  static ne_get_playlist(url) {
    // special thanks for @Binaryify
    // https://github.com/Binaryify/NeteaseCloudMusicApi
    return {
      success: (fn) => {
        const list_id = getParameterByName('list_id', url).split('_').pop();
        const target_url = 'https://music.163.com/weapi/v3/playlist/detail';
        const d = {
          id: list_id,
          offset: 0,
          total: true,
          limit: 1000,
          n: 1000,
          csrf_token: '',
        };
        const data = this.weapi(d);
        this.ne_ensure_cookie(() => {
          axios.post(target_url, new URLSearchParams(data)).then((response) => {
            const { data: res_data } = response;
            const info = {
              id: `neplaylist_${list_id}`,
              cover_img_url: res_data.playlist.coverImgUrl,
              title: res_data.playlist.name,
              source_url: `https://music.163.com/#/playlist?id=${list_id}`,
            };
            const max_allow_size = 1000;
            const trackIdsArray = this.split_array(
              res_data.playlist.trackIds,
              max_allow_size
            );

            function ng_parse_playlist_tracks_wrapper(trackIds, callback) {
              return netease.ng_parse_playlist_tracks(trackIds, callback);
            }

            async.concat(
              trackIdsArray,
              ng_parse_playlist_tracks_wrapper,
              (err, tracks) => {
                fn({ tracks, info });
              }
            );

            // request every tracks to fetch song info
            // async_process_list(res_data.playlist.trackIds, ng_render_playlist_result_item,
            //   (err, tracks) => fn({
            //     tracks,
            //     info,
            //   }));
          });
        });
      },
    };
  }

  static bootstrap_track(track, success, failure) {
    const sound = {};
    const target_url = `https://interface3.music.163.com/eapi/song/enhance/player/url`;
    let song_id = track.id;
    const eapiUrl = '/api/song/enhance/player/url';

    song_id = song_id.slice('netrack_'.length);

    const d = {
      ids: `[${song_id}]`,
      br: 999000,
    };
    const data = this.eapi(eapiUrl, d);
    const expire =
      (new Date().getTime() + 1e3 * 60 * 60 * 24 * 365 * 100) / 1000;

    cookieSet(
      {
        url: 'https://interface3.music.163.com',
        name: 'os',
        value: 'pc',
        expirationDate: expire,
      },
      (cookie) => {
        axios.post(target_url, new URLSearchParams(data)).then((response) => {
          const { data: res_data } = response;
          const { url, br } = res_data.data[0];
          if (url != null) {
            sound.url = url;
            const bitrate = `${(br / 1000).toFixed(0)}kbps`;
            sound.bitrate = bitrate;
            sound.platform = 'netease';

            success(sound);
          } else {
            failure(sound);
          }
        });
      }
    );
  }

  static is_playable(song) {
    return song.fee !== 4 && song.fee !== 1;
  }

  static search(url) {
    // use chrome extension to modify referer.
    const target_url = 'https://music.163.com/api/search/pc';
    const keyword = getParameterByName('keywords', url);
    const curpage = getParameterByName('curpage', url);
    const searchType = getParameterByName('type', url);
    let ne_search_type = '1';
    if (searchType === '1') {
      ne_search_type = '1000';
    }
    const req_data = {
      s: keyword,
      offset: 20 * (curpage - 1),
      limit: 20,
      type: ne_search_type,
    };
    return {
      success: (fn) => {
        axios
          .post(target_url, new URLSearchParams(req_data))
          .then((response) => {
            const { data } = response;
            let result = [];
            let total = 0;
            if (searchType === '0') {
              result = data.result.songs.map((song_info) => ({
                id: `netrack_${song_info.id}`,
                title: song_info.name,
                artist: song_info.artists[0].name,
                artist_id: `neartist_${song_info.artists[0].id}`,
                album: song_info.album.name,
                album_id: `nealbum_${song_info.album.id}`,
                source: 'netease',
                source_url: `https://music.163.com/#/song?id=${song_info.id}`,
                img_url: song_info.album.picUrl,
                // url: `netrack_${song_info.id}`,
                url: !this.is_playable(song_info) ? '' : undefined,
              }));
              total = data.result.songCount;
            } else if (searchType === '1') {
              result = data.result.playlists.map((info) => ({
                id: `neplaylist_${info.id}`,
                title: info.name,
                source: 'netease',
                source_url: `https://music.163.com/#/playlist?id=${info.id}`,
                img_url: info.coverImgUrl,
                url: `neplaylist_${info.id}`,
                author: info.creator.nickname,
                count: info.trackCount,
              }));
              total = data.result.playlistCount;
            }

            return fn({
              result,
              total,
              type: searchType,
            });
          })
          .catch(() =>
            fn({
              result: [],
              total: 0,
              type: searchType,
            })
          );
      },
    };
  }

  static ne_album(url) {
    const album_id = getParameterByName('list_id', url).split('_').pop();
    // use chrome extension to modify referer.
    const target_url = `https://music.163.com/api/album/${album_id}`;

    return {
      success: (fn) => {
        axios.get(target_url).then((response) => {
          const { data } = response;
          const info = {
            cover_img_url: data.album.picUrl,
            title: data.album.name,
            id: `nealbum_${data.album.id}`,
            source_url: `https://music.163.com/#/album?id=${data.album.id}`,
          };

          const tracks = data.album.songs.map((song_info) => ({
            id: `netrack_${song_info.id}`,
            title: song_info.name,
            artist: song_info.artists[0].name,
            artist_id: `neartist_${song_info.artists[0].id}`,
            album: song_info.album.name,
            album_id: `nealbum_${song_info.album.id}`,
            source: 'netease',
            source_url: `https://music.163.com/#/song?id=${song_info.id}`,
            img_url: song_info.album.picUrl,
            url: !this.is_playable(song_info) ? '' : undefined,
          }));
          return fn({
            tracks,
            info,
          });
        });
      },
    };
  }

  static ne_artist(url) {
    const artist_id = getParameterByName('list_id', url).split('_').pop();
    // use chrome extension to modify referer.
    const target_url = `https://music.163.com/api/artist/${artist_id}`;

    return {
      success: (fn) => {
        axios.get(target_url).then((response) => {
          const { data } = response;
          const info = {
            cover_img_url: data.artist.picUrl,
            title: data.artist.name,
            id: `neartist_${data.artist.id}`,
            source_url: `https://music.163.com/#/artist?id=${data.artist.id}`,
          };

          const tracks = data.hotSongs.map((song_info) => ({
            id: `netrack_${song_info.id}`,
            title: song_info.name,
            artist: song_info.artists[0].name,
            artist_id: `neartist_${song_info.artists[0].id}`,
            album: song_info.album.name,
            album_id: `nealbum_${song_info.album.id}`,
            source: 'netease',
            source_url: `https://music.163.com/#/song?id=${song_info.id}`,
            img_url: song_info.album.picUrl,
            // url: `netrack_${song_info.id}`,
            url: !this.is_playable(song_info) ? '' : undefined,
          }));
          return fn({
            tracks,
            info,
          });
        });
      },
    };
  }

  static lyric(url) {
    const track_id = getParameterByName('track_id', url).split('_').pop();
    // use chrome extension to modify referer.
    const target_url = 'https://music.163.com/weapi/song/lyric?csrf_token=';
    const csrf = '';
    const d = {
      id: track_id,
      lv: -1,
      tv: -1,
      csrf_token: csrf,
    };
    const data = this.weapi(d);
    return {
      success: (fn) => {
        axios.post(target_url, new URLSearchParams(data)).then((response) => {
          const { data: res_data } = response;
          let lrc = '';
          let tlrc = '';
          if (res_data.lrc != null) {
            lrc = res_data.lrc.lyric;
          }
          if (res_data.tlyric != null && res_data.tlyric.lyric != null) {
            // eslint-disable-next-line no-control-regex
            tlrc = res_data.tlyric.lyric.replace(/(|\\)/g, '');
            tlrc = tlrc.replace(/[\u2005]+/g, ' ');
          }
          return fn({
            lyric: lrc,
            tlyric: tlrc,
          });
        });
      },
    };
  }

  static parse_url(url) {
    let result;
    let id = '';
    // eslint-disable-next-line no-param-reassign
    url = url.replace(
      'music.163.com/#/discover/toplist?',
      'music.163.com/#/playlist?'
    ); // eslint-disable-line no-param-reassign
    url = url.replace('music.163.com/#/my/m/music/', 'music.163.com/'); // eslint-disable-line no-param-reassign
    url = url.replace('music.163.com/#/m/', 'music.163.com/'); // eslint-disable-line no-param-reassign
    url = url.replace('music.163.com/#/', 'music.163.com/'); // eslint-disable-line no-param-reassign
    if (url.search('//music.163.com/playlist') !== -1) {
      const match = /\/\/music.163.com\/playlist\/([0-9]+)/.exec(url);
      id = match ? match[1] : getParameterByName('id', url);
      result = {
        type: 'playlist',
        id: `neplaylist_${id}`,
      };
    } else if (url.search('//music.163.com/artist') !== -1) {
      result = {
        type: 'playlist',
        id: `neartist_${getParameterByName('id', url)}`,
      };
    } else if (url.search('//music.163.com/album') !== -1) {
      const match = /\/\/music.163.com\/album\/([0-9]+)/.exec(url);
      id = match ? match[1] : getParameterByName('id', url);
      result = {
        type: 'playlist',
        id: `nealbum_${id}`,
      };
    }
    return {
      success: (fn) => {
        fn(result);
      },
    };
  }

  static get_playlist(url) {
    const list_id = getParameterByName('list_id', url).split('_')[0];
    switch (list_id) {
      case 'neplaylist':
        return this.ne_get_playlist(url);
      case 'nealbum':
        return this.ne_album(url);
      case 'neartist':
        return this.ne_artist(url);
      default:
        return null;
    }
  }

  static get_playlist_filters() {
    const recommend = [
      { id: '', name: '全部' },
      { id: 'toplist', name: '排行榜' },
      { id: '流行', name: '流行' },
      { id: '民谣', name: '民谣' },
      { id: '电子', name: '电子' },
      { id: '舞曲', name: '舞曲' },
      { id: '说唱', name: '说唱' },
      { id: '轻音乐', name: '轻音乐' },
      { id: '爵士', name: '爵士' },
      { id: '乡村', name: '乡村' },
    ];

    const all = [
      {
        category: '语种',
        filters: [
          { id: '华语', name: '华语' },
          { id: '欧美', name: '欧美' },
          { id: '日语', name: '日语' },
          { id: '韩语', name: '韩语' },
          { id: '粤语', name: '粤语' },
        ],
      },
      {
        category: '风格',
        filters: [
          { id: '流行', name: '流行' },
          { id: '民谣', name: '民谣' },
          { id: '电子', name: '电子' },
          { id: '舞曲', name: '舞曲' },
          { id: '说唱', name: '说唱' },
          { id: '轻音乐', name: '轻音乐' },
          { id: '爵士', name: '爵士' },
          { id: '乡村', name: '乡村' },
          { id: 'R%26B%2FSoul', name: 'R&B/Soul' },
          { id: '古典', name: '古典' },
          { id: '民族', name: '民族' },
          { id: '英伦', name: '英伦' },
          { id: '金属', name: '金属' },
          { id: '朋克', name: '朋克' },
          { id: '蓝调', name: '蓝调' },
          { id: '雷鬼', name: '雷鬼' },
          { id: '世界音乐', name: '世界音乐' },
          { id: '拉丁', name: '拉丁' },
          { id: 'New Age', name: 'New Age' },
          { id: '古风', name: '古风' },
          { id: '后摇', name: '后摇' },
          { id: 'Bossa Nova', name: 'Bossa Nova' },
        ],
      },
      {
        category: '场景',
        filters: [
          { id: '清晨', name: '清晨' },
          { id: '夜晚', name: '夜晚' },
          { id: '学习', name: '学习' },
          { id: '工作', name: '工作' },
          { id: '午休', name: '午休' },
          { id: '下午茶', name: '下午茶' },
          { id: '地铁', name: '地铁' },
          { id: '驾车', name: '驾车' },
          { id: '运动', name: '运动' },
          { id: '旅行', name: '旅行' },
          { id: '散步', name: '散步' },
          { id: '酒吧', name: '酒吧' },
        ],
      },
      {
        category: '情感',
        filters: [
          { id: '怀旧', name: '怀旧' },
          { id: '清新', name: '清新' },
          { id: '浪漫', name: '浪漫' },
          { id: '伤感', name: '伤感' },
          { id: '治愈', name: '治愈' },
          { id: '放松', name: '放松' },
          { id: '孤独', name: '孤独' },
          { id: '感动', name: '感动' },
          { id: '兴奋', name: '兴奋' },
          { id: '快乐', name: '快乐' },
          { id: '安静', name: '安静' },
          { id: '思念', name: '思念' },
        ],
      },
      {
        category: '主题',
        filters: [
          { id: '综艺', name: '综艺' },
          { id: '影视原声', name: '影视原声' },
          { id: 'ACG', name: 'ACG' },
          { id: '儿童', name: '儿童' },
          { id: '校园', name: '校园' },
          { id: '游戏', name: '游戏' },
          { id: '70后', name: '70后' },
          { id: '80后', name: '80后' },
          { id: '90后', name: '90后' },
          { id: '网络歌曲', name: '网络歌曲' },
          { id: 'KTV', name: 'KTV' },
          { id: '经典', name: '经典' },
          { id: '翻唱', name: '翻唱' },
          { id: '吉他', name: '吉他' },
          { id: '钢琴', name: '钢琴' },
          { id: '器乐', name: '器乐' },
          { id: '榜单', name: '榜单' },
          { id: '00后', name: '00后' },
        ],
      },
    ];
    return {
      success: (fn) => fn({ recommend, all }),
    };
  }

  static login(url) {
    // use chrome extension to modify referer.
    let target_url = 'https://music.163.com/weapi/login';
    const loginType = getParameterByName('type', url);

    const password = getParameterByName('password', url);

    let req_data = {};
    if (loginType === 'email') {
      const email = getParameterByName('email', url);

      req_data = {
        username: email,
        password: forge.md5
          .create()
          .update(forge.util.encodeUtf8(password))
          .digest()
          .toHex(),
        rememberLogin: 'true',
      };
    } else if (loginType === 'phone') {
      target_url = `https://music.163.com/weapi/login/cellphone`;
      const countrycode = getParameterByName('countrycode', url);
      const phone = getParameterByName('phone', url);
      req_data = {
        phone,
        countrycode,
        password: forge.md5
          .create()
          .update(forge.util.encodeUtf8(password))
          .digest()
          .toHex(),
        rememberLogin: 'true',
      };
    }

    const encrypt_req_data = this.weapi(req_data);
    const expire =
      (new Date().getTime() + 1e3 * 60 * 60 * 24 * 365 * 100) / 1000;

    cookieSet(
      {
        url: 'https://music.163.com',
        name: 'os',
        value: 'pc',
        expirationDate: expire,
      },
      (cookie) => {}
    );
    return {
      success: (fn) => {
        axios
          .post(target_url, new URLSearchParams(encrypt_req_data))
          .then((response) => {
            const { data } = response;
            const result = {
              is_login: true,
              user_id: data.account.id,
              user_name: data.account.userName,
              nickname: data.profile.nickname,
              avatar: data.profile.avatarUrl,
              platform: 'netease',
              data,
            };
            return fn({
              status: 'success',
              data: result,
            });
          })
          .catch(() =>
            fn({
              status: 'fail',
              data: {},
            })
          );
      },
    };
  }

  static get_user_playlist(url, playlistType) {
    const user_id = getParameterByName('user_id', url);
    const target_url = 'https://music.163.com/api/user/playlist';

    const req_data = {
      uid: user_id,
      limit: 1000,
      offset: 0,
      includeVideo: true,
    };

    return {
      success: (fn) => {
        axios
          .post(target_url, new URLSearchParams(req_data))
          .then((response) => {
            const playlists = [];
            response.data.playlist.forEach((item) => {
              if (playlistType === 'created' && item.subscribed !== false) {
                return;
              }
              if (playlistType === 'favorite' && item.subscribed !== true) {
                return;
              }
              const playlist = {
                cover_img_url: item.coverImgUrl,
                id: `neplaylist_${item.id}`,
                source_url: `https://music.163.com/#/playlist?id=${item.id}`,
                title: item.name,
              };
              playlists.push(playlist);
            });
            return fn({
              status: 'success',
              data: {
                playlists,
              },
            });
          });
      },
    };
  }

  static get_user_created_playlist(url) {
    return this.get_user_playlist(url, 'created');
  }

  static get_user_favorite_playlist(url) {
    return this.get_user_playlist(url, 'favorite');
  }

  static get_recommend_playlist() {
    const target_url = 'https://music.163.com/weapi/personalized/playlist';

    const req_data = {
      limit: 30,
      total: true,
      n: 1000,
    };

    const encrypt_req_data = this.weapi(req_data);

    return {
      success: (fn) => {
        axios
          .post(target_url, new URLSearchParams(encrypt_req_data))
          .then((response) => {
            const playlists = [];
            response.data.result.forEach((item) => {
              const playlist = {
                cover_img_url: item.picUrl,
                id: `neplaylist_${item.id}`,
                source_url: `https://music.163.com/#/playlist?id=${item.id}`,
                title: item.name,
              };
              playlists.push(playlist);
            });
            return fn({
              status: 'success',
              data: {
                playlists,
              },
            });
          });
      },
    };
  }

  static get_user() {
    const url = `https://music.163.com/api/nuser/account/get`;

    const encrypt_req_data = this.weapi({});
    return {
      success: (fn) => {
        axios.post(url, new URLSearchParams(encrypt_req_data)).then((res) => {
          let result = { is_login: false };
          let status = 'fail';
          if (res.data.account !== null) {
            status = 'success';
            const { data } = res;
            result = {
              is_login: true,
              user_id: data.account.id,
              user_name: data.account.userName,
              nickname: data.profile.nickname,
              avatar: data.profile.avatarUrl,
              platform: 'netease',
              data,
            };
          }

          return fn({
            status,
            data: result,
          });
        });
      },
    };
  }

  static get_login_url() {
    return `https://music.163.com/#/login`;
  }

  static logout() {
    cookieRemove(
      {
        url: 'https://music.163.com',
        name: 'MUSIC_U',
      },
      (cookie) => {}
    );
  }
}