1 | import json
|
---|
2 | import unicodedata
|
---|
3 | from datetime import datetime
|
---|
4 | import psycopg2
|
---|
5 | import config_read
|
---|
6 | from bs4 import BeautifulSoup
|
---|
7 | import requests
|
---|
8 | import sys
|
---|
9 |
|
---|
10 | from classes.phoneoffer import PhoneOffer
|
---|
11 |
|
---|
12 | file_path = 'outputfile.txt'
|
---|
13 | sys.stdout = open(file_path, "w")
|
---|
14 |
|
---|
15 | offer_shop = "Setec" # offer shop
|
---|
16 | last_updated = datetime.now().date()
|
---|
17 | is_validated = False
|
---|
18 |
|
---|
19 | # Setec phone offers that are already in database
|
---|
20 |
|
---|
21 | offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/setec').text))
|
---|
22 |
|
---|
23 | database_offers = []
|
---|
24 |
|
---|
25 | for offer in offers:
|
---|
26 | phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'],
|
---|
27 | offer['ram_memory'],
|
---|
28 | offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'],
|
---|
29 | offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'],
|
---|
30 | offer['image_url'],
|
---|
31 | offer['offer_url'], offer['last_updated'], offer['is_validated'],
|
---|
32 | offer['offer_description'],
|
---|
33 | offer['offer_shop_code'])
|
---|
34 | database_offers.append(phoneOffer)
|
---|
35 |
|
---|
36 | new_offers = []
|
---|
37 |
|
---|
38 | for i in range(1, 9):
|
---|
39 | setec_url = 'https://setec.mk/index.php?route=product/category&path=10066_10067&page=' + str(i)
|
---|
40 |
|
---|
41 | response1 = requests.get(setec_url)
|
---|
42 | soup1 = BeautifulSoup(response1.content, 'html.parser')
|
---|
43 |
|
---|
44 | phones = soup1.find('div', {'id': 'mfilter-content-container'}) \
|
---|
45 | .find_all('div', {'class': 'col-sm-4 col-xs-6'})
|
---|
46 |
|
---|
47 | for phone in phones:
|
---|
48 | offer_url = phone.find('div', {'class': 'left'}).find('a').get('href')
|
---|
49 | image_url = phone.find('div', {'class': 'left'}).find('a').find('img').get('src')
|
---|
50 | offer_name = phone.find('div', {'class': 'right'}).find('div', {'class': 'name'}).find('a').get_text().strip()
|
---|
51 | brand = offer_name.split(' ')[0]
|
---|
52 |
|
---|
53 | back_camera = None
|
---|
54 | operating_system = None
|
---|
55 | chipset = None
|
---|
56 | battery = None
|
---|
57 | ram_memory = None
|
---|
58 | rom_memory = None
|
---|
59 | cpu = None
|
---|
60 | front_camera = None
|
---|
61 | color = None
|
---|
62 |
|
---|
63 | if 'Cable' in offer_name or 'AirTag' in offer_name:
|
---|
64 | continue
|
---|
65 |
|
---|
66 | if brand not in offer_name:
|
---|
67 | offer_name = brand + " " + offer_name
|
---|
68 |
|
---|
69 | offer_shop_code = phone.find('div', {'class': 'right'}) \
|
---|
70 | .find('div', {'class': 'shifra'}).get_text().replace('Шифра:', '').strip()
|
---|
71 |
|
---|
72 | price_tag = phone.find('div', {'class': 'right'}).find('div', {'class': 'price'}). \
|
---|
73 | find('div', {'class': 'category-price-redovna'}).find('span', {'class': 'price-old-new'})
|
---|
74 |
|
---|
75 | if price_tag is None:
|
---|
76 | price_tag = phone.find('div', {'class': 'right'}).find('div', {'class': 'price'}). \
|
---|
77 | find('div', {'class': 'category-price-redovna'}).find('span', {'class': 'cena_za_kesh'})
|
---|
78 |
|
---|
79 | price = int(price_tag.get_text().replace('Ден.', '').replace(',', '').strip())
|
---|
80 |
|
---|
81 | response2 = requests.get(offer_url)
|
---|
82 | soup2 = BeautifulSoup(response2.content, 'html.parser')
|
---|
83 |
|
---|
84 | offer_description = soup2.find('div', {'id': 'tab-description'}).get_text(separator='\n')
|
---|
85 |
|
---|
86 | new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
|
---|
87 | color, front_camera, back_camera, chipset, battery, operating_system, cpu,
|
---|
88 | image_url,
|
---|
89 | offer_url, last_updated, is_validated, offer_description, offer_shop_code))
|
---|
90 |
|
---|
91 | for new_offer in new_offers:
|
---|
92 | flag = False
|
---|
93 | flag_price = False
|
---|
94 | offer_id = None
|
---|
95 |
|
---|
96 | for old_offer in database_offers:
|
---|
97 |
|
---|
98 | if new_offer.offer_shop_code == old_offer.offer_shop_code:
|
---|
99 | flag = True
|
---|
100 | if new_offer.price != old_offer.price:
|
---|
101 | flag_price = True
|
---|
102 | offer_id = old_offer.offer_id
|
---|
103 |
|
---|
104 | if flag:
|
---|
105 | # print('ALREADY IN DATABASE')
|
---|
106 | # print(new_offer)
|
---|
107 | # if it's already in database, check PRICE and if it's changed, change it !!!!!!
|
---|
108 | if flag_price:
|
---|
109 | print('PRICE CHANGED!') # CHANGE PRICE
|
---|
110 | print('offer id: ' + str(offer_id))
|
---|
111 | headers = {'Content-type': 'application/json'}
|
---|
112 | requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
|
---|
113 | headers=headers)
|
---|
114 | else:
|
---|
115 | print('ADDED') # ADD OFFER
|
---|
116 | print(new_offer)
|
---|
117 | headers = {'Content-type': 'application/json'}
|
---|
118 | requests.post('http://localhost:8080/phoneoffer/addoffer',
|
---|
119 | headers=headers, data=json.dumps(new_offer.__dict__, default=str))
|
---|
120 |
|
---|
121 | print('------------------------------------')
|
---|
122 |
|
---|
123 | for old_offer in database_offers:
|
---|
124 | flag = False
|
---|
125 | for new_offer in new_offers:
|
---|
126 | if old_offer.offer_shop_code == new_offer.offer_shop_code:
|
---|
127 | flag = True
|
---|
128 |
|
---|
129 | if not flag:
|
---|
130 | print('OFFER DELETED')
|
---|
131 | print(old_offer)
|
---|
132 | # DELETE OFFER
|
---|
133 | requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id)) |
---|