Automated Twit

I needed to create a throwaway Twitter account for a research project. I decided to automate its creation and share the code with you 🙂

I used the handy service of 10minutemail for receiving Twitter’s verification code.

I used the wonderful browser automation tool Nightmare.

Automation in action:

Patiently wait 10 seconds in the beginning and to receive Verification code


# Zoe Phin, 2021/01/28

require() { sudo apt-get -y install npm && npm install nightmare; }

newtwit() { echo "nm = require('nightmare')
    async function main() {
        e = nm({show: false})
        await e.goto('').wait(2000)

        email = await e.evaluate( ()=> {return document.querySelector('input').value} )

        n = nm({show: true}).viewport(740,680)
        await n.goto('').wait(6000)

        await n.insert('input[name=name]','Unique Snowflake')

        await n.evaluate( ()=> { 
            document.querySelectorAll('div[role=button]')[1].children[0].click() } )

        await n.insert('input[name=email]', email)


        await n.type('body','\t\t\t\t\t\t ')
        await n.type('body','\t\t\t \t\t\t ')
        await n.type('body','\t\t\t\t\t\t\t\t\t ')
        vcode = await e.wait(10000).evaluate( ()=> {
            return document.querySelector('div.small_subject').innerText.substr(0,6) })

        await n.insert('input', vcode).type('body','\t\t\t ')
        await n.wait(2000).type('input[name=password]', 'Un1qu3 Sn0wfl4k3!')
        await n.wait(1000).type('body','\t\t\t ')
        await n.wait(1000).type('body','\t\t ')
        await n.wait(1000).type('body','\t\t ')
        await n.wait(2000).type('body','\t\t ')
        await n.wait(2000).type('body',' ')
        await n.wait(2000).type('body',' ')
        await n.wait(2000).type('body','\t ')
    //	await n.wait(5000).end()
    " | node 

Setup NodeJS and Nightmare:

$ .; require


$ .; newtwit

Note: As I’m not an adept browser-bot coder this code may fail once or twice before working. Just run it until it does. Hopefully someone with more time can fix it. It’s good enough for me.

Enjoy 🙂 -Zoe

P.S. Did you notice Twitter is pushing POTUS as the most important person to follow?

Published by Zoe Phin

2 thoughts on “Automated Twit

  1. Thank you for this crash-course introduction to Nightmare. Useful tool I’ve totally missed, anyways, I think your comment “Hopefully someone with more time can fix it.” actually answers why the script fails from time to time.

    I had the same issue with it failing randomly. After some testing I increased the wait for capturing the vcode to 15000. I also split the “tab, tab, space” lines into separate actions with a 200ms delay between each. Worked 100% after that, so for me it was just more time that was needed.
    Might be my crappy connection that caused the issue in the first place though.

    Liked by 1 person

    1. You’re very welcome 🙂

      Thank you for the suggestions.

      Be careful with this tool. 10minutemail will ban your IP after 40? tries. Use a VPN.

      Twitter uses sophisticated means to thwart automation. They generate random class and other identifiers. The “Next” button has no unique identifier, and so I used the keyboard to navigate the menu.

      However, you can identify the “Next” button by a unique ancestor div tag. If you don’t mind making the code ugly, you can use something like …

      n.evaluate( ()=> { document.querySelectorAll(‘div[unique identifier]’)[x].children[0].children[0].children[0] … click() } )

      I liked the slow keyboard method because it makes a cooler video 😉

      Have fun looking at my other code on this blog.

      P.S. Is that younger or older futhark?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: