const iconv = require('iconv-lite');
const rp = require('request-promise');
const striptags = require('striptags');

const LyricBase = require('../include/lyric_base');

const keyword = 'kashinavi';

class Lyric extends LyricBase {
  find_song_id(url) {
    const pattern = /\?([0-9]+)/;
    return this.get_first_group_by_pattern(url, pattern);
  }

  async find_lyric(url, html) {
    const prefix = 'oncopy="return false;" unselectable="on;">';
    const suffix = '</p>';
    let lyric = this.find_string_by_prefix_suffix(html, prefix, suffix, false);
    lyric = lyric.replace(/<br>　<br>/g, '\n\n');
    lyric = lyric.replace(/<br>/g, '\n');
    lyric = striptags(lyric);
    lyric = lyric.trim();

    this.lyric = lyric;
    return true;
  }

  async find_info(url, html) {
    const prefix = '<table border=0 cellpadding=0 cellspacing=5>';
    const suffix = '<hr noshade size=1>';
    const table_str = this.find_string_by_prefix_suffix(html, prefix, suffix, false);

    const patterns = {
      title: '<td><h.>([^<]+?)[♪<]',
      artist: '<a href="[^"]+">(.+?)</a>',
      lyricist: '作詞　：　([^<]+)<br>',
      composer: '作曲　：　([^<]+)</td>',
    };

    this.fill_song_info(table_str, patterns);
  }

  async get_html(url) {
    const raw = await rp({ url, encoding: null });
    const html = iconv.decode(raw, 'Shift_JIS');

    return html;
  }

  async parse_page() {
    const { url } = this;

    const html = await this.get_html(url);

    this.find_lyric(url, html);
    this.find_info(url, html);

    return true;
  }
}

exports.keyword = keyword;
exports.Lyric = Lyric;

if (require.main === module) {
  (async () => {
    const url = 'https://kashinavi.com/song_view.html?77597';
    const obj = new Lyric(url);
    const lyric = await obj.get();
    console.log(lyric);
  })();
}

@@ -799,49 +799,43 @@
  '%3Ct
-able border=0 cellpadding=0 cellspacing=5
+d valign=top align=center width=550
 %3E';%0A
@@ -1005,14 +1005,28 @@
 : '%3C
-t
 d
+iv align=center
 %3E%3Ch
-.
+1
 %3E(%5B%5E
@@ -1147,18 +1147,18 @@
 %5B%5E%3C%5D+)%3C/
-td
+.+
 %3E',%0A    

fix kashinavi info