Socialify

Folder ..

Viewing bilibili.ts
118 lines (99 loc) • 3.6 KB

  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
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
import axios from 'axios';
import { BilibiliExtractor } from '@consumet/extensions/dist/utils';

class BilibiliUtilis {
  private apiUrl = 'https://api.bilibili.tv/intl/gateway/web';
  private sgProxy = 'https://cors.proxy.consumet.org';

  constructor(private locale: string = 'en_US') {}
  returnDASH = async (fastify: FastifyInstance, options: RegisterOptions) => {
    fastify.get(
      '/bilibili/playurl',
      async (request: FastifyRequest, reply: FastifyReply) => {
        const episodeId = (request.query as { episode_id: string }).episode_id;

        if (typeof episodeId === 'undefined')
          return reply.status(400).send({ message: 'episodeId is required' });

        try {
          // const ss = await axios.get(
          //   `${this.sgProxy}/${this.apiUrl}/playurl?s_locale=${this.locale}&platform=web&ep_id=${episodeId}`,
          //   { headers: { cookie: String(process.env.BILIBILI_COOKIE) } }
          // );
          const ss = await axios.get(
            `https://kaguya.app/server/source?episode_id=${episodeId}&source_media_id=1&source_id=bilibili`,
            { headers: { cookie: String(process.env.BILIBILI_COOKIE) } },
          );
          //kaguya.app/server/source?episode_id=11560397&source_media_id=1&source_id=bilibili
          //console.log(ss.data);
          if (!ss.data.sources)
            return reply.status(404).send({ message: 'No sources found' });

          const dash = await axios.get(ss.data.sources[0].file);
          //const dash = new BilibiliExtractor().toDash(ss.data.data.playurl);

          return reply.status(200).send(dash.data);
        } catch (err) {
          console.log(err);
          reply
            .status(500)
            .send({ message: 'Something went wrong. Contact developer for help.' });
        }
      },
    );
  };

  returnVTT = async (fastify: FastifyInstance, options: RegisterOptions) => {
    fastify.get(
      '/bilibili/subtitle',
      async (request: FastifyRequest, reply: FastifyReply) => {
        const url = (request.query as { url: string }).url;
        try {
          const jsonVtt = await axios.get(url);
          const vtt = new VTT();
          jsonVtt.data.body.map((subtitle: any) => {
            vtt.add(subtitle.from, subtitle.to, subtitle.content);
          });

          reply.status(200).send(vtt.toString());
        } catch (err) {
          reply
            .status(500)
            .send({ message: 'Something went wrong. Contact developer for help.' });
        }
      },
    );
  };
}

class VTT {
  counter = 0;
  content = 'WEBVTT\r\n';

  private pad = (num: any) => {
    if (num < 10) {
      return '0' + num;
    }

    return num;
  };

  private secondsToTime = (sec: any) => {
    if (typeof sec !== 'number') {
      throw new Error('Invalid type: expected number');
    }

    var seconds = (sec % 60).toFixed(3);
    var minutes = Math.floor(sec / 60) % 60;
    var hours = Math.floor(sec / 60 / 60);

    return this.pad(hours) + ':' + this.pad(minutes) + ':' + this.pad(seconds);
  };

  add = (from: any, to: any, lines: any, settings?: any) => {
    ++this.counter;
    lines = lines.constructor === Array ? lines : [lines];

    this.content +=
      '\r\n' +
      this.counter +
      '\r\n' +
      this.secondsToTime(from) +
      ' --> ' +
      this.secondsToTime(to) +
      (settings ? ' ' + settings : '') +
      '\r\n';

    lines.forEach((line: any) => {
      this.content += line + '\r\n';
    });
  };

  toString = () => {
    return this.content;
  };
}

export default BilibiliUtilis;