-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
134 lines (97 loc) · 4.42 KB
/
App.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Standard library imports
import logging
import re
import functools
import time
import requests
from time import gmtime, strftime
import datetime
import sys
import atexit
""" Function wrappers for main() """
# Continue running if there is an exception in the main method
def main():
"""
Extract, Transform, Load
Access each RentFaster.ca API page individually and store the results in MySQL.
Built in data cleaning, duplicate entry checking, database error handling, and scheduling.
"""
i = 0 # Loop counter
keys = [] # Primary Key List
j = 0
while True:
page = str(i)
url = 'https://www.rentfaster.ca/api/search.json?keywords=&proximity_type=location-proximity&cur_page=' + \
page + \
'&beds=&type=&price_range_adv[from]=null&price_range_adv[to]=null&novacancy=0&city_id=2'
data = requests.get(url).json()
# There are multiple keys that can be accessed at this level, we want the listings data
listings = data['listings']
# We have reached the last page
if len(listings) == 0:
break
# We have not reached the last page
i += 1
#print("Page " + page + " data obtained")
for listing in listings:
j += 1
# Save Primary Key
keys.append(listing['ref_id'])
# # Add retrieval date
# listing['retrieval_date'] = pd.to_datetime(
# 'today').strftime("%m/%d/%Y")
# Add position
listing['position'] = "active"
# --- Data Cleaning ---
if 'sq_feet' in listing:
listing['sq_feet'] = re.sub(
"[^0-9]", "", listing['sq_feet'])
else:
listing['sq_feet'] = ""
if 'bedrooms' in listing:
listing['bedrooms'] = listing['bedrooms'].replace(
'bachelor', "0")
else:
listing['bedrooms'] = "0"
# Convert the list to a CSV string
if 'utilities_included' in listing:
if 'utilities_included' != False:
continue
listing['utilities_included'] = ",".join(
[str(x) for x in listing['utilities_included']])
# Remove whitespace
listing['utilities_included'] = listing['utilities_included'].strip(
)
# Remove trailing comma
listing['utilities_included'] = listing['utilities_included'].rstrip(
',')
# else:
# listing['utilities_included'] = ""
if 'den' not in listing:
listing['den'] = " "
if 'baths' not in listing:
listing['baths'] = " "
if 'cats' not in listing:
listing['cats'] = " "
if 'dogs' not in listing:
listing['dogs'] = " "
# --- End Data Cleaning ---
# db = Database.Database.db_config()[3]
# Instantiate Object
# rental = Rental.Rental(listing["ref_id"], listing["userId"], listing["id"], listing["title"], listing["price"], listing["type"],
# listing["sq_feet"], listing["availability"], listing["avdate"], listing[
# "location"], listing["rented"], listing["thumb"], listing["thumb2"],
# listing["slide"], listing["link"], listing["latitude"], listing[
# "longitude"], listing["marker"], listing["address"], listing["address_hidden"],
# listing["city"], listing["province"], listing["intro"], listing[
# "community"], listing["quadrant"], listing["phone"], listing["phone_2"],
# listing["preferred_contact"], listing["website"], listing["email"], listing[
# "status"], listing["bedrooms"], listing["den"], listing["baths"],
# listing["cats"], listing["dogs"], listing['utilities_included'], listing['position'], listing["retrieval_date"])
f = open("file.txt", "a",encoding="utf-8")
print(listings, file=f)
f.close()
print(j)
# Enter the runtime in sys.argv[1]
if __name__ == "__main__":
main()