1 | import json
|
---|
2 | import unicodedata
|
---|
3 | from datetime import datetime
|
---|
4 |
|
---|
5 | import psycopg2
|
---|
6 | import config_read
|
---|
7 | from bs4 import BeautifulSoup
|
---|
8 | import requests
|
---|
9 | import sys
|
---|
10 |
|
---|
11 | from classes.phoneoffer import PhoneOffer
|
---|
12 |
|
---|
13 | file_path = 'outputfile.txt'
|
---|
14 | sys.stdout = open(file_path, "w")
|
---|
15 |
|
---|
16 |
|
---|
17 | mobitech_url = "https://mobitech.mk/shop/"
|
---|
18 |
|
---|
19 | response1 = requests.get(mobitech_url)
|
---|
20 |
|
---|
21 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
22 |
|
---|
23 | phones = soup1.find_all('div', {'class': 'jet-woo-products__inner-box'})
|
---|
24 |
|
---|
25 | offer_shop = "Mobitech" # offer shop
|
---|
26 | last_updated = datetime.now().date()
|
---|
27 | is_validated = False
|
---|
28 |
|
---|
29 | # Mobitech phone offers that are already in database
|
---|
30 |
|
---|
31 | offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/mobitech').text))
|
---|
32 |
|
---|
33 | database_offers = []
|
---|
34 |
|
---|
35 | for offer in offers:
|
---|
36 | phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'],
|
---|
37 | offer['ram_memory'],
|
---|
38 | offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'],
|
---|
39 | offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'],
|
---|
40 | offer['image_url'],
|
---|
41 | offer['offer_url'], offer['last_updated'], offer['is_validated'],
|
---|
42 | offer['offer_description'],
|
---|
43 | offer['offer_shop_code'])
|
---|
44 | database_offers.append(phoneOffer)
|
---|
45 |
|
---|
46 | new_offers = []
|
---|
47 |
|
---|
48 | for phone in phones:
|
---|
49 | offer_url = phone.find('h5', {'class': 'jet-woo-product-title'}).find('a').get('href') # url
|
---|
50 | image_url = phone.find('div', {'class': 'jet-woo-product-thumbnail'}).find('img').get('src') # image
|
---|
51 | brand = phone.find_next('div', {'class': 'jet-woo-product-categories'}).find('a').get_text().strip() # brand
|
---|
52 | offer_name = phone.find('h5', {'class': 'jet-woo-product-title'}).find('a').get_text().strip() # offer_name
|
---|
53 | if brand not in offer_name:
|
---|
54 | offer_name = brand+" "+offer_name
|
---|
55 | temp_prices = phone.find('div', {'class': 'jet-woo-product-price'}).find_all('bdi')
|
---|
56 | price = int(float(temp_prices[len(temp_prices) - 1].get_text().replace("ден", "").replace(",", "").strip())) # price
|
---|
57 |
|
---|
58 | response2 = requests.get(offer_url)
|
---|
59 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
60 |
|
---|
61 | specifications = soup2.find_all('h2', {'class': 'elementor-heading-title elementor-size-default'})
|
---|
62 |
|
---|
63 | ram_memory = None
|
---|
64 | rom_memory = None
|
---|
65 | battery = None
|
---|
66 | back_camera = None
|
---|
67 | front_camera = None
|
---|
68 | operating_system = None
|
---|
69 | chipset = None
|
---|
70 | color = None
|
---|
71 | offer_shop_code = None
|
---|
72 | cpu = None
|
---|
73 | offer_description = None
|
---|
74 |
|
---|
75 | for specification in specifications:
|
---|
76 | # rom memory
|
---|
77 | if specification.get_text().startswith("Меморија:"):
|
---|
78 | rom_memory = specification.get_text().split("Меморија:")[1].strip()
|
---|
79 | if rom_memory == "Нема" or rom_memory == "/":
|
---|
80 | rom_memory = None
|
---|
81 |
|
---|
82 | # ram memory
|
---|
83 | if specification.get_text().startswith("РАМ Меморија:"):
|
---|
84 | ram_memory = specification.get_text().split("РАМ Меморија:")[1].replace('RAM', '')\
|
---|
85 | .replace('Ram', '').strip()
|
---|
86 | if ram_memory == "Нема" or ram_memory == "/":
|
---|
87 | ram_memory = None
|
---|
88 |
|
---|
89 | # camera
|
---|
90 | if specification.get_text().startswith("Камера:"):
|
---|
91 | back_camera = specification.get_text().split("Камера:")[1].strip()
|
---|
92 | if back_camera == "Нема":
|
---|
93 | back_camera = None
|
---|
94 |
|
---|
95 | # operating system
|
---|
96 | if specification.get_text().startswith("Оперативен систем:"):
|
---|
97 | operating_system = specification.get_text().split("Оперативен систем:")[1].split(",")[0].strip()
|
---|
98 | if operating_system == "Нема":
|
---|
99 | operating_system = None
|
---|
100 |
|
---|
101 | # battery
|
---|
102 | if specification.get_text().startswith("Батерија:"):
|
---|
103 | battery = specification.get_text().split("Батерија:")[1].strip()
|
---|
104 | if battery == "Нема":
|
---|
105 | battery = None
|
---|
106 |
|
---|
107 | new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
|
---|
108 | color, front_camera, back_camera, chipset, battery, operating_system, cpu,
|
---|
109 | image_url,
|
---|
110 | offer_url, last_updated, is_validated, offer_description, offer_shop_code))
|
---|
111 |
|
---|
112 | for new_offer in new_offers:
|
---|
113 | flag = False
|
---|
114 | flag_price = False
|
---|
115 | offer_id = None
|
---|
116 |
|
---|
117 | for old_offer in database_offers:
|
---|
118 |
|
---|
119 | if new_offer.offer_name == old_offer.offer_name:
|
---|
120 | flag = True
|
---|
121 | if new_offer.price != old_offer.price:
|
---|
122 | flag_price = True
|
---|
123 | offer_id = old_offer.offer_id
|
---|
124 |
|
---|
125 | if flag:
|
---|
126 | print('ALREADY IN DATABASE')
|
---|
127 | print(new_offer)
|
---|
128 | # if it's already in database, check PRICE and if it's changed, change it !!!!!!
|
---|
129 | if flag_price:
|
---|
130 | print('PRICE CHANGED!') # CHANGE PRICE
|
---|
131 | print('offer id: ' + str(offer_id))
|
---|
132 | headers = {'Content-type': 'application/json'}
|
---|
133 | requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
|
---|
134 | headers=headers)
|
---|
135 | else:
|
---|
136 | print('ADDED') # ADD OFFER
|
---|
137 | print(new_offer)
|
---|
138 | headers = {'Content-type': 'application/json'}
|
---|
139 | requests.post('http://localhost:8080/phoneoffer/addoffer',
|
---|
140 | headers=headers, data=json.dumps(new_offer.__dict__, default=str))
|
---|
141 |
|
---|
142 | print('------------------------------------')
|
---|
143 |
|
---|
144 | for old_offer in database_offers:
|
---|
145 | flag = False
|
---|
146 | for new_offer in new_offers:
|
---|
147 | if old_offer.offer_name == new_offer.offer_name:
|
---|
148 | flag = True
|
---|
149 |
|
---|
150 | if not flag:
|
---|
151 | print('OFFER DELETED')
|
---|
152 | print(old_offer)
|
---|
153 | # DELETE OFFER
|
---|
154 | requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
|
---|
155 |
|
---|