-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.py
126 lines (94 loc) · 4 KB
/
item.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from database import Database
from webdriver import Webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import requests
import inspect
class Item:
def __init__(self, item_id, name, category, main_link, price):
self.item_id = item_id
self.name = name
self.category = category
self.main_link = main_link
self.price = price
@classmethod
def register(cls, name, category, links, main_link, price):
db = Database()
cursor = db.database.cursor()
query = "INSERT INTO items (name, category, main_link, price) VALUES (%s, %s, %s, %s)"
cursor.execute(query, [name, category, main_link, price])
db.database.commit()
item_id = cursor.lastrowid
for link in links:
query = "INSERT INTO links (item_id, link) VALUES (%s, %s)"
cursor.execute(query, [item_id, link])
db.database.commit()
cursor.close()
db.database.close()
item = cls(item_id, name, category, main_link, price)
return item
@classmethod
def load(cls, item_id):
db = Database()
cursor = db.database.cursor()
query = "SELECT item_id, name, category, main_link, price FROM items WHERE item_id = %s"
cursor.execute(query, [item_id])
item_info = cursor.fetchone()
if item_info is None:
raise ValueError("No such item exists")
item = cls(*item_info)
return item
def __str__(self) -> str:
return f"Item({self.name}, {self.item_id})"
def __repr__(self) -> str:
return f"Item({self.name}, {self.item_id})"
def getLinks(self):
'''returns a list of links registered for a particular item'''
db = Database()
cursor = db.database.cursor()
query = "SELECT link FROM links WHERE item_id = %s"
cursor.execute(query, [self.item_id])
links = cursor.fetchall()
links = [link[0] for link in links]
return links
def getImagesFromLinks(self):
'''returns a list containing images obtained from item links'''
wd = Webdriver()
wd.driver.get(self.main_link)
shop_image_elements = wd.driver.find_elements(By.CLASS_NAME, "shop-logo")
shop_images = []
shop_names = []
for shop_image_element in shop_image_elements:
url = shop_image_element.get_attribute('src')
shop_name = shop_image_element.get_attribute('alt')
shop_names.append(shop_name)
try:
image = requests.get(url).content
except:
url = shop_image_element.get_attribute('data-lazysrc')
image = requests.get(url).content
shop_images.append(image)
return list(zip(shop_images, shop_names))
def getPricesFromLinks(self):
'''returns a list of prices registered for an item from links'''
wd = Webdriver()
wd.driver.get(self.main_link)
shop_price_elements = wd.driver.find_elements(By.CLASS_NAME, "shop-price")
shop_prices = []
for shop_price_element in shop_price_elements:
shop_prices.append(shop_price_element.text)
shop_prices = [shop_price.split('\n')[-1] for shop_price in shop_prices]
wd.driver.quit()
return shop_prices
def getImage(self):
'''returns an image of the item from the main link'''
# print("...Getting Image...")
# previous_frame = inspect.currentframe().f_back
# print(inspect.getframeinfo(previous_frame))
page = requests.get(self.main_link)
soup = BeautifulSoup(page.text, 'lxml')
item_image_element = soup.find("img", {"id" : "ContentPlaceHolder1_rptMainSlider_ImgSlideItem_0"})
item_image_url = item_image_element.get('src')
item_image = requests.get(item_image_url).content
# print("...Got Image...")
return item_image