File size: 2,300 Bytes
b438494
 
 
 
 
 
 
6df648f
 
 
923ad12
 
 
 
 
 
 
 
 
 
 
 
 
e254dbd
923ad12
 
 
 
 
e24d810
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
923ad12
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
from pymongo import MongoClient
import os
def data_field(data_src):
        mongodb_uri = os.getenv("MONGODB_URI")
        myclient = MongoClient(mongodb_uri)
        mydb = myclient.get_database()
        mycol = mydb["df_data"]
        x = mycol.find_one({"data_field": data_src},
                sort=[('timestamp', -1)])
        x = x["result"]
        return x

def get_analyst_response(data_src):
        try:
                mongodb_uri = os.getenv("MONGODB_URI")
                myclient = MongoClient(mongodb_uri)
                mydb = myclient.get_database()
                mycol = mydb["df_response"]
                x = mycol.find_one({"data_field": data_src},
                sort=[('timestamp', -1)])
                if x and "result" in x:
                        return x["result"]
                else:
                        print(f"No matching document or 'result' field found for data_src: {data_src} in df_response. 404")
                        return None # Return None if no doc or 'result' field found
        finally:
                if myclient:
                        myclient.close()


def get_marketplace_response(data_src):
    try:
        mongodb_uri = os.getenv("MONGODB_URI")
        myclient = MongoClient(mongodb_uri)
        mydb = myclient.get_database()
        mycol = mydb["df_response"]
        
        # Find the most recent document matching the data_src
        document = mycol.find_one(
            {"data_field": data_src},
            sort=[('timestamp', -1)]
        )
        
        # Check if document exists and has the result field
        if document and "result" in document:
            # Extract amazon and ebay data separately
            amazon_data = document["result"].get("amazon", [])
            ebay_data = document["result"].get("ebay", [])
            
            # Combine both datasets into a single list
            combined_data = amazon_data + ebay_data
            
            return combined_data
        else:
            print(f"No matching document or 'result' field found for data_src: {data_src} in df_response. 404")
            return None
    except Exception as e:
        print(f"Error retrieving data: {str(e)}")
        return None
    finally:
        if myclient:
            myclient.close()