source: phonelux_scrappers/scrappers/neptun_scrapper.py@ 895cd87

Last change on this file since 895cd87 was 895cd87, checked in by Marko <Marko@…>, 21 months ago

Refactored code

  • Property mode set to 100644
File size: 6.6 KB
Line 
1import json
2import unicodedata
3from datetime import datetime
4import psycopg2
5import config_read
6from bs4 import BeautifulSoup
7from selenium import webdriver
8import requests
9
10import sys
11
12from classes.phoneoffer import PhoneOffer
13
14file_path = 'outputfile.txt'
15sys.stdout = open(file_path, "w")
16
17offer_shop = "Neptun" # offer shop
18last_updated = datetime.now().date()
19is_validated = False
20
21# Neptun phone offers that are already in database
22
23offers = json.loads(unicodedata.normalize('NFKD', requests.get('http://localhost:8080/phoneoffer/shop/neptun').text))
24
25database_offers = []
26
27for 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
38new_offers = []
39
40for i in range(1, 11):
41 neptun_url = 'https://www.neptun.mk/mobilni_telefoni.nspx?page=' + str(i)
42
43 # selenium is used because of the dynamic content of the page
44 driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
45 driver1.get(neptun_url)
46 neptun_html = driver1.page_source
47
48 # closing the driver so the safari instance can pair with another webdriver session
49 driver1.close()
50
51 # response1 = requests.get(neptun_url)
52 soup1 = BeautifulSoup(neptun_html, 'html.parser')
53
54 phones = soup1.find('div', {'id': 'mainContainer'}).find('div',
55 {'class': 'col-lg-9 col-md-9 col-sm-8 col-fix-main'}) \
56 .find_all('div', {'class': 'ng-scope product-list-item-grid'})
57
58 for phone in phones:
59 offer_url = 'https://www.neptun.mk' + phone.find('a').get('href')
60 offer_name = phone.find('a').find('h2').get_text().replace('MOB.TEL.', '').strip()
61 brand = offer_name.split(' ')[0].strip().capitalize()
62 image_url = 'https://www.neptun.mk' + phone.find('a').find('div', {'class': 'row'}).find('img').get('src')
63 price = int(
64 phone.find('div', {'class': 'col-sm-12 static'}).find('div', {'class': 'product-list-item__prices pt35'})
65 .find('div', {'class': 'row'}).find('div', {'class': 'newPriceModel'}) \
66 .find('span', {'class': 'product-price__amount--value ng-binding'}).get_text().replace('.', ''))
67
68 driver1 = webdriver.Safari(executable_path='/usr/bin/safaridriver')
69 driver1.get(offer_url)
70 offer_html = driver1.page_source
71 # closing the driver so the safari instance can pair with another webdriver session
72 driver1.close()
73
74 soup2 = BeautifulSoup(offer_html, 'html.parser')
75
76 offer_shop_code = soup2.find('div', {'ng-if': 'showProductDetails'}) \
77 .find('div', {'class': 'product-details-first-row'}).find('span', {
78 'ng-bind': 'model.CodeNumber'}).get_text().strip()
79
80 specifications_table = \
81 soup2.find('div', {'id': 'mainContainer'}).find('div', {'ng-if': 'showProductDetails'}).find_all('ul')[-1]
82 specifications = specifications_table.get_text(separator='\n').strip().split("\n")
83
84 offer_description = specifications_table.get_text(separator='\n').strip()
85
86 back_camera = None
87 operating_system = None
88 chipset = None
89 battery = None
90 ram_memory = None
91 rom_memory = None
92 cpu = None
93 front_camera = None
94 color = None
95
96 for specification in specifications:
97 if 'Батерија:' in specification:
98 battery = specification.split('Батерија:')[1]
99
100 if 'CPU:' in specification:
101 cpu = specification.split('CPU:')[1]
102
103 if 'Chipset:' in specification:
104 chipset = specification.split('Chipset:')[1]
105
106 if 'RAM Меморија:' in specification:
107 ram_memory = specification.split('RAM Меморија:')[1]
108 continue
109
110 if 'ROM Меморија:' in specification:
111 rom_memory = specification.split('ROM Меморија:')[1]
112 continue
113
114 if 'ROM:' in specification:
115 rom_memory = specification.split('ROM:')[1]
116
117 if 'RAM:' in specification:
118 ram_memory = specification.split('RAM:')[1]
119
120 if 'iOS' in specification or 'Android' in specification:
121 operating_system = specification
122
123 new_offers.append(PhoneOffer(offer_shop, offer_name, price, ram_memory, rom_memory,
124 color, front_camera, back_camera, chipset, battery, operating_system, cpu,
125 image_url,
126 offer_url, last_updated, is_validated, offer_description, offer_shop_code))
127
128for new_offer in new_offers:
129 flag = False
130 flag_price = False
131 offer_id = None
132
133 for old_offer in database_offers:
134
135 if new_offer.offer_shop_code == old_offer.offer_shop_code:
136 flag = True
137 if new_offer.price != old_offer.price:
138 flag_price = True
139 offer_id = old_offer.offer_id
140
141 if flag:
142 # print('ALREADY IN DATABASE')
143 # print(new_offer)
144 # if it's already in database, check PRICE and if it's changed, change it !!!!!!
145 if flag_price:
146 print('PRICE CHANGED!') # CHANGE PRICE
147 print('offer id: ' + str(offer_id))
148 headers = {'Content-type': 'application/json'}
149 requests.put('http://localhost:8080/phoneoffer/' + str(offer_id) + '/changeprice/' + str(new_offer.price),
150 headers=headers)
151 else:
152 print('ADDED') # ADD OFFER
153 print(new_offer)
154 headers = {'Content-type': 'application/json'}
155 requests.post('http://localhost:8080/phoneoffer/addoffer',
156 headers=headers, data=json.dumps(new_offer.__dict__, default=str))
157
158print('------------------------------------')
159
160for old_offer in database_offers:
161 flag = False
162 for new_offer in new_offers:
163 if old_offer.offer_shop_code == new_offer.offer_shop_code:
164 flag = True
165
166 if not flag:
167 print('OFFER DELETED')
168 print(old_offer)
169 # DELETE OFFER
170 requests.delete('http://localhost:8080/phoneoffer/deleteoffer/' + str(old_offer.offer_id))
Note: See TracBrowser for help on using the repository browser.