-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLstCacheImporter.py
More file actions
111 lines (90 loc) · 3.91 KB
/
LstCacheImporter.py
File metadata and controls
111 lines (90 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from DataStructures.Listings import Listing as Lst
from BBDatabase import ListingDatabase
from BBSearch import LstSearch
import json
import os
tempListings = []
tempPath = "TempCache.json"
def runScrapers():
"""A short script to run the webscrapers."""
# FINISHME:
print("Running Scrapers...")
try:
os.system("python3 Scraper/ApartmentListScraper.py")
except:
print("ApartmentListScraper Failed! Skipping...")
os.system("python3 Scraper/WebScraper.py")
print("Scrapers Finished!")
def readFromListingCache(path):
"""
Reads listings from a JSON file and appends them to the tempListings list. Returns the list of listings.
Args:
path (str): The path to the JSON file containing the listings data.
The function reads each listing from the specified JSON file, creates a Listing
object for each, and appends it to the tempListings list. It then prints out
each listing.
Raises:
FileNotFoundError: If the file at the specified path does not exist.
json.JSONDecodeError: If the file at the specified path is not a valid JSON file.
"""
try:
with open(path, "r") as f:
data = json.load(f)
for listing in data:
tempListings.append(Lst(
unitIndex = listing["unitIndex"],
name = listing["name"],
address = listing["address"],
numRooms = listing["numRooms"],
utilsIncluded = listing["utilsIncluded"],
rentAmt = listing["rentAmt"],
listingURL = listing["listingURL"],
hostSite = listing["hostSite"],
notes = listing["notes"],
favorited = listing["favorited"]
))
print(f" ID {'Listing Name':<50} {'Rent':<11} Rooms Utils Host Site Address URL Notes \n{'─'*198}")
for listing in tempListings:
print(listing)
except FileNotFoundError:
print(f"The file {path} does not exist.")
except json.JSONDecodeError:
print(f"The file {path} is not a valid JSON file.")
def writeListingsToDB():
"""
Compares listings in the temporary cache with those in the database and updates the database accordingly.
The function retrieves all listings from the database and compares them with those in the tempListings list.
If a listing from tempListings does not exist in the database (based on address comparison), it is added to the database.
If a listing already exists, it is updated in the database.
Note: The actual database operations (add/update) are currently commented out.
The function assumes that the database connection will be closed after operations are performed.
"""
db = ListingDatabase()
comparisonList = db.getAllListings()
# FIXME: This may need a better sort algorithm later down the line, depending on the size of the DB per user
for listing in tempListings:
match = False
for compListing in comparisonList:
if listing.address == compListing.address:
match = True
break
if not match:
db.addListing(listing)
print(f"Adding Listing {listing.unitIndex} | {listing.name} to DB")
print()
elif match:
print(f"Updating Listing {listing.unitIndex} | {listing.name} in DB")
db.updateListing(listing)
print()
db.close()
def main():
"""
Reads listings from a JSON file and writes them to a database.
This is the main script to write to the database."""
runScrapers()
readFromListingCache(tempPath)
writeListingsToDB()
print(f"[FINISHED] All Listings has been imported from {tempPath}!")
if __name__ == "__main__":
main()
input("Press ENTER to continue...")