Created
July 4, 2023 09:59
-
-
Save josifoski/baacec2b0896a046638551d343e369f0 to your computer and use it in GitHub Desktop.
Target.com.au
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Script for buying ps5 | |
# Aleksandar Josifoski about.me/josifsk for Timothy Yang via upwork | |
# Date created: 2022 January | |
# Dependencies: | |
# pip3 install -U selenium | |
######################################################################### | |
import getpass | |
username = getpass.getuser() | |
comp = username | |
if comp == 'josifoski': | |
dir_in = '/data/upwork/Timothy_Yang/' | |
geckodriver_path = '/data/Scrape/geckodriver' | |
firefox_profile_path = '/data/thunderbird-profile/ff-profiles/timothy' | |
else: | |
dir_in = '/home/ubuntu/amznau/' | |
geckodriver_path = '/home/ubuntu/amznau/geckodriver' | |
firefox_profile_path = '/home/ubuntu/amznau/ffprofile/timothy' | |
brows = 'firefox' | |
headless = False | |
import codecs | |
import datetime | |
timeout = 10 | |
logfile_name = 'sonylog' + '.txt' | |
oldPrint = print | |
def print(*args, **kwargs): | |
'''This function prints both on stdout and log file''' | |
now = str(datetime.datetime.now())[:16] | |
with codecs.open(dir_in + logfile_name, 'a', 'utf8') as fout: | |
fout.write(now + " ") | |
try: | |
fout.write(str(*args), **kwargs) | |
except: | |
pass | |
fout.write(os.linesep) | |
try: | |
oldPrint(*args, **kwargs) | |
except: | |
pass | |
######################################################################### | |
from selenium import webdriver | |
from selenium.webdriver.firefox.options import Options | |
from selenium.webdriver.firefox.service import Service | |
from selenium.webdriver.common.keys import Keys | |
from selenium.common.exceptions import TimeoutException | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.action_chains import ActionChains | |
import sys | |
import os | |
import time | |
import traceback | |
import random | |
import requests | |
import re | |
import html | |
time1 = time.time() | |
driver = None | |
def openurl(url): | |
'''function to open url using selenium''' | |
global driver | |
try: | |
driver.get(url) | |
print('loading ' + url) | |
except Exception as e: | |
now = str(datetime.datetime.now())[:16] | |
print(str(e)) | |
def scroll_down(sbypx): | |
'''function to scroll by sbypx pixels''' | |
global driver | |
driver.execute_script("window.scrollBy(0, %d);" % (sbypx)) | |
time.sleep(0.5) | |
def setbrowser(): | |
''' function for preparing browser for automation ''' | |
print("Preparing browser") | |
global driver | |
# firefox | |
if brows.lower() in ('firefox',): | |
options = Options() | |
profile = webdriver.FirefoxProfile(firefox_profile_path) | |
#options.set_preference("javascript.enabled", True) | |
#options.profile = firefox_profile | |
#options.set_preference('network.proxy.type', 1) | |
#options.set_preference('network.proxy.socks', '127.0.0.1') | |
#options.set_preference('network.proxy.socks_port', 9050) | |
#options.set_preference('network.proxy.socks_remote_dns', False) | |
#options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36') | |
profile.set_preference("general.useragent.override", f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36') | |
if headless: | |
options.headless = True | |
service = Service(geckodriver_path) | |
#driver = webdriver.Firefox(service=service, options=options) | |
driver = webdriver.Firefox(firefox_profile=profile, service=service, options=options) | |
driver.maximize_window() | |
driver.implicitly_wait(timeout) | |
def calculate_time(): | |
'''function to calculate elapsed time''' | |
time2 = time.time() | |
hours = int((time2-time1)/3600) | |
minutes = int((time2-time1 - hours * 3600)/60) | |
sec = time2 - time1 - hours * 3600 - minutes * 60 | |
print("processed in %dh:%dm:%ds" % (hours, minutes, sec)) | |
def driverquit(): | |
''' quiting session ''' | |
global driver | |
driver.quit() | |
driver = None | |
calculate_time() | |
sys.exit() | |
def restartff(): | |
''' restart firefox ''' | |
global driver | |
driver.quit() | |
driver = None | |
setbrowser() | |
print('Preparing browser done') | |
login() | |
def login(): | |
''' login ''' | |
pass | |
def main(): | |
''' main function ''' | |
global driver | |
openurl('https://target.com.au') | |
some = input('press enter') | |
if __name__ == '__main__': | |
setbrowser() | |
print('Preparing browser done') | |
try: | |
main() | |
driverquit() | |
except Exception as e: | |
print(str(e)) | |
print(traceback.format_exc()) | |
driverquit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment