forked from tomslee/airbnb-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airbnb_s3_upload.py
236 lines (215 loc) · 8.91 KB
/
airbnb_s3_upload.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python3
# ============================================================================
# Manage files in S3
# ============================================================================
import boto3
import os
import pandas as pd
import logging
import zipfile
from airbnb_config import ABConfig
AWS_S3_BUCKET = "tomslee-airbnb-data-2"
START_DATE = '2013-05-02'
# Set up logging
LOG_FORMAT = '%(levelname)-8s%(message)s'
LOG_LEVEL = logging.INFO
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
def surveys(ab_config):
sql_survey_ids = """
select survey_id,
sa.name city,
sa.abbreviation city_abbrev,
survey_date,
comment
from survey s join search_area sa
on s.search_area_id = sa.search_area_id
where sa.abbreviation is not null
and sa.bb_n_lat is not null
and s.status = 1
and 'listing_' || sa.abbreviation in
( select table_name
FROM information_schema.views
where table_schema = 'public'
)
order by 2, 1
"""
conn = ab_config.connect()
cur = conn.cursor()
cur.execute(sql_survey_ids)
rs = cur.fetchall()
logging.info("Found {surveys} surveys.".format(surveys=len(rs)))
cur.close()
conn.close()
return(rs)
def get_city_view(ab_config, city, abbrev):
try:
view_name = "listing_" + abbrev
sql = "select viewname from pg_views where viewname = '{}'".format(view_name)
conn = ab_config.connect()
cur = conn.cursor()
cur.execute(sql)
view = cur.fetchone()[0]
cur.close()
logging.debug("Found view for city {0}: {1}".format(city, view_name))
return view
except:
logging.debug("No view for city {0}".format(city))
return None
def city_data(ab_config, city, city_view, survey_id):
# sql = """
# select room_id, host_id, room_type,
# borough, neighborhood,
# reviews, overall_satisfaction,
# accommodates, bedrooms,
# price, minstay,
# latitude, longitude,
# last_modified
# from {city_view}
# where survey_id = %(survey_id)s
# """.format(city_view=city_view)
sql = """
select *
from {city_view}
where survey_id = %(survey_id)s
""".format(city_view=city_view)
conn = ab_config.connect()
df = pd.read_sql(sql, conn,
index_col="room_id",
params={"survey_id": survey_id}
)
return df
def cities(ab_config, survey_list):
city_views = {}
logging.info("-" * 70)
logging.info("Querying database for cities...")
for survey in survey_list:
(survey_id, city, city_abbrev, survey_date, comment) = survey
if city in city_views:
logging.debug("View for {0} is known: {1}".format(city, city_views[city]))
else:
city_view = get_city_view(ab_config, city, city_abbrev)
if city_view:
city_views[city] = city_view
logging.debug("Found view for {0}: {1}".format(city, city_views[city]))
else:
continue
return city_views
def write_csv_files(ab_config, survey_list, city_views, s3_dir):
survey_counts = {}
logging.info("-" * 70)
logging.info("Querying database and writing csv files...")
for survey in survey_list:
(survey_id, city, city_abbrev, survey_date, comment) = survey
if city not in city_views:
continue
city_view = city_views[city]
city_bar = city.replace(" ", "_").lower()
path = os.path.join(s3_dir, city_bar)
csv_file = ("tomslee_airbnb_{city}_{survey_id:0>4}_{survey_date}.csv"
).format(city=city_bar, survey_id=survey_id, survey_date=survey_date)
csv_full_file_path = os.path.join(path, csv_file)
# Only get the data if we don't already have it
if os.path.isfile(csv_full_file_path):
logging.info("File already exists: {csv_file}. Skipping...".format(csv_file=csv_full_file_path))
df_data = pd.read_csv(csv_full_file_path, index_col="room_id", encoding="utf-8")
survey_counts[survey_id] = len(df_data)
else:
df_data = city_data(ab_config, city, city_view, survey_id)
if len(df_data) > 0:
survey_counts[survey_id] = len(df_data)
if not os.path.exists(path):
os.makedirs(path)
logging.info("Writing {listings:>6} listings to {csv_file}..."
.format(listings=survey_counts[survey_id], csv_file=csv_full_file_path))
df_data.to_csv(csv_full_file_path, encoding="utf-8")
return survey_counts
def write_html_file(survey_list, city_views, survey_counts):
"""
The HTML file contains a block of HTML that has descriptions of and a link
to each zip file. I manually paste the file into the website for users.
"""
logging.info("-" * 70)
logging.info("Writing HTML list of links...")
html = "<dl>\n"
for city in sorted(city_views):
city_bar = city.replace(" ", "_").lower()
s = "<dt>{city}</dt>\n".format(city=city)
s += "<dd><p>Survey dates: "
for survey in survey_list:
(survey_id, survey_city, city_abbrev, survey_date, comment) = survey
survey_date = survey[3]
if (survey_city == city) and (survey_id in survey_counts):
s += "{survey_date} ({survey_count} listings), ".format(
survey_date=survey_date, survey_count=survey_counts[survey_id])
s = s[:-2]
s += "</p>\n"
s += "<p><a href=\"https://s3.amazonaws.com/{bucket}/{city_bar}.zip\">Download zip file</a></p>\n".format(
city_bar=city_bar, bucket=AWS_S3_BUCKET)
s += "<p><a href=\"https://s3.amazonaws.com/{bucket}/{city_bar}.pdf\">Download pdf file</a></p>\n".format(
city_bar=city_bar, bucket=AWS_S3_BUCKET)
s += "</dd>\n"
html += s
logging.info("City {0} linked.".format(city))
html += "</dl>"
f1 = open('city_list.html', 'w')
f1.write(html)
f1.close()
def zip_csv_files(city_views, s3_dir):
logging.info("-" * 70)
logging.info("Zipping data files...")
for city in city_views:
try:
city_bar = city.replace(" ", "_").lower()
csv_path = os.path.join(s3_dir, city_bar)
zip_file = os.path.join(s3_dir, city_bar + ".zip")
csv_files = [f for f in os.listdir(csv_path) if os.path.isfile(os.path.join(csv_path, f))]
with zipfile.ZipFile(zip_file, 'w') as city_zip_file:
for csv_file in csv_files:
city_zip_file.write(os.path.join(csv_path, csv_file))
logging.info("\tCity {0} zipped.".format(city_bar))
except:
continue
def upload_files(city_views, survey_list, s3_dir):
logging.info("-" * 70)
logging.info("Uploading zip files...")
s3 = boto3.resource('s3')
logging.info("Connected to S3...")
report_dir = "reports"
map_dir = "../airbnb-ipython-notebooks/notebooks/maps"
for city in city_views:
city_bar = city.replace(" ", "_").lower()
zip_file = os.path.join(s3_dir, city_bar + ".zip")
if os.path.isfile(zip_file):
key = city_bar + ".zip"
s3.Object(AWS_S3_BUCKET, key).put(Body=open(zip_file, 'rb'))
s3.Object(AWS_S3_BUCKET, key).Acl().put(ACL='public-read')
logging.info("\tUploaded {0}.".format(zip_file))
pdf_file = os.path.join(report_dir, "Airbnb City Notebook " + city + ".pdf")
if os.path.isfile(pdf_file):
key = city_bar + ".pdf"
s3.Object(AWS_S3_BUCKET, key).put(Body=open(pdf_file, 'rb'))
s3.Object(AWS_S3_BUCKET, key).Acl().put(ACL='public-read')
logging.info("\tUploaded {0}.".format(pdf_file))
for survey in survey_list:
(survey_id, city_name, city_abbrev, survey_date, comment) = survey
if city_name == city:
html_file = os.path.join(map_dir,
"{city}_{survey_id}.html".format(city=city, survey_id=survey_id))
logging.info("Inspecting {0}".format(html_file))
if os.path.isfile(html_file):
key = city_bar + ".html"
s3.Object(AWS_S3_BUCKET, key).put(Body=open(html_file, 'rb'))
s3.Object(AWS_S3_BUCKET, key).Acl().put(ACL='public-read')
logging.info("\tUploaded {0}.".format(html_file))
def main():
ab_config = ABConfig()
survey_list = surveys(ab_config)
city_views = cities(ab_config, survey_list)
logging.debug(city_views)
s3_dir = "s3_files"
survey_counts = write_csv_files(ab_config, survey_list, city_views, s3_dir)
zip_csv_files(city_views, s3_dir)
upload_files(city_views, survey_list, s3_dir)
write_html_file(survey_list, city_views, survey_counts)
if __name__ == "__main__":
main()