2023-05-04 18:43:41 +00:00
|
|
|
#!.venv/bin/python
|
2022-07-11 16:36:52 +00:00
|
|
|
|
2023-05-04 18:43:41 +00:00
|
|
|
from db_con import DbConnect, sqlite3
|
2022-07-11 16:36:52 +00:00
|
|
|
from sel_source import SelRequest
|
|
|
|
import logging
|
|
|
|
import time
|
2023-05-04 18:43:41 +00:00
|
|
|
import json
|
|
|
|
from unidecode import unidecode
|
2022-07-11 16:36:52 +00:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2023-05-05 06:26:31 +00:00
|
|
|
logging.FileHandler('logfile.log')
|
2022-07-13 15:44:28 +00:00
|
|
|
logging.basicConfig(filename='logfile.log', filemode='w', level=logging.INFO,
|
2022-07-11 16:36:52 +00:00
|
|
|
format='%(asctime)s :: %(levelname)s :: %(name)s :: %(message)s')
|
|
|
|
|
2023-05-04 18:43:41 +00:00
|
|
|
try:
|
2023-05-05 06:26:31 +00:00
|
|
|
dbfile = open("olx_data.db", "r")
|
2023-05-04 18:43:41 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
logger.info("No database found, creating one now.")
|
|
|
|
with open('create_db.sql', 'r') as sql_file:
|
|
|
|
sql_script = sql_file.read()
|
|
|
|
db = sqlite3.connect("olx_data.db")
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.executescript(sql_script)
|
|
|
|
cursor.close()
|
|
|
|
db.close()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
con = DbConnect("olx_data.db")
|
2023-05-05 06:26:31 +00:00
|
|
|
con.get_connection()
|
2022-07-20 09:14:09 +00:00
|
|
|
drive = SelRequest()
|
2022-07-11 16:36:52 +00:00
|
|
|
|
2023-05-04 18:43:41 +00:00
|
|
|
cities = []
|
|
|
|
cities_file = open("miasta", "r")
|
|
|
|
for line in cities_file:
|
|
|
|
if line[0] != "#":
|
|
|
|
cities.append(json.loads(line))
|
2022-07-11 16:36:52 +00:00
|
|
|
|
2023-05-05 10:53:21 +00:00
|
|
|
dane_dzis = open("danedzis", "w")
|
|
|
|
|
2022-07-11 16:36:52 +00:00
|
|
|
url_base = 'https://www.olx.pl/d/nieruchomosci/mieszkania/'
|
2023-05-04 18:43:41 +00:00
|
|
|
cities_list = [(url_base + unidecode(i["name"]).lower().replace(" ", "-") + i["is_dubble"] + '/', i["city_id"]) for i in cities]
|
2022-07-20 09:14:09 +00:00
|
|
|
not_empty = len(cities_list)
|
|
|
|
|
2023-05-04 18:43:41 +00:00
|
|
|
for city in cities:
|
2022-07-20 09:14:09 +00:00
|
|
|
for i in cities_list:
|
|
|
|
start = time.time()
|
|
|
|
curTime = date.today()
|
2023-05-05 06:26:31 +00:00
|
|
|
# try:
|
|
|
|
for x in range(1):
|
2022-07-20 09:14:09 +00:00
|
|
|
source = drive.get_source(i[0])
|
|
|
|
values = drive.get_olx_stats(source)
|
|
|
|
values.append(i[1])
|
|
|
|
values.append(curTime)
|
|
|
|
query = """
|
|
|
|
INSERT INTO
|
|
|
|
olx_data(adv_rent_count, adv_sale_count, adv_exchange_count, city_id, date)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?, ?); """
|
|
|
|
con.insert_data(query, values)
|
2023-05-05 10:53:21 +00:00
|
|
|
dane_dzis.write("ostatnia aktualizacja: %s\nogloszen sprzedazy: %d\nogloszen wynajmu: %d\n" % (values[4], values[1], values[0]))
|
|
|
|
dane_dzis.close()
|
2023-05-05 06:26:31 +00:00
|
|
|
# except Exception as e:
|
|
|
|
# logger.info(f'an exception has occurred:{e}')
|
|
|
|
# pass
|
2022-07-20 09:14:09 +00:00
|
|
|
end = time.time()
|
|
|
|
logger.info(f'loop executed in {end-start}')
|
2022-07-11 16:36:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
|
|
|
|
# writer = csv.writer(out)
|
|
|
|
# for row in csv.reader(inp):
|
|
|
|
# if row[2] != "0":
|
|
|
|
# writer.writerow(row)
|
|
|
|
|
|
|
|
# add_record = """
|
|
|
|
# INSERT INTO
|
|
|
|
# olx_data(city_id, adv_sale_count, adv_rent_count, adv_exchange_count)
|
|
|
|
# VALUES
|
|
|
|
# (4548, 629, 837, 7);
|
|
|
|
# """
|
|
|
|
|
|
|
|
|
|
|
|
# cities_div_urls = {}
|
|
|
|
# url_base = 'https://www.olx.pl/d/nieruchomosci/mieszkania/'
|
|
|
|
#
|
|
|
|
# for el in regions:
|
|
|
|
# cities_list = [(url_base+removeaccents(i[1]).lower().replace(" ", "-")+'/', i[0]) for i in cities if i[2] == el[1]]
|
|
|
|
# cities_div_urls[el[0]] = cities_list
|
|
|
|
#
|
|
|
|
# reg_urls = [(url_base+removeaccents(el[0]).lower()+'/', el[1]) for el in regions]
|
|
|
|
|
|
|
|
# select c.city_id, o.city_id from cities c left join
|
|
|
|
# (select city_id from olx_data where date = date('now')) o on o.city_id=c.city_id;
|
|
|
|
|
|
|
|
# select name, city_id from cities where city_id not in
|
|
|
|
# (select city_id from olx_data where date=date('now'));
|
|
|
|
|
|
|
|
# select name from olx_data join cities on olx_data.city_id = cities.city_id where date = date('now');
|
2022-07-13 15:44:28 +00:00
|
|
|
|
|
|
|
# select c.city_id, c.name, c.is_dubble
|
|
|
|
# from custom c left join
|
|
|
|
# (select city_id from olx_data where date = date('now')) o
|
2022-07-19 08:16:05 +00:00
|
|
|
# on o.city_id=c.city_id where o.city_id is null;
|