Skip to content

Commit 026e5d1

Browse files
author
Daniel
committed
Many important changes: sms sender, project reconstruction, improve some things, query.py was deleted.
1 parent 880a591 commit 026e5d1

File tree

6 files changed

+150
-75
lines changed

6 files changed

+150
-75
lines changed

myproject/project_files/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from .model import *
21
from .functions import *
32
from .config import *
43
from .bot import *

myproject/project_files/bot.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from lib2to3.pgen2 import driver
21
from project_files import link as url
32
from selenium import webdriver
43
from selenium.webdriver.common.by import By
@@ -82,14 +81,14 @@ def years(self, min_year, max_year):
8281
maximum.send_keys(Keys.ENTER)
8382

8483
def mileage(self, min_mileage, max_mileage):
85-
self.implicitly_wait(2)
84+
self.implicitly_wait(5)
8685
minimum = self.find_element(By.XPATH,
8786
'//input[@id="filter_float_mileage:from"]'
8887
)
8988
minimum.send_keys(min_mileage)
9089
minimum.send_keys(Keys.ENTER)
9190

92-
self.implicitly_wait(2)
91+
self.implicitly_wait(5)
9392
maximum = self.find_element(By.XPATH,
9493
'//input[@id="filter_float_mileage:to"]'
9594
)
@@ -105,7 +104,7 @@ def search_all(self):
105104
button = self.find_element(By.XPATH,
106105
'//*[@data-testid="submit-btn"]'
107106
)
108-
self.implicitly_wait(10)
107+
self.implicitly_wait(20)
109108
button.click()
110109

111110

@@ -134,10 +133,10 @@ def info(self):
134133
)
135134
info_list = []
136135
for title, city, price, year, mileage, engine, petrol in zip(titles, cities, prices, years, mileages, engines, petrols):
137-
# print(title.text, city.text, price.text, year.text, mileage.text, engine.text, petrol.text)
138-
price = value(price=price.text, data_type='PLN')
136+
price = value(price=price.text, data_type='PLN')
139137
mileage = value(price=mileage.text, data_type='km')
140-
engine = value(price=engine.text, data_type='cm3')
138+
engine = value(price=engine.text, data_type='cm3')
139+
# print(title.text, city.text, price, year.text, mileage, engine, petrol.text)
141140
info_list.append([title.text, city.text, price, year.text, mileage, engine, petrol.text])
142141
return info_list
143142

myproject/project_files/functions.py

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import mysql.connector
22
from mysql.connector import errorcode
3+
from project_files.config import conn
4+
import urllib.request
5+
import urllib.parse
36

47
def value(price, data_type):
58
price = price.split(data_type)[0]
@@ -22,24 +25,43 @@ def connection(config):
2225
print(err)
2326

2427

25-
def create_database(cursor, DB_NAME):
28+
def create_database( DB_NAME):
2629
try:
30+
connect = mysql.connector.connect(**conn)
31+
cursor = connect.cursor()
2732
cursor.execute(
2833
"CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
2934
except mysql.connector.Error as err:
30-
print("Failed creating database: {}".format(err))
35+
print("Failed creating database: {} ".format(err))
3136

3237

3338
def create_table(cursor, TABLES):
3439
for table_name in TABLES:
40+
# print('table name::: ',table_name)
3541
table_description = TABLES[table_name]
36-
try:
37-
print("Creating table {}: ".format(table_name), end='')
38-
cursor.execute(table_description)
39-
except mysql.connector.Error as err:
40-
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
41-
print("already exists.")
42-
else:
43-
print(err.msg)
42+
try:
43+
print("Creating table {}: ".format(table_name), end='')
44+
cursor.execute(table_description)
45+
except mysql.connector.Error as err:
46+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
47+
print("already exists.")
48+
else:
49+
print(err.msg)
4450
else:
45-
print("OK")
51+
print("OK")
52+
53+
54+
def data_send(connect, cursor, sql, data):
55+
cursor.execute( sql, data)
56+
connect.commit()
57+
58+
def sendSMS(apikey, *numbers, author, message):
59+
data = urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers,
60+
'message' : message, 'author': author})
61+
data = data.encode('utf-8')
62+
request = urllib.request.Request("https://api.txtlocal.com/send/?")
63+
f = urllib.request.urlopen(request, data)
64+
fr = f.read()
65+
return(fr)
66+
67+

myproject/project_files/model.py

-23
This file was deleted.

myproject/project_files/query.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,38 @@
33
TABLES = {}
44

55
TABLES['car'] = (
6-
"CREATE TABLE `car` ("
6+
"CREATE TABLE `car`("
77
" `ID` int(11) NOT NULL AUTO_INCREMENT,"
8-
" `title` varchar(50) NOT NULL,"
9-
" `city` varchar(20) NOT NULL,"
8+
" `title` varchar(100) NOT NULL,"
9+
" `city` varchar(50) NOT NULL,"
1010
" `price` int(10) NOT NULL,"
11-
" `year` int(10) NOT NULL,"
11+
" `year` char(10) NOT NULL,"
1212
" `mileage_km` int(10) NOT NULL,"
1313
" `engine_cm3` int(10) NOT NULL,"
1414
" `petrol` char(15) NOT NULL,"
1515
" PRIMARY KEY (`ID`)"
1616
") ENGINE=InnoDB")
1717

