[895cd87] | 1 | import json
|
---|
[b68ae8d] | 2 | import unicodedata
|
---|
| 3 | from datetime import datetime
|
---|
| 4 | import psycopg2
|
---|
| 5 | import config_read
|
---|
| 6 | from bs4 import BeautifulSoup
|
---|
| 7 | from selenium import webdriver
|
---|
| 8 | import requests
|
---|
| 9 |
|
---|
| 10 | import sys
|
---|
| 11 |
|
---|
[895cd87] | 12 | from classes.phoneoffer import PhoneOffer
|
---|
| 13 |
|
---|
[b68ae8d] | 14 | file_path = 'outputfile.txt'
|
---|
| 15 | sys.stdout = open(file_path, "w")
|
---|
| 16 |
|
---|
| 17 | offer_shop = "Ledikom" # offer shop
|
---|
| 18 | last_updated = datetime.now().date()
|
---|
| 19 | is_validated = False
|
---|
| 20 |
|
---|
[895cd87] | 21 | # Ledikom phone offers that are already in database
|
---|
| 22 |
|
---|
| 23 | offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/ledikom').text))
|
---|
| 24 |
|
---|
| 25 | database_offers = []
|
---|
| 26 |
|
---|
| 27 | for offer in offers:
|
---|
| 28 | phoneOffer = PhoneOffer(offer['id'], offer['offer_shop'], offer['offer_name'], offer['price'],
|
---|
| 29 | offer['ram_memory'],
|
---|
| 30 | offer['rom_memory'], offer['color'], offer['front_camera'], offer['back_camera'],
|
---|
| 31 | offer['chipset'], offer['battery'], offer['operating_system'], offer['cpu'],
|
---|
| 32 | offer['image_url'],
|
---|
| 33 | offer['offer_url'], offer['last_updated'], offer['is_validated'],
|
---|
| 34 | offer['offer_description'],
|
---|
| 35 | offer['offer_shop_code'])
|
---|
| 36 | database_offers.append(phoneOffer)
|
---|
| 37 |
|
---|
| 38 | new_offers = []
|
---|
| 39 |
|
---|
[b68ae8d] | 40 | ledikom_phone_urls = [
|
---|
| 41 | 'https://ledikom.mk/c/416/uredi/apple/iphone?limit=96',
|
---|
| 42 | 'https://ledikom.mk/c/421/uredi/samsung/telefoni?limit=96',
|
---|
| 43 | 'https://ledikom.mk/c/424/mobilni-telefoni/xiaomi/telefoni?limit=96',
|
---|
| 44 | 'https://ledikom.mk/c/430/uredi/huawei/telefoni?limit=96',
|
---|
| 45 | 'https://ledikom.mk/c/441/uredi/oneplus/telefoni?limit=96',
|
---|
| 46 | 'https://ledikom.mk/c/413/uredi/google/telefoni?limit=96',
|
---|
| 47 | 'https://ledikom.mk/c/411/uredi/honor/telefoni?limit=96',
|
---|
| 48 | 'https://ledikom.mk/c/460/uredi/nokia/telefoni?limit=96',
|
---|
| 49 | 'https://ledikom.mk/c/461/uredi/asus/telefoni?limit=96',
|
---|
| 50 | 'https://ledikom.mk/c/488/proizvodi/oppo/telefoni?limit=96'
|
---|
| 51 | ]
|
---|
| 52 |
|
---|
| 53 | for ledikom_url in ledikom_phone_urls:
|
---|
| 54 |
|
---|
| 55 | # selenium is used because of the dynamic content of the page
|
---|
| 56 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 57 | driver1.get(ledikom_url)
|
---|
| 58 | ledikom_html = driver1.page_source
|
---|
| 59 |
|
---|
| 60 | # closing the driver so the safari instance can pair with another webdriver session
|
---|
| 61 | driver1.close()
|
---|
| 62 |
|
---|
| 63 | soup1 = BeautifulSoup(ledikom_html, 'html.parser')
|
---|
| 64 |
|
---|
| 65 | phones = soup1.find('div', {'id': 'content'}) \
|
---|
| 66 | .find('div', {'class': 'container'}).find('div', {'class': 'row'}).find('div', {'class': 'item-display'}) \
|
---|
| 67 | .find_all('div', {'class': 'item-in-grid'})
|
---|
| 68 |
|
---|
| 69 | if len(phones) == 0:
|
---|
| 70 | continue
|
---|
| 71 |
|
---|
| 72 | for phone in phones:
|
---|
| 73 | offer_url = 'https://ledikom.mk' + phone.find('a').get('href')
|
---|
| 74 | image_url = phone.find('a').find('img').get('src')
|
---|
| 75 | temp_offer_name = phone.find('div', {'class': 'item-name'}).find('a').get_text().strip()
|
---|
| 76 | offer_name = ' '.join(temp_offer_name.split())
|
---|
| 77 | brand = offer_name.split(' ')[0]
|
---|
[895cd87] | 78 | price = int(phone.find('span', {'class': 'price'}).get_text().replace('ден.', '')
|
---|
| 79 | .replace('ден', '')
|
---|
| 80 | .replace('.', '').strip())
|
---|
[b68ae8d] | 81 |
|
---|
| 82 | driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
|
---|
| 83 | driver1.get(offer_url)
|
---|
| 84 | # getting offer page html
|
---|
| 85 | offer_html = driver1.page_source
|
---|
| 86 | driver1.close()
|
---|
| 87 |
|
---|
| 88 | soup2 = BeautifulSoup(offer_html, 'html.parser')
|
---|
| 89 |
|
---|
| 90 | specifications = soup2.find('div', {'id': 'content'}).find('section', {'class': 'padding-section'}) \
|
---|
| 91 | .find_all('div', {'class': 'container'})[1].find('div', {'class': 'col-md-7'}) \
|
---|
| 92 | .find_all('div', {'class': 'row'})
|
---|
| 93 |
|
---|
| 94 | color = None
|
---|
| 95 | rom_memory = None
|
---|
| 96 | ram_memory = None
|
---|
[895cd87] | 97 | back_camera = None
|
---|
| 98 | operating_system = None
|
---|
| 99 | chipset = None
|
---|
| 100 | battery = None
|
---|
| 101 | cpu = None
|
---|
| 102 | front_camera = None
|
---|
| 103 | offer_shop_code = None
|
---|
| 104 | offer_description = None
|
---|
[b68ae8d] | 105 |
|
---|
| 106 | if len(specifications) != 0:
|
---|
| 107 | colors_tags = specifications[0].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 108 | temp_colors = []
|
---|
| 109 | for color_tag in colors_tags:
|
---|
| 110 | temp_colors.append(color_tag.get_text().strip())
|
---|
| 111 | color = ','.join(temp_colors)
|
---|
| 112 |
|
---|
| 113 | if len(specifications) >= 2:
|
---|
| 114 | temp_rom = specifications[1].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 115 | rom_list = []
|
---|
| 116 | for rom in temp_rom:
|
---|
| 117 | rom_list.append(rom.get('title'))
|
---|
| 118 | rom_memory = ','.join(rom_list)
|
---|
| 119 |
|
---|
| 120 | if len(specifications) >= 3:
|
---|
| 121 | temp_ram = specifications[2].find('div', {'class': 'col-md-12 col-xs-12'}).find_all('a')
|
---|
| 122 | ram_list = []
|
---|
| 123 | for ram in temp_ram:
|
---|
| 124 | ram_list.append(ram.get('title'))
|
---|
| 125 |
|
---|
| 126 | ram_memory = ','.join(ram_list)
|
---|
| 127 |
|
---|
| 128 | if 'Xiaomi' in brand:
|
---|
| 129 | temp = color
|
---|
| 130 | color = rom_memory
|
---|
| 131 | rom_memory = temp
|
---|
| 132 |
|
---|
| 133 | temp = ram_memory
|
---|
| 134 | ram_memory = color
|
---|
| 135 | color = temp
|
---|
| 136 |
|
---|
[895cd87] | 137 | new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
|
---|
| 138 | color, front_camera, back_camera, chipset, battery, operating_system, cpu,
|
---|
| 139 | image_url,
|
---|
| 140 | offer_url, last_updated, is_validated, offer_description, offer_shop_code))
|
---|
| 141 |
|
---|
| 142 | for new_offer in new_offers:
|
---|
| 143 | flag = False
|
---|
| 144 | flag_price = False
|
---|
| 145 | offer_id = None
|
---|
| 146 |
|
---|
| 147 | for old_offer in database_offers:
|
---|
| 148 |
|
---|
| 149 | if new_offer.offer_name == old_offer.offer_name:
|
---|
| 150 | flag = True
|
---|
| 151 | if new_offer.price != old_offer.price:
|
---|
| 152 | flag_price = True
|
---|
| 153 | offer_id = old_offer.offer_id
|
---|
| 154 |
|
---|
| 155 | if flag:
|
---|
| 156 | # print('ALREADY IN DATABASE')
|
---|
| 157 | # print(new_offer)
|
---|
| 158 | # if it's already in database, check PRICE and if it's changed, change it !!!!!!
|
---|
| 159 | if flag_price:
|
---|
| 160 | print('PRICE CHANGED!') # CHANGE PRICE
|
---|
| 161 | print('offer id: ' + str(offer_id))
|
---|
| 162 | headers = {'Content-type': 'application/json'}
|
---|
| 163 | requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
|
---|
| 164 | headers=headers)
|
---|
| 165 | else:
|
---|
| 166 | print('ADDED') # ADD OFFER
|
---|
| 167 | print(new_offer)
|
---|
| 168 | headers = {'Content-type': 'application/json'}
|
---|
| 169 | requests.post('http://localhost:8080/phoneoffer/addoffer',
|
---|
| 170 | headers=headers, data=json.dumps(new_offer.__dict__, default=str))
|
---|
| 171 |
|
---|
| 172 | print('------------------------------------')
|
---|
| 173 |
|
---|
| 174 | for old_offer in database_offers:
|
---|
| 175 | flag = False
|
---|
| 176 | for new_offer in new_offers:
|
---|
| 177 | if old_offer.offer_name == new_offer.offer_name:
|
---|
| 178 | flag = True
|
---|
| 179 |
|
---|
| 180 | if not flag:
|
---|
| 181 | print('OFFER DELETED')
|
---|
| 182 | print(old_offer)
|
---|
| 183 | # DELETE OFFER
|
---|
| 184 | requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id)) |
---|