forked from JustinShenk/wg-activator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg-updater.py
98 lines (78 loc) · 2.99 KB
/
wg-updater.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
91
92
93
94
95
96
97
98
import logging
import os
import random
import time
from configparser import ConfigParser
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException, WebDriverException
from selenium.webdriver.common.keys import Keys
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s:%(asctime)-15s:%(message)s'
)
def login():
driver = webdriver.Remote("http://{}/wd/hub".format(SELENIUM_HOST), DesiredCapabilities.FIREFOX)
driver.get('https://wg-gesucht.de')
# Remove cookie button
driver.find_element_by_partial_link_text("Accept").click()
driver.find_element_by_partial_link_text("Konto").click()
time.sleep(1)
username = driver.find_element_by_name("login_email_username")
password = driver.find_element_by_id("login_password")
username.send_keys(USERNAME)
password.send_keys(PASSWORD)
driver.find_element_by_id('login_submit').click()
time.sleep(5)
return driver
def reload(driver):
# Open listing
driver.get(EDIT_URL)
time.sleep(3)
# Refresh listing
def orange_button():
return driver.find_element_by_class_name('btn-orange')
# weiter
orange_button().send_keys(Keys.TAB)
time.sleep(2)
orange_button().send_keys(Keys.SPACE)
# Änderungen ubernehmen
orange_button().send_keys(Keys.TAB)
time.sleep(2)
orange_button().send_keys(Keys.SPACE)
if __name__ == '__main__':
CONFIG_PATH = os.environ.get("CONFIG_PATH")
SELENIUM_HOST = os.environ.get("SELENIUM_HOST")
logging.info(f"Config path: {CONFIG_PATH}")
logging.info(f"Selenium host: {SELENIUM_HOST}")
# Parse configuration
config = ConfigParser()
with open(CONFIG_PATH) as conf_file:
config.read_file(conf_file)
USERNAME = config.get('Login', 'email')
PASSWORD = config.get('Login', 'password')
LISTING_ID = config.get('Listing', 'listing_id')
LISTING_URL = "https://www.wg-gesucht.de/{}.html".format(LISTING_ID)
EDIT_URL = "https://www.wg-gesucht.de/angebot-bearbeiten.html?action=update_offer&offer_id={}".format(LISTING_ID)
DELAY = int(config.get('Driver', 'delay'))
MARGIN = float(config.get('Driver', 'margin')) or 0.0
logging.info('Starting up...')
web_driver = login()
while True:
try:
logging.info('Starting to reload')
reload(web_driver)
logging.info("Reloaded")
except NoSuchElementException as e:
logging.error(f"{e}\nTry to increase the waiting time if the page wasn't fully loaded")
except WebDriverException as e:
logging.error(e)
web_driver.quit()
web_driver = login()
# offset = percent of margin around delay
if MARGIN != 0.0:
offset = int(DELAY * ((random.random() * MARGIN * 2.0) - MARGIN))
else:
offset = 0.0
logging.info(f"Sleeping for {(DELAY + offset) // 60}min")
time.sleep(DELAY + offset)