Skip to content

Commit

Permalink
Fixed server-user timezone conflict issue (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
arpanghosh8453 committed Feb 27, 2024
1 parent 255a78c commit b1c2434
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 106 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /app
COPY ./Fitbit_Fetch.py /app
COPY ./requirements.txt /app

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
Expand Down
25 changes: 12 additions & 13 deletions Fitbit_Fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
ACCESS_TOKEN = "" # Empty Global variable initialization, will be replaced with a functional access code later using the refresh code
AUTO_DATE_RANGE = True # Automatically selects date range from todays date and update_date_range variable
auto_update_date_range = 1 # Days to go back from today for AUTO_DATE_RANGE *** DO NOT go above 2 - otherwise may break rate limit ***
LOCAL_TIMEZONE = "Automatic" # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).
LOCAL_TIMEZONE = os.environ.get("LOCAL_TIMEZONE") or "Automatic" # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).
SCHEDULE_AUTO_UPDATE = True if AUTO_DATE_RANGE else False # Scheduling updates of data when script runs
SERVER_ERROR_MAX_RETRY = 3
EXPIRED_TOKEN_MAX_RETRY = 5
Expand Down Expand Up @@ -202,14 +202,22 @@ def write_points_to_influxdb(points):
else:
logging.error("No matching version found. Supported values are 1 and 2")
raise InfluxDBClientError("No matching version found. Supported values are 1 and 2:" + str(err))


# %% [markdown]
# ## Set Timezone from profile data

# %%
if LOCAL_TIMEZONE == "Automatic":
LOCAL_TIMEZONE = pytz.timezone(request_data_from_fitbit("https://api.fitbit.com/1/user/-/profile.json")["user"]["timezone"])
else:
LOCAL_TIMEZONE = pytz.timezone(LOCAL_TIMEZONE)

# %% [markdown]
# ## Selecting Dates for update

# %%
if AUTO_DATE_RANGE:
end_date = datetime.now()
end_date = datetime.now(LOCAL_TIMEZONE)
start_date = end_date - timedelta(days=auto_update_date_range)
end_date_str = end_date.strftime("%Y-%m-%d")
start_date_str = start_date.strftime("%Y-%m-%d")
Expand All @@ -227,7 +235,7 @@ def write_points_to_influxdb(points):

def update_working_dates():
global end_date, start_date, end_date_str, start_date_str
end_date = datetime.now()
end_date = datetime.now(LOCAL_TIMEZONE)
start_date = end_date - timedelta(days=auto_update_date_range)
end_date_str = end_date.strftime("%Y-%m-%d")
start_date_str = start_date.strftime("%Y-%m-%d")
Expand Down Expand Up @@ -558,15 +566,6 @@ def fetch_latest_activities(end_date_str):
logging.error("Fetching 50 recent activities failed : before date " + end_date_str)


# %% [markdown]
# ## Set Timezone from profile data

# %%
if LOCAL_TIMEZONE == "Automatic":
LOCAL_TIMEZONE = pytz.timezone(request_data_from_fitbit("https://api.fitbit.com/1/user/-/profile.json")["user"]["timezone"])
else:
LOCAL_TIMEZONE = pytz.timezone(LOCAL_TIMEZONE)

# %% [markdown]
# ## Call the functions one time as a startup update OR do switch to bulk update mode

Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ services:
- ./logs:/app/logs
- ./tokens:/app/tokens
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
environment:
- FITBIT_LOG_FILE_PATH=/app/logs/fitbit.log
- TOKEN_FILE_PATH=/app/tokens/fitbit.token
Expand All @@ -73,15 +72,9 @@ services:
- INFLUXDB_DATABASE=fitbit_database # for influxdb 1.x
- CLIENT_ID=your_application_client_ID # Change this to your client ID
- CLIENT_SECRET=your_application_client_secret # Change this to your client Secret
- DEVICENAME='Your_Device_Name' # Change this to your device name - e.g. "Charge5"
# Dont change the following if you are not sure about the variable.
- AUTO_DATE_RANGE=True # Automatically selects date range from todays date and update_date_range variable
- auto_update_date_range=1 # Days to go back from today for AUTO_DATE_RANGE *** DO NOT go above 2 - otherwise may break rate limit ***
- LOCAL_TIMEZONE="Automatic" # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).
- SCHEDULE_AUTO_UPDATE=True # if AUTO_DATE_RANGE else False # Scheduling updates of data when script runs
- SERVER_ERROR_MAX_RETRY=3
- EXPIRED_TOKEN_MAX_RETRY=5
- SKIP_REQUEST_ON_SERVER_ERROR=True
- DEVICENAME=Your_Device_Name # Change this to your device name - e.g. "Charge5"
- LOCAL_TIMEZONE=Automatic # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).
influxdb:
restart: unless-stopped
Expand Down
60 changes: 0 additions & 60 deletions docker-compose-local.yml

This file was deleted.

11 changes: 0 additions & 11 deletions docker-compose.debug.yml

This file was deleted.

14 changes: 3 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services:
- ./logs:/app/logs
- ./tokens:/app/tokens
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
environment:
- FITBIT_LOG_FILE_PATH=/app/logs/fitbit.log
- TOKEN_FILE_PATH=/app/tokens/fitbit.token
Expand All @@ -33,16 +32,9 @@ services:
# MAKE SURE you set the application type to PERSONAL. Otherwise, you won't have access to intraday data series, resulting in 40X errors.
- CLIENT_ID=your_application_client_ID # Change this to your client ID
- CLIENT_SECRET=your_application_client_secret # Change this to your client Secret
- DEVICENAME='Your_Device_Name' # e.g. "Charge5"
# ACCESS_TOKEN='' # Empty Global variable initialization, will be replaced with a functional access code later using the refresh code
- AUTO_DATE_RANGE=True # Automatically selects date range from todays date and update_date_range variable
- auto_update_date_range=1 # Days to go back from today for AUTO_DATE_RANGE *** DO NOT go above 2 - otherwise may break rate limit ***
- LOCAL_TIMEZONE="Automatic" # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).
- SCHEDULE_AUTO_UPDATE=True # if AUTO_DATE_RANGE else False # Scheduling updates of data when script runs
- SERVER_ERROR_MAX_RETRY=3
- EXPIRED_TOKEN_MAX_RETRY=5
- SKIP_REQUEST_ON_SERVER_ERROR=True

- DEVICENAME=Your_Device_Name # e.g. "Charge5"
- LOCAL_TIMEZONE=Automatic # set to "Automatic" for Automatic setup from User profile (if not mentioned here specifically).

# We are using influxdb 1.8 in this stack
influxdb:
restart: unless-stopped
Expand Down
File renamed without changes.

0 comments on commit b1c2434

Please sign in to comment.