18+
19+
TABLES['selected_car'] = (
20+
"CREATE TABLE `selected_car`("
21+
" `ID` int(11) NOT NULL AUTO_INCREMENT,"
22+
" `title` varchar(100) NOT NULL,"
23+
" `city` varchar(50) NOT NULL,"
24+
" `price` int(10) NOT NULL,"
25+
" `year` char(10) NOT NULL,"
26+
" `mileage_km` int(10) NOT NULL,"
27+
" `engine_cm3` int(10) NOT NULL,"
28+
" `petrol` char(15) NOT NULL,"
29+
" PRIMARY KEY (`ID`)"
30+
") ENGINE=InnoDB")
31+
32+
33+
34+
add_new_row = ("INSERT INTO car"
35+
"(title, city, price, year, mileage_km, engine_cm3, petrol)"
36+
"VALUES (%s, %s, %s, %s, %s, %s, %s)")
37+
38+
add_specific_row = ("INSERT INTO selected_car"
39+
"(title, city, price, year, mileage_km, engine_cm3, petrol)"
40+
"VALUES (%s, %s, %s, %s, %s, %s, %s)")

myproject/run.py

+84-29
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,92 @@
11
from project_files import Cars
2-
from project_files import Operations
32
from project_files import config
4-
from project_files import connection, create_database, create_table
5-
from project_files.query import DB_NAME, TABLES
3+
from project_files import connection, create_database, create_table, data_send, sendSMS
4+
from project_files.query import DB_NAME, TABLES, add_new_row, add_specific_row
5+
import os
66

7+
car_type = input('Enter car type, for example: Auta małe \n')
8+
brand = input('Enter brand \n')
9+
min_price = input('Enter minimal price \n')
10+
max_price = input('Enter maximum price \n')
11+
min_year = input('Enter minimum year \n')
12+
max_year = input('Enter maximum year \n')
13+
min_mileage = input('Enter minimum mileage \n')
14+
max_mileage = input('Enter maximum mileage \n')
15+
max_page = input('how many pages should be searched? \n')
16+
17+
create_database(DB_NAME=DB_NAME)
718
connect = connection(config=config)
819
cursor = connect.cursor()
9-
create_database(cursor=cursor, DB_NAME=DB_NAME)
1020
create_table(cursor=cursor, TABLES=TABLES)
21+
22+
try:
23+
with Cars() as bot:
24+
correct_list = []
25+
bot.land_first_page()
26+
bot.accept_button()
27+
bot.car_type(car=car_type)
28+
bot.car_brand(brand=brand)
29+
bot.price(min_price=min_price, max_price=max_price)
30+
bot.years(min_year=min_year, max_year=max_year)
31+
bot.mileage(min_mileage=min_mileage, max_mileage=max_mileage)
32+
bot.search_all()
33+
for page in range(1,max_page):
34+
data = bot.info()
35+
for row in data:
36+
correct_list.append( row)
37+
if int(page) > 1:
38+
bot.page_change(page=page)
39+
except:
40+
print('There is a problem with a main bot')
41+
42+
43+
for row in correct_list:
44+
data_send(connect=connect, cursor=cursor, sql=add_new_row, data=row)
45+
46+
try:
47+
try:
48+
cursor.execute("SELECT title, city, price, year, mileage_km, engine_cm3, petrol from cars.car order by year asc limit 1")
49+
record = cursor.fetchall()
50+
first_ask = ''
51+
for row in record:
52+
first_ask = row
53+
except:
54+
print('Error with first table')
55+
try:
56+
cursor.execute("SELECT title, city, price, year, mileage_km, engine_cm3, petrol from cars.selected_car order by year asc limit 1")
57+
second_record = cursor.fetchall()
58+
second_ask = ''
59+
for row in second_record:
60+
if row != '' or row != None:
61+
second_ask = row
62+
except:
63+
print('error with second table')
64+
try:
65+
if first_ask != second_ask:
66+
data_send(connect=connect, cursor=cursor, sql=add_specific_row, data=first_ask)
67+
except:
68+
print('Error with adding row to: cars.selected_car')
69+
try:
70+
cursor.execute("SELECT ID, title, city, price, year, mileage_km, engine_cm3, petrol from cars.selected_car order by year asc limit 1")
71+
second_record = cursor.fetchall()
72+
for data in second_record:
73+
message = 'title: {} \ncity: {} \nprice: {} \nyear: {} \nmileage: {} km2 \nengine: {} cm3 \npetrol: {}'.format(data[1],data[2],data[3],data[4],data[5],data[6],data[7])
74+
print(message)
75+
# sendSMS(apikey='****API_KEY****', numbers='', author='', message=message)
76+
except:
77+
'Error with sms api'
78+
except:
79+
print('Error with second part')
80+
81+
delete_database = input('Do you want delete database? Write Yes or Not \n')
82+
if delete_database == 'yes' or delete_database == 'Yes' or delete_database == 'YES':
83+
try:
84+
cursor.execute('DROP DATABASE cars')
85+
print('DATABASE WAS DELETED')
86+
except:
87+
print('Error with delete database')
88+
1189
cursor.close()
1290
connect.close()
13-
# cursor.execute("SHOW TABLES")
14-
15-
# for x in cursor:
16-
# print(x)
17-
18-
# try:
19-
# with Cars() as bot:
20-
# bot.land_first_page()
21-
# bot.accept_button()
22-
# bot.car_type(car='Auta małe')
23-
# bot.car_brand(brand='Renault')
24-
# bot.price(min_price='0', max_price='15000')
25-
# bot.years(min_year='2000', max_year='2010')
26-
# bot.mileage(min_mileage='0', max_mileage='2000000')
27-
# bot.search_all()
28-
# for page in range(1,5):
29-
# data = bot.info()
30-
31-
# for row in data:
32-
# print(row)
33-
34-
# if int(page) > 1:
35-
# bot.page_change(page=page)
36-
# except:
37-
# 'There is a problem with a main bot'
91+
92+

0 commit comments

Comments
 (0)