Repository navigation
Expand file tree
/
Copy pathdata_manager.py
More file actions
50 lines (46 loc) · 2.15 KB
/
Copy pathdata_manager.py
File metadata and controls
50 lines (46 loc) · 2.15 KB
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
import requests
import os
from dotenv import load_dotenv
load_dotenv()
SHEETY_PRICES_ENDPOINT = os.getenv("BASE_URL_SHEETY") + "/" + os.getenv("USERNAME_SHEETY") + "/" + os.getenv("PROJECT_NAME_SHEETY") + "/" + os.getenv("FIRST_SHEET_NAME_SHEETY") # get the url of the sheet
SHEETY_USERS_ENDPOINT = os.getenv("BASE_URL_SHEETY") + "/" + os.getenv("USERNAME_SHEETY") + "/" + os.getenv("PROJECT_NAME_SHEETY") + "/" + os.getenv("SECOND_SHEET_NAME_SHEETY") # get the url of the sheet
class DataManager:
def __init__(self):
"""this class is responsible for getting the data from the sheet"""
self._user = os.getenv("USERNAME_SHEETY") # get the username
self._password = os.getenv("PASSWORD_SHEETY") # get the password
self._authorization = {"Authorization": f"Bearer {os.getenv("BOT_TOKEN")}"} # get the token
self.destination_data = {} # create an empty dictionary
self.customer_data = {}
def get_destination_data(self):
"""
get all the data in that sheet
:return: all the data in that sheet
"""
response = requests.get(url=SHEETY_PRICES_ENDPOINT, headers=self._authorization) # get the data from the sheet
data = response.json() # get the data in JSON format
self.destination_data = data["prices"] # save the data in a dictionary
return self.destination_data
def update_lowest_price(self, row_id, new_price):
"""
Update the lowest price
:param row_id: Row ID
:param new_price: New price
:return: status code
"""
new_data = {
"price": {
"lowestPrice": new_price
}
}
response = requests.put(url=f"{SHEETY_PRICES_ENDPOINT}/{row_id}", headers=self._authorization, json=new_data)
return response.status_code
def get_customer_emails(self):
"""
Get the customer emails
:return: customer emails
"""
response = requests.get(url=f"{SHEETY_USERS_ENDPOINT}", headers=self._authorization) # get the data from the sheet
data = response.json()
self.customer_data = data["users"]
return self.customer_data