<commit_before>
request = require 'request'
cheerio = require 'cheerio'

#=====================================================================

exports.TwitterBot = class TwitterBot

  constructor : ({@username, @password}) ->

  load_page : (path, status = [200], cb) ->
    uri = [ "https://twitter.com" , path ].join "/"
    await request { uri, cookie_jar : true }, defer err, res, body
    if not err? and not((sc = res.statusCode) in status)
      err = new Error "HTTP status code #{sc}"
    cb err, body, res

  load_login_page : (cb) ->
    await @load_page "/login", null, defer err, body
    $ = cheerio.load body
    tok = (JSON.parse $("#init-data").val()).formAuthenticityToken
    console.log tok
    cb err

#=====================================================================

bot = new TwitterBot {}
await bot.load_login_page defer()<commit_msg>ok, got the token, onwards!<commit_after>
request = require 'request'
cheerio = require 'cheerio'
{katch} = require('iced-utils').util
{make_esc} = require('iced-error')

#=====================================================================

exports.TwitterBot = class TwitterBot

  constructor : ({@username, @password}) ->

  #---------------------------------

  load_page : (path, status = [200], cb) ->
    uri = [ "https://twitter.com" , path ].join "/"
    await request { uri, cookie_jar : true }, defer err, res, body
    if not err? and not((sc = res.statusCode) in status)
      err = new Error "HTTP status code #{sc}"
    cb err, body, res

  #---------------------------------

  grab_auth_token : ($, cb) ->
    err = null
    if not (raw = $("#init-data").val())? or raw.length is 0
      err = new Error "No #init-data form data found"
    else 
      [err, json] = katch () -> JSON.parse(raw)
    if not err? and not (ret = json.formAuthenticityToken)?
      err = new Error "init-data didn't have a 'formAuthenticityToken'"
    cb err, ret

  #---------------------------------

  load_login_page : (cb) ->
    esc = make_esc cb, "load_log_page"
    await @load_page "/login", null, esc defer body
    $ = cheerio.load body
    await @grab_auth_token $, esc defer tok
    console.log tok
    cb null

#=====================================================================

bot = new TwitterBot {}
await bot.load_login_page defer()
{'fraction_non_alphanumeric': 0.2823834196891192, 'fraction_numerical': 0.0030224525043177895, 'mean_word_length': 2.9879518072289155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '5899036', 'n_tokens_mistral': 698, 'n_tokens_neox': 662, 'n_words': 204}