Skip to content

Instantly share code, notes, and snippets.

@gpollo
Created January 8, 2018 17:17
Show Gist options
  • Save gpollo/5baec525cc133af9699bc6b45d460846 to your computer and use it in GitHub Desktop.
Save gpollo/5baec525cc133af9699bc6b45d460846 to your computer and use it in GitHub Desktop.
Small python script that automatically vote on Strawpoll using different proxies
#!/usr/bin/env python2
#
# Usage: <executable> <url> <choice> <list>
#
# The `url` is the url of the poll. It won't work if "Improve spam prevention"
# is activated. The `choice` is the index of the answer your want to spam. The
# `list` is the list containing the proxies.
#
import re
import os
import sys
import mechanize
import urllib2
import requests
import threading
from bs4 import BeautifulSoup
agent = 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0)Gecko/20100101 Firefox/18.0 (compatible;)'
# this function reads proxies from a file
def get_proxy_list(filename):
proxies = []
with open(filename) as f:
while True:
proxy = f.readline().split(' ')[0]
if proxy == '':
break
proxies.append(proxy)
return proxies
class Voter(threading.Thread):
url = None
choice = None
proxies = None
# used to syncronize the threads
lock = threading.Lock()
# index of the next proxy
index = 0
# list of voter threads
voters = []
def __init__(self):
self.proxy = Voter.get_proxy()
threading.Thread.__init__(self)
def run(self):
count = 0
# make sure we have a proxy
if self.proxy == None:
return
# add us to the list
with Voter.lock:
Voter.voters.append(self)
count = len(Voter.voters)
print("Starting voter with ip={}, count={}".format(self.proxy, count))
# a bunch of shit can go wrong, we don't care about them
try:
self.vote()
except:
pass
# create a new voters
Voter().start()
# remove ourself
with Voter.lock:
Voter.voters.remove(self)
count = len(Voter.voters)
print("Removing voter with ip={}, count={}".format(self.proxy, count))
def vote(self):
# browser settings
browser = mechanize.Browser()
browser.set_proxies({"https" : self.proxy, "http" : self.proxy})
browser.set_handle_robots(False)
browser.set_handle_redirect(True)
browser.set_handle_equiv(False)
browser.addheaders = [('User-agent', agent), ('Accept', '*/*')]
# navigate to the download link
page = browser.open(Voter.url)
browser.select_form(nr=1)
for control in browser.form.controls:
if not control.type == "radio":
continue
control.items[Voter.choice].selected = True
print(browser.submit().read())
# get a proxy from the list safely
@staticmethod
def get_proxy():
proxy = None
with Voter.lock:
if Voter.index < len(Voter.proxies):
proxy = Voter.proxies[Voter.index]
Voter.index = Voter.index + 1
return proxy
if __name__ == '__main__':
# make sure the user specified the URL
if len(sys.argv) < 3:
print("missing arguments")
exit(1)
# configure the voters
Voter.url = sys.argv[1]
Voter.choice = int(sys.argv[2])
Voter.proxies = get_proxy_list(sys.argv[3])
# start threads
for i in range(40):
Voter().start()
# wait for the threads to end
while len(Voter.voters) > 0:
Voter.voters[0].join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment