got#Headers TypeScript Examples

The following examples show how to use got#Headers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: request.ts    From cloudmusic-vscode with MIT License 6 votes vote down vote up
responseHandler = async <T>(
  url: string,
  headers: Headers,
  data: QueryInput
): Promise<T | void> => {
  const res = await got<{ readonly code?: number } & T>(url, {
    form: data,
    headers,
    http2: true,
    method: "POST",
    responseType: "json",
    timeout: { response: 8000 },
  });
  if (!res) return;
  const status = res.body.code || res.statusCode;
  if (!spStatus.has(status)) return;
  return res.body;
}
Example #2
Source File: request.ts    From cloudmusic-vscode with MIT License 6 votes vote down vote up
eapiRequest = async <T = QueryInput>(
  url: string,
  data: NodeJS.Dict<string | number | boolean | Headers>,
  encryptUrl: string,
  cookie = AccountState.defaultCookie
): Promise<T | void> => {
  url = `${APISetting.apiProtocol}://${url}`;
  const now = Date.now();
  const header: Headers = {
    osver: cookie.osver,
    deviceId: cookie.deviceId,
    appver: cookie.appver || "8.7.01",
    versioncode: cookie.versioncode || "140",
    mobilename: cookie.mobilename,
    buildver: cookie.buildver || now.toString().slice(0, 10),
    resolution: cookie.resolution || "1920x1080",
    // eslint-disable-next-line @typescript-eslint/naming-convention
    __csrf: cookie.__csrf || "",
    os: cookie.os || ("android" as const),
    channel: cookie.channel,
    requestId: `${now}_${Math.floor(Math.random() * 1000)
      .toString()
      .padStart(4, "0")}`,
  };
  if (cookie.MUSIC_U) header["MUSIC_U"] = cookie.MUSIC_U;
  else {
    cookie.MUSIC_A = anonymousToken;
    header["MUSIC_A"] = anonymousToken;
  }
  const headers = generateHeader();
  headers["Cookie"] = jsonToCookie(header);
  data.header = header;
  return responseHandler<T>(url, headers, eapi(encryptUrl, data));
}
Example #3
Source File: instagram.ts    From scraper with GNU General Public License v3.0 5 votes vote down vote up
export async function instagramdlv3 (url: string): Promise<InstagramDownloaderV2[]> {
  const payload = {
    link: url,
    submit: ''
  }; const headers: Headers = {
    'content-type': 'application/x-www-form-urlencoded',
    origin: 'https://instasave.website',
    referer: 'https://instasave.website/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
  }

  const body: Response<string> = await got('https://instasave.website/', {
    form: payload,
    method: 'POST',
    headers: headers
  }).catch(async (_) => await got('https://server.instasave.website/', {
    form: payload,
    method: 'POST',
    headers: {
      ...headers,
      origin: 'https://server.instasave.website',
      referer: 'https://server.instasave.website'
    }
  }))
  const $ = cheerio.load(body.body)
  let results: InstagramDownloaderV2[] = []
  if ($('#downloadBox > a').length) {
    const temp: {
      thumbnail?: string;
      sourceUrl?: string;
      index: number;
      url?: string;
    }[] = []
    $('#downloadBox > video').each(function (i) {
      const thumbnail = $(this).attr('poster')
      const sourceUrl = $(this).find('source[src]').attr('src')
      if (thumbnail) {
        temp.push({
          thumbnail,
          sourceUrl,
          index: i
        })
      }
    })

    $('#downloadBox > img').each(function (i) {
      const j = temp.findIndex(({ index }) => index === i)
      const thumbnail = $(this).attr('src')
      if (thumbnail) {
        if (j !== -1) temp[j].thumbnail = thumbnail
        else temp.push({ thumbnail, index: i })
      }
    })
    $('#downloadBox > a').each(function (i) {
      const j = temp.findIndex(({ index }) => index === i)
      const url = $(this).attr('href')
      if (j !== -1) temp[j].url = url
      else temp.push({ url, index: i })
    })
    results = temp.map((tmp) => ({
      thumbnail: tmp.thumbnail as string,
      sourceUrl: tmp.sourceUrl as string,
      url: tmp.url as string
    }))
  }
  if (!results.length) throw new ScraperError(`Can't download!\n${$.html()}`)
  return results
}
Example #4
Source File: instagram.ts    From scraper with GNU General Public License v3.0 5 votes vote down vote up
export async function instagramStoryv2 (name: string): Promise<InstagramStoryv2> {
  const headers: Headers = {
    accept: '*/*',
    cookie: '_ga=GA1.2.1814586753.1642307018; _gid=GA1.2.136857157.1642307018; __gads=ID=6f5ca6608dd8b1e9-22e4ea18ffcf0077:T=1642307019:RT=1642307019:S=ALNI_MZA7NeGtOEcSPXyFhf4LY8w7Myg9g; PHPSESSID=1i9dscs75l6v2h17cvdtd587b4; _gat=1; FCNEC=[["AKsRol9R3FQaOjrrETFMIMIvWtuoY3xRHpQEPHMujRWOd_nxuLgWCSyYK9lLC3ev0L5V8fuaSIjhupCtaReRepP4qNvch536pzvrcU13Gh8CRHSEIh8O3zM42ASwGUQfjoKbxkTV1L15EA6O7FLZ-Qh3Fy1rvh_h8w=="],null,[]]',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
  }
  const data = await got('https://www.instagramsave.com/instagram-story-downloader.php', {
    headers: {
      ...headers,
      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
      referer: 'https://www.google.com/'
    }

  }).text()
  const $ = cheerio.load(data)
  const payload = {
    url: 'https://www.instagram.com/' + name,
    action: 'story',
    token: $('#token').val() as string,
    json: ''
  }
  const { user, medias: results, error }: {
    user: {
      id: string;
      username: string;
      fullName: string;
      profilePicUrl: string;
      biography: string;
      followers: number;
      following: number;
    }
    medias: {
      type: string;
      fileType: string;
      url: string;
      downloadUrl: string;
      preview: string;
    }[];
    error?: string;
  } = await got('https://www.instagramsave.com/system/action.php', {
    form: payload,
    method: 'POST',
    headers: {
      ...headers,
      'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
      origin: 'https://www.instagramsave.com',
      referer: 'https://www.instagramsave.com/instagram-story-downloader.php'
    }
  }).json()
  if (error || !results) throw new ScraperError(`Maybe user ${name} not have story!!\n${JSON.stringify({ error, user, results, payload }, null, 2)}`)
  return {
    user,
    results: results.map(({ preview, url, downloadUrl, type, fileType }) => ({
      thumbnail: preview,
      url: downloadUrl,
      sourceUrl: url,
      type,
      fileType,
      isVideo: type === 'video'
    }))
  }
}
Example #5
Source File: instagram.ts    From scraper with GNU General Public License v3.0 4 votes vote down vote up
// export async function instagramdlv5 (url: string): Promise<InstagramDownloaderV5[]> {
//   const json: {
//     url: {
//       url: string;
//       name: string;
//       type: string;
//       ext: string;
//     }[];
//     meta: {
//       title: string;
//       source: string;
//     }
//     thumb: string;
//     [Key: string]: any
//   } = await got('https://api.savefrom.biz/api/convert', {
//     method: 'POST',
//     headers: {
//       'content-type': 'application/json',
//       origin: 'https://savefrom.biz',
//       referer: 'https://savefrom.biz/',
//       'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
//     },
//     json: {
//       url
//     }
//   }).json()
//   return json.url.map(({ url, ext }) => ({
//     url: `https://savefrom.biz${encodeURIComponent(url)}`,
//     ext
//   })) as InstagramDownloaderV5[]
// }

export async function instagramStory (name: string): Promise<InstagramStory> {
  const resKey = await got('https://storydownloader.app/en')
  const $$ = cheerio.load(resKey.body)
  const _token = $$('input[name="_token"]').attr('value')
  const cookie = resKey.headers['set-cookie']?.map(v => v.split('; ')[0]).join('; ').trim()
  const headers: Headers = {
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    cookie: cookie || 'locale=eyJpdiI6IjE5VUJqZm1DdXl3ODhoQnV2SHJaMFE9PSIsInZhbHVlIjoiUnBqZTMvbDFUTWZLWVkvQy9rVjVhOUdrbjRWTVRCYmp2aTVaUlVsUnZZY0RWN2ZoVkdjMVNhemM1MFl6eWt2dCIsIm1hYyI6IjdlMTc4ZDZkMTYyMDVmMTcwZTc5Nzg3YTBjM2ZkOWEyNjRlODZmZDIwOGY5OTgyYzQzZjE3YTY3MjQ2NGNlYzQiLCJ0YWciOiIifQ%3D%3D; _ga_ZXS0LB5VTY=GS1.1.1647856609.1.0.1647856609.0; _ga=GA1.1.1392191220.1647856609; XSRF-TOKEN=eyJpdiI6IkhjVVdRMmRSZ0tOaklvUHlncWxqeVE9PSIsInZhbHVlIjoiTkZLTnFmUnpjM0Y0KzF3NmpxNnMyMTJQWmNPRXFPVjlKQW9la3poN3kySEN4UUw0TUd3TGIzZ0plT2RUWXJGTEp1bzF1NkN2R3FrQkdLbmJpa0o4cUZUM2EzS2N4QTY2aGVKdFM0ZWNhclZBQVBhMDV1cm4vcEZFMVB5NXRLL1UiLCJtYWMiOiI4MjQ1ZDJhYWE2NjQ1MGUyMmY5ZmQ0OTlkMDFhNjZjOWE2MGVjMTRlNmFjN2VjMmNkYzA0OGY5OTRkMDY3MjI3IiwidGFnIjoiIn0%3D; laravel_session=eyJpdiI6IjQ2RHJ3TUtRU1gxblhpbGtsNXRqamc9PSIsInZhbHVlIjoiTFl2bTg5QVhxcHBkZUN2THRPYkxhbnBmWEkyaWdBc0RFbDM0eUhhbGY0RCs2NFFmRXQ2NXBaNktUMkVpYk9wcDF2SE11SUQ0bW9zazJYaUdLQVZFbjJTaXZ3MmREUEJURnczb1c4ZE5uNDJzTVprNytjNzVCT3loS1ovKysyR1oiLCJtYWMiOiIzOTAyMDc5MDg1N2UxZjgwYmExODcwMjQ2ZWQzNGJjODM3YzkxOTI2MTkwMTEzMTFjNjExN2IzZjdkMmY0ODI4IiwidGFnIjoiIn0%3D',
    origin: 'https://storydownloader.app',
    referer: 'https://storydownloader.app/en',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
    'X-CSRF-TOKEN': _token
  }
  const formData = new Form()
  formData.append('username', name)
  formData.append('_token', _token)
  const res = await got('https://storydownloader.app/request', {
    method: 'POST',
    headers: {
      ...headers,
      ...formData.getHeaders()
    },
    body: formData.getBuffer()
  })
  const { html }: {
    [key: string]: any,
    html: string
  } = JSON.parse(res.body)
  if (!html) throw new ScraperError(`Can't download!\n${res.body}`)
  const $ = cheerio.load(html)
  const username = $('h3.card-title').text()
  const profilePicUrl = $('img.card-avatar').attr('src') as string
  const results: InstagramStory['results'] = []
  $('div.row > div').each(function () {
    const $el = $(this)
    const thumbnail = $el.find('img').attr('src')
    const url = $el.find('a').attr('href') as string
    const type = /video_dashinit\.mp4/i.test(url) ? 'video' : 'image'
    const isVideo = type === 'video'
    if (thumbnail && url) {
      results.push({
        thumbnail,
        url,
        type,
        isVideo
      })
    }
  })
  return {
    user: {
      username,
      profilePicUrl
    },
    results
  }
}
Example #6
Source File: module.req.ts    From multi-downloader-nx with MIT License 4 votes vote down vote up
async getData<T = string> (durl: string, params?: Params) {
    params = params || {};
    // options
    const options: Options & {
      minVersion?: string,
      maxVersion?: string
      curlDebug?: boolean
    } = {
      method: params.method ? params.method : 'GET',
      headers: {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:90.0) Gecko/20100101 Firefox/90.0',
      },
    };
    // additional params
    if(params.headers){
      options.headers = {...options.headers, ...params.headers};
    }
    if(options.method == 'POST'){
      (options.headers as Headers)['Content-Type'] = 'application/x-www-form-urlencoded';
    }
    if(params.body){
      options.body = params.body;
    }
    if(params.binary == true){
      options.responseType = 'buffer';
    }
    if(typeof params.followRedirect == 'boolean'){
      options.followRedirect = params.followRedirect;
    }
    // if auth
    const loc = new URL(durl);
    // avoid cloudflare protection
    // debug
    options.hooks = {
      beforeRequest: [
        (options) => {
          if(this.debug){
            console.log('[DEBUG] GOT OPTIONS:');
            console.log(options);
          }
        }
      ]
    };
    if(this.debug){ 
      options.curlDebug = true;
    }
    // try do request
    try {
      const res = await got(durl.toString(), options) as unknown as Response<T>;
      return {
        ok: true,
        res
      };
    }
    catch(_error){
      const error = _error as {
        name: string
      } & ReadError & {
        res: Response<unknown>
      };
      if(error.response && error.response.statusCode && error.response.statusMessage){
        console.log(`[ERROR] ${error.name} ${error.response.statusCode}: ${error.response.statusMessage}`);
      }
      else{
        console.log(`[ERROR] ${error.name}: ${error.code || error.message}`);
      }
      if(error.response && !error.res){
        error.res = error.response;
        const docTitle = (error.res.body as string).match(/<title>(.*)<\/title>/);
        if(error.res.body && docTitle){
          console.log('[ERROR]', docTitle[1]);
        }
      }
      if(error.res && error.res.body && error.response.statusCode 
        && error.response.statusCode != 404 && error.response.statusCode != 403){
        console.log('[ERROR] Body:', error.res.body);
      }
      return {
        ok: false,
        error,
      };
    }
  }