Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanth Vikash authored and Hemanth Vikash committed Aug 4, 2022
0 parents commit 18ab4cc
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Vs code stuff
.vscode/
__pycache__

# Mac stuff
.DS_Store




Binary file added chromedriver
Binary file not shown.
29 changes: 29 additions & 0 deletions common_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



def wait_until_class(webdriver, classname):
try:
wait = WebDriverWait(webdriver, 10)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, classname)))
return element
except:
return None

def check_exists_by_xpath(webdriver, xpath):
try:
webdriver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True


def check_exists_by_class(webdriver, classname):
try:
webdriver.find_element_by_class_name(classname)
except NoSuchElementException:
return False
return True
128 changes: 128 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

import settings
import common_methods as cm
import time

import json
import sys
import random
import requests
import timeit

useless_phones = {
'https://www.visible.com/shop/smartphones/iphone-se': "Apple iPhone SE",
'https://www.visible.com/shop/smartphones/iphone-11': "Apple iPhone 11",
'https://www.visible.com/shop/smartphones/iphone-xs-pre-owned': "Apple iPhone XS Pre-Owned",
'https://www.visible.com/shop/smartphones/iphone-xr-pre-owned': "Apple iPhone XR",
"https://www.visible.com/shop/smartphones/iphone-x-pre-owned": "Apple iPhone X",
'https://www.visible.com/shop/smartphones/iphone-8-pre-owned': "Apple iPhone 8"

}

class Scraper:

def __init__(self):
self.driver = webdriver.Chrome(executable_path='./chromedriver')

def test_driver(self):
self.driver.get('https://www.google.com/')
xpath = '/html/body/div[1]/div[2]/div/img'
if (cm.check_exists_by_xpath(self.driver, xpath)):
print("Driver works")
else:
print("Ripperoni")




def light_pass(self):
print(f"Getting {settings.url} ...")
self.driver.get(settings.url)

try:
phones_container = self.driver.find_element_by_class_name('TileWrapper-sc-1xbwotf-1')
except Exception as e:
print(e)
return None
if (phones_container is not None):
phones = phones_container.find_elements_by_class_name('Tile-sc-1xbwotf-2')

for phone in phones:
top_banner = phone.find_element_by_class_name('TileTopBannerSection-sc-1xbwotf-37')
in_stock = True
if (cm.check_exists_by_class(top_banner, 'OutOfStock-sc-1xbwotf-41')):
in_stock = False
phone_detail = phone.find_element_by_class_name('TileMiddleSection-sc-1xbwotf-38')
phone_name = phone_detail.find_element_by_tag_name('span').text

else:
in_stock = True
phone_detail = phone.find_element_by_class_name('TileMiddleSection-sc-1xbwotf-38')
link = phone_detail.find_element_by_tag_name('a').get_attribute('href')
phone_name = phone_detail.find_element_by_tag_name('span').text
if (link not in useless_phones):
print(phone_name, "is available. Head over to: ", link, "NOW!")
slack_notify(link, phone_name)







def __del__(self):
self.driver.quit()


def slack_notify(phone_link, phone_name):
url = "https://hooks.slack.com/services/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"
message = (f"A {phone_name} has been spotted! Go to {phone_link} to get it!")
title = (f"Company Phone Available!")
slack_data = {
"username": "NotificationBot",
"icon_emoji": ":iphone:",
#"channel" : "#somerandomcahnnel",
"attachments": [
{
"color": "#9733EE",
"fields": [
{
"title": title,
"value": message,
"short": "false",
}
]
}
]
}
byte_length = str(sys.getsizeof(slack_data))
headers = {'Content-Type': "application/json", 'Content-Length': byte_length}
response = requests.post(url, data=json.dumps(slack_data), headers=headers)
if response.status_code != 200:
raise Exception(response.status_code, response.text)


def main():
scraper = Scraper()
print('Starting cycle')
start = time.time()
while(1):
try:
scraper.light_pass()
except WebDriverException as e:
print(e)

end = time.time()
print('Time elapsed: ', end - start, "seconds")

lag = 30
print(f"Sleeping for {lag} seconds")
time.sleep(lag)

if __name__ == "__main__":
settings.main()
main()
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
selenium
validators
requests
30 changes: 30 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import validators

url = 'https://www.visible.com/shop/smartphones/apple'

def __modify_url():
global url
url = url.strip()


def __validate_url():
global url
__modify_url()
if ("https" not in url):
raise RuntimeWarning("The url entered is not safe. Driver may fail at runtime")
return validators.url(url)




def main():
global url
flag = True
while(flag):
# url = input('Copy-paste the link for scraping: ')
if(__validate_url()):
print("Accepted url")
flag = False
else:
print("Error with the url. Just copy-paste bruh..")

0 comments on commit 18ab4cc

Please sign in to comment.