<commit_before>import cookielib
import random
import time
import socket
import subprocess
import string
import sqlite3
import os

import mechanize
import socks


DB_NAME = 'usernames.db'
SCHEMA = 'create table usernames (id integer primary key autoincrement not null,name text not null)'
# For ease of use this will create a bunch of users with the same string followed by a number and the same password
# This will come in handy further on down the road when we do vote manipulation
PASSWORD = "<PASSWORD>"


def gen_random_string(len_name=10):
    """
    This generates a random string of the specified length using
    the ASCII characters excluding special chars

    :param len_name: optional. specifies the length of the string to return
    :returns: random string of specified length
    """
    chars = string.ascii_letters + string.digits
    name = ''
    for i in range(len_name):
        idx = random.randint(0, len(chars)-1)
        name += chars[idx]
    return name


def init_db():
    """
    Creates a database file and initializes the table(s)
    if it does not exist

    :returns: nothing
    """
    exists = os.path.exists(DB_NAME)
    with sqlite3.connect(DB_NAME) as conn:
        if not exists:
            conn.executescript(SCHEMA)
            conn.commit()


def insert_to_db(name):
    """
    Inserts a username into the table

    :param name: a username string to be inserted
    :returns: true if inserted, false if the name is already in the db
    """
    with sqlite3.connect(DB_NAME) as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT id FROM usernames WHERE name = ?', (name,))
        data = cursor.fetchone()
        if not data:
            # it doesnt exist yet in our db
            cursor.execute('INSERT INTO usernames (name) values (?)', (name,))
            conn.commit()
            return True
        else:
            return False


def get_all_names():
    """
    Gives back a list of all the usernames in the db

    :returns: a list containing all usernames in the db
    """
    with sqlite3.connect(DB_NAME) as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT name FROM usernames')
        names = cursor.fetchall()
        return [row[0] for row in names]


# Necessary for it to work with mechanize
def create_connection(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

# Sets up TOR 
def setTOR():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

    #patch the socket module
    socket.socket = socks.socksocket
    socket.create_connection = create_connection

    checkTor = subprocess.check_output('service tor restart', shell = True)

    if "Starting tor daemon...done." in checkTor:
        print "[+] Started tor daemon for first time"
    else:
        print "[-] Error starting tor daemon"

# This class if almost verbatim from *Violent Python*
class anonBrowser(mechanize.Browser):

    def __init__(self, user_agents = []):
        mechanize.Browser.__init__(self)
        self.set_handle_robots(False)
        self.user_agents = user_agents + ['Mozilla/4.0 ',\
        'FireFox/6.01','ExactSearch', 'Nokia7110/1.0']
        self.cookie_jar = cookielib.CookieJar()
        self.set_cookiejar(self.cookie_jar)
        self.anonymize()

    def clear_cookies(self):
        self.cookie_jar = cookielib.CookieJar()
        self.set_cookiejar(self.cookie_jar)

    def change_user_agent(self):
        userAgent = random.choice(self.user_agents)
        self.addheaders = [('User-agent', userAgent)]

    # This gets a new ip by resetting tor
    def change_proxy(self):
        checkTor = subprocess.check_output('service tor restart', shell = True)
        if "Stopping tor daemon...done." and "Starting tor daemon...done." in checkTor:
            print "[+] Restarted tor daemon"
        elif "Stopping tor daemon...done." and not "Starting tor daemon...done." in checkTor:
            print "[-] Error starting tor daemon"
            exit(0)
        elif not "Stopping tor daemon...done." in checkTor:
            print "[-] Error stopping tor daemon"
            exit(0)

    def anonymize(self, sleep = False):
        self.clear_cookies()
        self.change_user_agent()
        self.change_proxy()
        
        # Largely unnecessary at this point
        if sleep:
            print "Sleep Started"
            time.sleep(600)
            print "Sleeping Finished"

def main():    
    init_db()
    setTOR()
    # This will create 1000 users with the same password
    for x in range(1, 1000):
        success = False

        # Ran into a socks issue that this corrects
        while not success:
    
            # First we anonymize which also sleeps for 10 minutes
            br = anonBrowser()
            
            # Next we open up reddit's login and grab the create user form
            try:
                br.open('https://www.reddit.com/login')
            except Exception, e:
                print "[-] Error connecting to reddit.com "
                continue

            br.form = list(br.forms())[0]

            user = gen_random_string(12)
            done = insert_to_db(user)
            while not done:
                user = gen_random_string(12)
                done = insert_to_db(user)

            br['user'] = user
            br['passwd'] = PASSWORD
            br['passwd2'] = PASSWORD

            br.method = "POST"
            response = br.submit()
            response2 = br.response().read()

            # Cleaner error handling
            if "you are doing that too much" in response2:
                rateLocation = response2.find("you are doing that too much. try again in ")
                rateLimit = int(response2[rateLocation + 42 : rateLocation + 44])
                print "[-] Rate limiting detected. Try again in {0} minutes".format(rateLimit)
            elif "that username is already taken" in response2:
                print "[-] User: {0} already exists".format(user)
            elif "username can only" in response2:
                print "[-] User: {0} is an invalid username, can only contain numbers, letters \'-\'' and \'_\'".format(user)
            else:
                print "[+] {0} successfully created.".format(user)
                success = True

if __name__ == "__main__":
    main()
<commit_msg>Modified range to be more concise<commit_after>import cookielib
import random
import time
import socket
import subprocess
import string
import sqlite3
import os

import mechanize
import socks


DB_NAME = 'usernames.db'
SCHEMA = 'create table usernames (id integer primary key autoincrement not null,name text not null)'
# For ease of use this will create a bunch of users with the same string followed by a number and the same password
# This will come in handy further on down the road when we do vote manipulation
PASSWORD = "<PASSWORD>"


def gen_random_string(len_name=10):
    """
    This generates a random string of the specified length using
    the ASCII characters excluding special chars

    :param len_name: optional. specifies the length of the string to return
    :returns: random string of specified length
    """
    chars = string.ascii_letters + string.digits
    name = ''
    for i in range(len_name):
        idx = random.randint(0, len(chars)-1)
        name += chars[idx]
    return name


def init_db():
    """
    Creates a database file and initializes the table(s)
    if it does not exist

    :returns: nothing
    """
    exists = os.path.exists(DB_NAME)
    with sqlite3.connect(DB_NAME) as conn:
        if not exists:
            conn.executescript(SCHEMA)
            conn.commit()


def insert_to_db(name):
    """
    Inserts a username into the table

    :param name: a username string to be inserted
    :returns: true if inserted, false if the name is already in the db
    """
    with sqlite3.connect(DB_NAME) as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT id FROM usernames WHERE name = ?', (name,))
        data = cursor.fetchone()
        if not data:
            # it doesnt exist yet in our db
            cursor.execute('INSERT INTO usernames (name) values (?)', (name,))
            conn.commit()
            return True
        else:
            return False


def get_all_names():
    """
    Gives back a list of all the usernames in the db

    :returns: a list containing all usernames in the db
    """
    with sqlite3.connect(DB_NAME) as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT name FROM usernames')
        names = cursor.fetchall()
        return [row[0] for row in names]


# Necessary for it to work with mechanize
def create_connection(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

# Sets up TOR 
def setTOR():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

    #patch the socket module
    socket.socket = socks.socksocket
    socket.create_connection = create_connection

    checkTor = subprocess.check_output('service tor restart', shell = True)

    if "Starting tor daemon...done." in checkTor:
        print "[+] Started tor daemon for first time"
    else:
        print "[-] Error starting tor daemon"

# This class if almost verbatim from *Violent Python*
class anonBrowser(mechanize.Browser):

    def __init__(self, user_agents = []):
        mechanize.Browser.__init__(self)
        self.set_handle_robots(False)
        self.user_agents = user_agents + ['Mozilla/4.0 ',\
        'FireFox/6.01','ExactSearch', 'Nokia7110/1.0']
        self.cookie_jar = cookielib.CookieJar()
        self.set_cookiejar(self.cookie_jar)
        self.anonymize()

    def clear_cookies(self):
        self.cookie_jar = cookielib.CookieJar()
        self.set_cookiejar(self.cookie_jar)

    def change_user_agent(self):
        userAgent = random.choice(self.user_agents)
        self.addheaders = [('User-agent', userAgent)]

    # This gets a new ip by resetting tor
    def change_proxy(self):
        checkTor = subprocess.check_output('service tor restart', shell = True)
        if "Stopping tor daemon...done." and "Starting tor daemon...done." in checkTor:
            print "[+] Restarted tor daemon"
        elif "Stopping tor daemon...done." and not "Starting tor daemon...done." in checkTor:
            print "[-] Error starting tor daemon"
            exit(0)
        elif not "Stopping tor daemon...done." in checkTor:
            print "[-] Error stopping tor daemon"
            exit(0)

    def anonymize(self, sleep = False):
        self.clear_cookies()
        self.change_user_agent()
        self.change_proxy()
        
        # Largely unnecessary at this point
        if sleep:
            print "Sleep Started"
            time.sleep(600)
            print "Sleeping Finished"

def main():    
    init_db()
    setTOR()
    # This will create 1000 users with the same password
    for x in range(1000):
        success = False

        # Ran into a socks issue that this corrects
        while not success:
    
            # First we anonymize which also sleeps for 10 minutes
            br = anonBrowser()
            
            # Next we open up reddit's login and grab the create user form
            try:
                br.open('https://www.reddit.com/login')
            except Exception, e:
                print "[-] Error connecting to reddit.com "
                continue

            br.form = list(br.forms())[0]

            user = gen_random_string(12)
            done = insert_to_db(user)
            while not done:
                user = gen_random_string(12)
                done = insert_to_db(user)

            br['user'] = user
            br['passwd'] = PASSWORD
            br['passwd2'] = PASSWORD

            br.method = "POST"
            response = br.submit()
            response2 = br.response().read()

            # Cleaner error handling
            if "you are doing that too much" in response2:
                rateLocation = response2.find("you are doing that too much. try again in ")
                rateLimit = int(response2[rateLocation + 42 : rateLocation + 44])
                print "[-] Rate limiting detected. Try again in {0} minutes".format(rateLimit)
            elif "that username is already taken" in response2:
                print "[-] User: {0} already exists".format(user)
            elif "username can only" in response2:
                print "[-] User: {0} is an invalid username, can only contain numbers, letters \'-\'' and \'_\'".format(user)
            else:
                print "[+] {0} successfully created.".format(user)
                success = True

if __name__ == "__main__":
    main()

{'fraction_non_alphanumeric': 0.0925853582311118, 'fraction_numerical': 0.010391436831002422, 'mean_word_length': 2.2249937011841774, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '2104033', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3561, 'n_words': 1212}