<commit_before>'use strict';
import cheerio = require('cheerio');
import request = require('request');
import rp = require('request-promise');
import Promise = require('bluebird');
import path = require('path');
import fs = require('fs-extra');
import log = require('./log');

import { RequestPromise } from 'request-promise';
import { Response } from 'request';

// tslint:disable-next-line:no-var-requires
const cookieStore = require('tough-cookie-file-store');
// tslint:disable-next-line:no-var-requires
const cloudscraper = require('cloudscraper');

let isAuthenticated = false;
let isPremium = false;

let j: request.CookieJar;

const defaultHeaders: request.Headers =
{
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
  'Connection': 'keep-alive',
  'Referer': 'https://www.crunchyroll.com/login',
};

function generateDeviceId(): string
{
  let id = '';
  const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

  for (let i = 0; i < 32; i++)
  {
    id += possible.charAt(Math.floor(Math.random() * possible.length));
  }

  return id;
}

function startSession(): Promise<string>
{
  return rp(
  {
    method: 'GET',
    url: 'CR_SESSION_URL',
    qs:
    {
      device_id: generateDeviceId(),
      device_type: 'CR_DEVICE_TYPE',
      access_token: 'CR_SESSION_KEY',
      version: 'CR_API_VERSION',
      locale: 'CR_LOCALE',
    },
    json: true,
  })
  .then((response: any) =>
  {
    return response.data.session_id;
  });
}
<commit_msg>Use a proper UUID v4 instead of the weird code to generate a device id<commit_after>'use strict';
import cheerio = require('cheerio');
import request = require('request');
import rp = require('request-promise');
import Promise = require('bluebird');
import uuid = require('uuid');
import path = require('path');
import fs = require('fs-extra');
import log = require('./log');

import { RequestPromise } from 'request-promise';
import { Response } from 'request';

// tslint:disable-next-line:no-var-requires
const cookieStore = require('tough-cookie-file-store');
// tslint:disable-next-line:no-var-requires
const cloudscraper = require('cloudscraper');

let isAuthenticated = false;
let isPremium = false;

let j: request.CookieJar;

const defaultHeaders: request.Headers =
{
  'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; x64; rv:58.0) Gecko/20100101 Firefox/58.0',
  'Connection': 'keep-alive',
  'Referer': 'https://www.crunchyroll.com/login',
};

function startSession(config: IConfig): Promise<string>
{
  if (config.crDeviceId === undefined)
  { 
    config.crDeviceId = uuid.v4();
  }

  return rp(
  {
    method: 'GET',
    url: 'CR_SESSION_URL',
    qs:
    {
      device_id: generateDeviceId(),
      device_type: 'CR_DEVICE_TYPE',
      access_token: 'CR_SESSION_KEY',
      version: 'CR_API_VERSION',
      locale: 'CR_LOCALE',
    },
    json: true,
  })
  .then((response: any) =>
  {
    return response.data.session_id;
  });
}

{'fraction_non_alphanumeric': 0.15706630944407235, 'fraction_numerical': 0.019758874748827863, 'mean_word_length': 4.15, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6154977', 'n_tokens_mistral': 1078, 'n_tokens_neox': 1028, 'n_words': 225}