-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenew.py
59 lines (49 loc) · 2.16 KB
/
renew.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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from dotenv import load_dotenv
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import os, argparse
from datetime import datetime
load_dotenv()
def config_selenium(headless):
selenium_options = Options()
# selenium_options.add_argument("--no-sandbox")
if not headless == False:
selenium_options.add_argument("--headless=new")
user_agent = os.getenv('USER_AGENT')
selenium_options.add_argument('user-agent={0}'.format(user_agent))
return selenium_options
def get_driver(home_url, headless):
selenium_options = config_selenium(headless)
driver = webdriver.Chrome(service=Service(), options=selenium_options)
driver.get(home_url)
return driver
def login(driver):
username = driver.find_element(by=By.NAME, value='inputEmailHandle')
username.send_keys(os.getenv('LOGIN_ID'))
password = driver.find_element(by=By.NAME, value='inputPassword')
password.send_keys(os.getenv('LOGIN_PW'))
driver.find_element(by=By.XPATH, value='//*[@id="login"]').click()
def check_for_renewals(driver):
renew_links = driver.find_elements(by=By.XPATH, value="//input[contains(@class, 'managebtn') and contains(@value, 'renew')]")
return renew_links
parser = argparse.ArgumentParser()
parser.add_argument('--headless', action=argparse.BooleanOptionalAction)
def main():
home_url = 'https://accounts.craigslist.org/login/home'
driver = get_driver(home_url, args.headless)
login(driver)
renew_links = check_for_renewals(driver)
print(f"{datetime.now()}: Found {len(renew_links)} listing(s) that can be renewed")
while len(renew_links):
renew_links[0].click()
renewed_listing = driver.find_element(by=By.ID, value='titletextonly').text
print(f"{datetime.now()}: Renewed {renewed_listing}")
driver.get(home_url)
renew_links = check_for_renewals(driver) #Refresh renewal list
print(f"{datetime.now()}: Found {len(renew_links)} more listing(s) that can be renewed")
driver.quit()
if __name__ == '__main__':
args = parser.parse_args()
main()