Skip to content

Instantly share code, notes, and snippets.

@bgulla
Created January 27, 2020 21:31
Show Gist options
  • Save bgulla/cfb793ebce79e4c44895a17ee7165e00 to your computer and use it in GitHub Desktop.
Save bgulla/cfb793ebce79e4c44895a17ee7165e00 to your computer and use it in GitHub Desktop.
import json
import random
import time
import logging
import requests
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import os, sys
def get_random_words(limit=25):
"""[summary]
Keyword Arguments:
limit {int} -- [description] (default: {25})
Returns:
[type] -- [description]
"""
randomlists_url = "https://www.randomlists.com/data/words.json"
response = requests.get(randomlists_url)
words_list = random.sample(json.loads(response.text)['data'], 60)
#print('{0} words selected from {1}'.format(len(words_list), randomlists_url))
return words_list[0:(limit-1)]
class BingRewardsBot:
#CONST
BROWSER_MOBILE="mobile"
BROWSER_DESKTOP="DESKTOP"
URL_BASE= "http://www.bing.com/search?q="
def __init__(self, words, browser_type="DESKTOP", email="default", password="default", selenium_host="localhost", selenium_port=4444):
"""[summary]
Arguments:
words {[type]} -- [description]
Keyword Arguments:
browser_type {str} -- [description] (default: {"DESKTOP"})
email {str} -- [description] (default: {"default"})
password {str} -- [description] (default: {"default"})
selenium_host {str} -- [description] (default: {"localhost"})
selenium_port {int} -- [description] (default: {4444})
"""
self.words = words
selenium_path = "http://" + selenium_host + ":" + str(selenium_port) + "/wd/hub"
# Set the logger
# Set the logger
self.logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.DEBUG)
self.logger.setLevel(logging.INFO)logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)
# setup the profile
profile = webdriver.FirefoxProfile()
# Override if we want mobile
if browser_type is not self.BROWSER_DESKTOP:
profile.set_preference("general.useragent.override","Mozilla/5.0 (Android 6.0.1; Mobile; rv:63.0) Gecko/63.0 Firefox/63.0")
self.logger.info("setting user-agent to mobile")
#Initialize the driver
self.driver = webdriver.Remote(selenium_path, DesiredCapabilities.FIREFOX, browser_profile=profile)
def login(self):
"""[summary]
Keyword Arguments:
browser_type {[type]} -- [description] (default: {self.BROWSER_DESKTOP})
"""
try:
self.driver.get('https://login.live.com')
EMAILFIELD = (By.ID, "i0116")
PASSWORDFIELD = (By.ID, "i0118")
NEXTBUTTON = (By.ID, "idSIButton9")
# wait for email field and enter email
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(EMAILFIELD)).send_keys(EMAIL)
# Click Next
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()
# wait for password field and enter password
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(PASSWORDFIELD)).send_keys(PASSWORD)
# Click Login - same id?
WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()
self.logger.info("logged into microsoft.")
self.driver.get(self.URL_BASE)
self.wait_for(3)
except Exception as e1:
print(e1)
self.wait_for(4)
def perform_search(self):
"""[summary]
"""
for num, word in enumerate(self.words):
self.search_word(word)
self.wait_for()
def search_word(self, word):
"""[summary]
Arguments:
word {[type]} -- [description]
"""
try:
self.driver.get(self.URL_BASE + word)
self.logger.info("searching for word: "% (word))
except Exception as e1:
print(e1)
def wait_for(self, sec=2):
"""[summary]
Keyword Arguments:
sec {int} -- [description] (default: {2})
"""
self.logger.debug("sleeping for %i seconds." % sec)
time.sleep(sec)
def close(self):
"""[summary]
"""
self.driver.close()
def get_env(env_var):
"""[summary]
Arguments:
env_var {[type]} -- [description]
Returns:
[type] -- [description]
"""
val = os.getenv(env_var)
is_set_or_die(val, env_var)
return val
def is_set_or_die( var_to_check, var_name ):
""" Bails on execution if a variable is not set
Arguments:
var_to_check {[type]} -- [description]
var_name {[type]} -- [description]
"""
if var_to_check == None or len(var_to_check) < 1:
print("[FATAL] %s is not set. ")
sys.exit(1)
return
def main():
"""[summary]
"""
email = get_env("MICROSOFT_EMAIL" )
password = get_env("MICROSOFT_PASSWORD")
selenium_host = get_env("SELENIUM_HOST")
selenium_port = get_env("SELENIUM_PORT")
# Perform Mobile Search
b_bot = BingRewardsBot(get_random_words(), browser_type="mobile", email=email, password=password, selenium_host=selenium_host, selenium_port=selenium_port)
b_bot.login()
b_bot.perform_search()
b_bot.close()
# Desktop Search
b_bot = BingRewardsBot(get_random_words(), email=email, password=password, selenium_host=selenium_host, selenium_port=selenium_port)
b_bot.login()
b_bot.perform_search()
b_bot.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment