olx-visualiser/plot.py

81 lines
2.4 KiB
Python
Executable File

#!.venv/bin/python
from db_con import DbConnect
from matplotlib import pyplot as plt
from matplotlib import ticker
from datetime import date, timedelta
import csv
con = DbConnect("olx_data.db")
con.get_connection()
query = """
select * from olx_data;
"""
data = con.get_data(query)
dates = []
rent = []
sell = []
#exchange = []
for i in data:
sell.append(i[3])
rent.append(i[4])
# exchange.append(i[5])
dates.append(date.fromisoformat(i[6]))
firstday = dates[0]
dates = [firstday-timedelta(days=x) for x in range(91 - len(dates), 0, -1)] + dates
sell = [0] * (90-len(sell)) + sell
rent = [0] * (90-len(rent)) + rent
#exchange = [0] * (90-len(exchange)) + exchange
plt.plot(dates[-7:], rent[-7:], label="wynajem")
plt.plot(dates[-7:], sell[-7:], label="sprzedaż")
plt.xlabel("data")
plt.xticks(rotation=45)
plt.ylim(sorted(set(sell+rent))[1]-200, max(sell+rent)+200)
plt.ylabel("ilość ogłoszeń")
plt.title("ogłoszenia o mieszkaniach na olx.pl (ostatnie 7 dni)")
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(200))
plt.legend()
plt.grid()
plt.savefig("week.jpg", bbox_inches="tight", dpi=150)
plt.cla()
plt.plot(dates[-30:], rent[-30:], label="wynajem")
plt.plot(dates[-30:], sell[-30:], label="sprzedaż")
#plt.plot(dates[-30:], exchange[-30:], label="zamiana")
plt.xlabel("data")
plt.xticks(rotation=45)
plt.ylabel("ilość ogłoszeń")
plt.title("ogłoszenia o mieszkaniach na olx.pl (ostatnie 30 dni)")
plt.ylim(sorted(set(sell+rent))[1]-200, max(sell+rent)+200)
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(200))
plt.legend()
plt.grid()
plt.savefig("month.jpg", bbox_inches="tight", dpi=150)
plt.cla()
plt.plot(dates[-90:], rent[-90:], label="wynajem")
plt.plot(dates[-90:], sell[-90:], label="sprzedaż")
#plt.plot(dates[-90:], exchange[-90:], label="zamiana")
plt.xlabel("data")
plt.xticks(rotation=45)
plt.ylabel("ilość ogłoszeń")
plt.title("ogłoszenia o mieszkaniach na olx.pl (ostatnie 90 dni)")
plt.ylim(sorted(set(sell+rent))[1]-200, max(sell+rent)+200)
plt.gca().yaxis.set_major_locator(plt.MultipleLocator(200))
plt.legend()
plt.grid()
plt.savefig("threemonths.jpg", bbox_inches="tight", dpi=150)
cursor=con.connection.cursor()
cursor.execute("SELECT * FROM olx_data;")
with open("data.csv", "w") as datacsv:
csv_out = csv.writer(datacsv)
csv_out.writerow([d[0] for d in cursor.description])
for result in cursor:
csv_out.writerow(result)
cursor.close()