diff --git a/README.md b/README.md index 8dc4c88..cbcf67f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,15 @@ cd parse-timelogs-for-upload python -m pip install --user -r requirements.txt ``` +### Create local environments file + +In a `.env` file, put your Harvest account ID and access token, both of which you can get at https://id.getharvest.com/ + +``` +HARVEST_ACCESS_TOKEN=12345.pt.6W7wKRJEsG73NaNwBWBhv_5rQz1YkiC7_0U-OuYNnYZlMh4xP-HvmloBlrFcpJ5ZbT666HJOhNo3tXispFz4wk +HARVEST_ACCOUNT_ID=123456 +``` + ## Usage ```bash diff --git a/fetch_clients_projects.py b/fetch_clients_projects.py index cf204fb..fe4fab8 100644 --- a/fetch_clients_projects.py +++ b/fetch_clients_projects.py @@ -1,11 +1,14 @@ import os import json import urllib.request -from dotenv import load_dotenv +# Import our local settings management. +import settings +# Allow HARVEST_ACCESS_TOKEN etc to be loaded from a .env file. +from dotenv import load_dotenv load_dotenv() -url = "https://api.harvestapp.com/v2/projects?updated_since=2021-05-01" +url = "https://api.harvestapp.com/v2/projects?is_active=true" headers = { "User-Agent": "Python Harvest API", "Authorization": "Bearer " + os.environ.get("HARVEST_ACCESS_TOKEN"), @@ -24,4 +27,4 @@ projects = {} for project in api_projects["projects"]: projects[project["name"]] = project["client"]["name"] - +settings.harvest_set_projects_clients_map(projects) diff --git a/pomodoro_to_harvest.py b/pomodoro_to_harvest.py index 9fdbc15..643a6a8 100644 --- a/pomodoro_to_harvest.py +++ b/pomodoro_to_harvest.py @@ -177,9 +177,7 @@ harvest = hrvst.rename(columns = {'date': 'Date', 'project': 'Project', 'subproj harvest["Hours"] = harvest["time"]/60 harvest["First name"] = "Benjamin" harvest["Last name"] = "Melançon" -project_client_mapping = { - "": "", -} +project_client_mapping = settings.harvest_get_projects_clients_map() harvest["Client"] = harvest["Project"].map(project_client_mapping) harvest.drop(columns = ['started', 'recorded', 'time'], inplace=True) diff --git a/settings.py b/settings.py index fc85a5e..8a8d795 100644 --- a/settings.py +++ b/settings.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 import configparser +import json import os config = configparser.ConfigParser() config['pomodoro'] = {} pomodoro = config['pomodoro'] +config['harvest'] = {} +harvest = config['harvest'] def write(): with open('settings.ini', 'w') as configfile: @@ -37,3 +40,10 @@ def pomodoro_latest_recorded(timestamp = None): write() else: return pomodoro.get('latest_recorded', None) + +def harvest_set_projects_clients_map(projects_clients_map = {}): + config.set('harvest', 'projects_clients', json.dumps(projects_clients_map)) + write() + +def harvest_get_projects_clients_map(): + return json.loads(harvest['projects_clients'])