def mongoOperation(i, data): if i == 'amazon': try: m1 = MongoDB('amazon') m1.insert(data) except Exception as e: print('Exception in MongoDB Amazon\n' + str(e)) elif i == 'flipkart': try: m2 = MongoDB('flipkart') m2.insert(data) except Exception as e: print('Exception in MongoDB Flipkart\n' + str(e)) elif i == 'paytm': try: m3 = MongoDB('paytm') m3.insert(data) except Exception as e: print('Exception in MongoDB Paytm\n' + str(e))
if not data_in_db: START_URL = "https://gateway.marvel.com/v1/public/characters" PRIVATE_KEY = "0f56ea9eaad631cee9aed42478145bac470364fb" PUBLIC_KEY = "f455a0f7b332cf2db81353408eabae56" timestamp = str(datetime.datetime.now().timestamp()) hash_ = timestamp + PRIVATE_KEY + PUBLIC_KEY params = { "ts": timestamp, "apikey": PUBLIC_KEY, "hash": md5(hash_.encode()).hexdigest() } print("Making request to Marvel.....") request = requests.get(START_URL, params) if request.status_code == 200: response = request.json() print("Inserting results.....") for value in response["data"]["results"]: mongo_db.insert(value) print("Inserted " + str(len(response["data"]["results"])) + " results.") else: print("Something went wrong with Marvel API.....") else: print("DB already populated.") except Exception as err: print("Something went wrong with the importing process.") print(err)