-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_insert.py
54 lines (40 loc) · 1.5 KB
/
db_insert.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
import pandas as pd
import requests
import pymongo
def request_marketplace_names(marketplace_ids):
marketplaces = {}
marketplace_names = []
for _, mid in marketplace_ids.items():
# for performance, try to skip doing the same http request
if mid in marketplaces:
marketplace_names.append(marketplaces[mid])
else:
url = f"https://stores.robinrover.io/api/{mid}"
response_json = requests.request("GET", url).json()
marketplaces[mid] = response_json["name"]
marketplace_names.append(response_json["name"])
return pd.Series(marketplace_names)
def main():
# Read csv
df_products = pd.read_csv("productAttributes.csv", sep=';')
# Retrieve marketplace names and assign in df
df_products["marketplaceName"] = request_marketplace_names(
df_products["marketplace"])
# Rename columns
columnRenames = {
"category.name": "categoryName",
"category.code": "categoryCode",
"brand.name": "brandName",
"marketplace": "marketplaceId",
}
df_products.rename(columns=columnRenames, inplace=True)
# Convert into dict to insert into mongodb
dict_products = df_products.to_dict("records")
# MongoDB insertion
# Create client with default credentials
myclient = pymongo.MongoClient("mongodb://localhost:27017")
db = myclient["robin"]
collection = db["testPythonDB"]
collection.insert_many(dict_products)
if __name__ == "__main__":
main()