<commit_before>
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()<commit_msg>fuck, can't figure out why it doesn't work<commit_after>
request = require 'request'
cheerio = require 'cheerio'
{katch} = require('iced-utils').util
{make_esc} = require('iced-error')
util = require 'util'

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

exports.TwitterBot = class TwitterBot

  constructor : ({@username, @password}) ->
    @jar = request.jar()

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

  load_page : ({path, status, form, method }, cb) ->
    status or= [200]
    uri = [ "https://<EMAIL>" , path ].join ""
    form.authenticity_token = @tok if form? and @tok?
    await request { uri, jar : true, form, method }, defer err, res, body
    console.log util.inspect res, { depth : null }
    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 {path : "/login" }, esc defer body
    $ = cheerio.load body
    await @grab_auth_token $, esc defer @tok
    cb null

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

  post_login : (cb) ->
    form =
      'session[username_or_email]' : @username
      'session[password]' : <PASSWORD>
    uri = "/session"
    await @load_page { uri, form, method : "POST" }, defer err, body
    $ = cheerio.load body
    await @grab_auth_token $, defer err, @tok
    cb err

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

  tweet : (txt, cb) ->
    console.log "shit"
    await @load_page { 
      path : "/i/tweet/create", 
      method : "POST", 
      form : {status : txt, place_id : "" },
      }, defer err, body
    cb err

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

test = (cb) ->
  esc = make_esc cb, "tesT"
  bot = new TwitterBot { username : "tacovontaco", password : "<PASSWORD>" }
  await bot.load_login_page esc defer()
  await bot.post_login esc defer()
  await bot.tweet "this be the tweet 22", esc defer()

await test defer err
console.log err
{'fraction_non_alphanumeric': 0.2619907168643631, 'fraction_numerical': 0.0025786487880350697, 'mean_word_length': 2.399649430324277, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 19, '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': '1033636', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1156, 'n_words': 357}