-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing_rewards_utils.py
90 lines (76 loc) · 3.24 KB
/
bing_rewards_utils.py
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
import json
import random
import time
from os import listdir
import httpx
from selenium import common, webdriver
from selenium.common import WebDriverException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options as EdgeOptions
from constants import MSEDGEDRIVER, RANDOM_WORDS_URL, GECKODRIVER, BING_LOGIN_URL
from get_webdriver import save_webdriver
def wait_for(sec=2):
time.sleep(sec)
def get_driver_firefox(mobile=False):
profile = webdriver.FirefoxProfile()
options = FirefoxOptions()
options.headless = True
options.add_argument("--disable-notifications")
if mobile:
profile.set_preference("general.useragent.override",
"Mozilla/5.0 (Android 8.0.0; Mobile; rv:63.0) Gecko/63.0 Firefox/63.0")
try:
return webdriver.Firefox(firefox_profile=profile, options=options,
service=FirefoxService(f"{GECKODRIVER}/{listdir(GECKODRIVER)[0]}"))
except FileNotFoundError:
save_webdriver(GECKODRIVER)
return get_driver_firefox(mobile=mobile)
def get_driver_edge(mobile=False, service=False):
options = EdgeOptions()
options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
options.add_argument("--disable-notifications")
options.add_argument('--disable-gpu')
if mobile:
mobile_emulation = {"deviceName": "Galaxy S5"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
options.add_argument('--headless')
try:
return webdriver.Edge(options=options,
service=EdgeService(f"{MSEDGEDRIVER}/{listdir(MSEDGEDRIVER)[0]}") if service else None)
except (FileNotFoundError, WebDriverException):
save_webdriver(MSEDGEDRIVER)
return get_driver_edge(mobile=mobile, service=True)
def get_word_list(search_count):
response = httpx.get(RANDOM_WORDS_URL, follow_redirects=True)
words_list = random.sample(json.loads(response.text)['data'], search_count)
print(f'{len(words_list)} words selected')
return words_list
def login(driver, email, password):
try:
print('Logging with microsoft credentials\n')
driver.get(BING_LOGIN_URL)
wait_for(5)
mail_element = driver.find_element('name', 'loginfmt')
mail_element.clear()
mail_element.send_keys(email)
mail_element.send_keys(Keys.RETURN)
wait_for(5)
password_element = driver.find_element('name', 'passwd')
password_element.clear()
password_element.send_keys(password)
password_element.send_keys(Keys.ENTER)
if driver.name == 'firefox':
try:
wait_for(5)
checkbox_element = driver.find_element('name', 'DontShowAgain')
checkbox_element.click()
checkbox_element.submit()
except common.exceptions.NoSuchElementException:
pass
except Exception as e:
driver.close()
raise SystemExit(e)
wait_for(5)