1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190  | #!/usr/bin/env python3
# Load libraries
import logging
import random
import datetime
import os
import sys
import time
import json
import requests
from torpy import TorClient
from torpy.http.adapter import TorHttpAdapter
from bs4 import BeautifulSoup as BeaSoup
# Constants
RETRIES = 3
OWNP = os.path.abspath(os.path.dirname(sys.argv[0]))
LOGFILE = '/media/sf_data/scriptsout/vote/vote.txt'
# Setup Logging
logging.basicConfig(
    format='%(asctime)s:%(levelname)s:%(message)s',
    level=logging.INFO,
    filename=LOGFILE)
# Storage for the useragent strings from file
#   facilitates picking at random
useragents = []
# Make it look like a real browser
headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml; \
        q=0.9,image/webp,*/*;q=0.8",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.5",
    "DNT": "1",
    "Host": "", # set at runtime
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "", # set at runtime
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive"
}
poll_id = "10614272" # change for a different poll
poll_winner = "49167224" # which item to vote for | 49167240, 49167239, 49167224
# Poll base url
url_poll = (
    "https://poll.fm/"
    + poll_id
)
# Generated URL to be submited for vote
url_template = (
    "https://poll.fm/vote?"
    "va={item_va}"
    "&pt=0"
    "&r={item_r}"
    "&p=" + poll_id +
    "&a=" + poll_winner + ","
    "&o="  # nothing needed here
    "&t={item_t}"
    "&token={item_token}"
    "&pz={item_pz}"
)
# *****************************************************************************
# Helper Functions
# *****************************************************************************
# Loads the user agents from file into the array
def get_useragents():
    logging.info("Loading useragent strings")
    with open(OWNP + '/useragent.txt', 'r') as f:
        for line in f:
            useragents.append(line.rstrip('\n').rstrip('\r'))    
# *****************************************************************************
# Main Function
# *****************************************************************************
def main():
    getdata = True
    try:
        get_useragents() # Load user agents
        headers["Host"] = "poll.fm"
        while getdata: # Loop until there is a problem
            tor = TorClient() # Let's use Tor, shall we....
            with tor.get_guard() as guard:
                adapter = TorHttpAdapter(guard, 3, retries=RETRIES)
                with requests.Session() as s:
                    s.mount('http://', adapter)
                    s.mount('https://', adapter)
                    # Make it look like a different browser randomly
                    #   by changing the user-agent with every request
                    line_no = random.randint(0, len(useragents)-1)
                    cur_useragent = useragents[line_no]
                    headers["User-Agent"] = cur_useragent
                    logging.info("User-Agent set to: %s" % cur_useragent)
                    # Go to poll main page to get the needed items
                    #   to submit for the vote
                    logging.info("Getting: %s" % url_poll)
                    r = s.get(url_poll, headers=headers)
                    logging.info(
                            "First response: %s" 
                            % r.status_code 
                            + " " 
                            + r.url
                        )
                    r.raise_for_status()
                    # Get the html from the web page
                    bs_content = BeaSoup(r.content, "lxml")
                    # Get the info from the Vote button json object
                    button_data = bs_content.find('a', 
                        class_="vote-button css-vote-button pds-vote-button")
                    v_bdata = button_data.attrs.get('data-vote')
                    # Load as json data
                    bdata = json.loads(v_bdata)
                    # Get hidden form value
                    v_pz = bs_content.find(attrs={"name": "pz"})
                    # Inject data into vote URL template
                    url_vote = url_template  # reset url_vote
                    url_vote = url_vote.replace("{item_t}", str(bdata['t']))
                    url_vote = url_vote.replace("{item_token}", str(bdata['n']))
                    url_vote = url_vote.replace("{item_pz}", str(v_pz['value']))
                    url_vote = url_vote.replace("{item_r}", str(bdata['b']))
                    url_vote = url_vote.replace("{item_va}", str(bdata['at']))
                    # Makes it look slightly more authentic, by waiting 
                    #  before actually submitting the vote after the
                    #  initial request to the poll.
                    logging.info("Waiting 2 seconds before submitting.")
                    time.sleep(2)
                    # Let's vote!
                    logging.info("Getting: %s" % url_vote)
                    r = s.get(url_vote, headers=headers)
                    # Redirected response, log it
                    if r.history:
                        logging.info("Request redirected")
                        for resp in r.history:
                            logging.info(
                                "Redirected response: %s" 
                                % resp.status_code 
                                + " " 
                                + resp.url
                            )
                    logging.info(
                            "Final destination: %s" 
                            % r.status_code 
                            + " " 
                            + r.url
                        )
                    r.raise_for_status()
                    # Wait a random number of seconds between 5 & 20
                    #  before starting again - more realistic
                    wait_time = random.randint(5, 20)
                    logging.info("Waiting %i seconds." % (wait_time))
                    time.sleep(wait_time)
    # Any issues, log them and get out.
    except Exception as e:
        logging.error(e)
        # Exit loop
        getdata = False
# *****************************************************************************
# Script Starting Point
# *****************************************************************************
if __name__ == "__main__":
    main()
  |