-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_posts_csv.py
54 lines (42 loc) · 1.77 KB
/
get_posts_csv.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
__author__ = 'liorsabag'
from bson import json_util
import csv
from .constants import db
def find_image_url_by_id(item_id, search_results):
if isinstance(item_id, basestring) and item_id.isdigit():
item_id = int(item_id)
for search_result in search_results:
if search_result["id"] == item_id:
return search_result["imageURL"]
def main():
all_posts_cursor = db.posts.find()
flattened_posts = []
for post in all_posts_cursor:
post = json_util.loads(json_util.dumps(post))
r_dict = {}
for item in post["items"]:
if "topResults" in item:
r_dict["imageURL"] = post["imageURL"]
r_dict["boundingBox"] = item["boundingBox"]
r_dict["categoryId"] = item["categoryId"]
result_num = 1
for result in item["topResults"]:
r_dict["similar" + str(result_num)] = find_image_url_by_id(result, item["searchResults"])
result_num += 1
result_num = 1
for result in item["bottomResults"]:
r_dict["other" + str(result_num)] = find_image_url_by_id(result, item["searchResults"])
result_num += 1
flattened_posts.append(r_dict)
print "appended " + r_dict["imageURL"]
headers = ["imageURL", "boundingBox", "categoryId"]
similars = ["similar" + str(i) for i in range(1, 6)]
others = ["other" + str(i) for i in range(1, 201)]
headers = headers + similars + others
with open('flattened_posts.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, headers)
w.writeheader()
for post in flattened_posts:
w.writerow(post)
if __name__ == "__main__":
main()