Skip to content

Commit

Permalink
Fix edge case, and a few other pieces of code.
Browse files Browse the repository at this point in the history
  • Loading branch information
programmerPhysicist committed Nov 26, 2024
1 parent 7cc3b34 commit 9e736f3
Show file tree
Hide file tree
Showing 7 changed files with 3,519 additions and 1,540 deletions.
27 changes: 14 additions & 13 deletions source/hab_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def due(self):
if self.__task_dict['type'] == 'todo' and self.__task_dict['date'] != '':
date = parser.parse(self.__task_dict['date'])
return date
elif self.__task_dict['type'] == 'daily':
elif self.__task_dict['type'] == 'daily':
if self.__task_dict['isDue'] == True:
date = datetime.now().replace(tzinfo=pytz.utc,hour=0,minute=0,second=0,microsecond=0)
elif self.__task_dict['nextDue'] != '':
date = parser.parse(self.__task_dict['nextDue'][0])
return date
return date
else:
return ''

@property
#When did the daily start running? (That is, is it active now?)
def starting(self):
Expand All @@ -82,26 +82,26 @@ def starting(self):
start = parser.parse(self.__task_dictself.__task_dict['startDate'])
else:
start = ''
return start
return start

@starting.setter
def starting(self, starting):
""" Task name """
self.__task_dict['startDate'] = starting

@property
#Is this a weekly daily or something that repeats every X days?
def rep_pattern(self):
if self.__task_dict['type'] == 'daily':
return self.__task_dict['frequency']
else:
return ''

@rep_pattern.setter
def rep_pattern(self, rep):
""" Task name """
self.__task_dict['frequency'] = rep

@property
#What days of the week does this daily repeat--or in how many days?
def dailies_due(self):
Expand Down Expand Up @@ -131,7 +131,7 @@ def dailies_due(self):
return dayCycle
else:
return ''

@property
#Is this task due today?
def due_now(self):
Expand All @@ -145,7 +145,7 @@ def due_now(self):
#is task complete? 0 for no, 1 for yes
def complete(self):
return self.__task_dict['checked']

@property
def id(self):
""" Task id """
Expand All @@ -161,7 +161,7 @@ def hardness(self):
return "B"
elif diffID == 1:
return "C"
else:
else:
return "D"


Expand All @@ -174,7 +174,7 @@ def name(self):
def name(self, name):
""" Task name """
self.__task_dict['text'] = name

@property
def alias(self):
""" Task name """
Expand Down Expand Up @@ -217,7 +217,7 @@ def category(self):
@category.setter
def category(self, name):
""" Task name """
self.__task_dict['type'] = name
self.__task_dict['type'] = name

@property
def description(self):
Expand All @@ -234,6 +234,7 @@ def completed(self):
""" Task completed """
return self.__task_dict['completed']

# TODO: Doesn't work
@completed.setter
def completed(self, completed):
""" Task completed """
Expand Down
9 changes: 9 additions & 0 deletions source/habitica.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def get_all_habtasks(auth):
hab_tasklist = []
print(response.reason)

# Get completed tasks
auth['type'] = "completedTodos"
response = requests.get(url, headers=auth)
if response.ok:
hab_raw = response.json()
hab_tasklist += hab_raw['data']
else:
print(response.reason)

# keeping records of all our tasks
hab_tasks = []

Expand Down
26 changes: 14 additions & 12 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import configparser
import sys
import pickle
from datetime import datetime
import requests
import time
Expand Down Expand Up @@ -151,7 +152,7 @@ def check_newMatches(matchDict, tod_uniq, hab_uniq):
if oldTid in matchDict.keys():
matchDict.pop(oldTid)
r = add_hab_id(tid, hab_task)
if r.ok:
if not r.ok:
print("Error updating hab %s! %s" % (hab.name, r.reason))
else:
matchDict[tid] = {}
Expand Down Expand Up @@ -411,14 +412,16 @@ def make_tod_from_hab(hab):


def openMatchDict():
import pickle
input_file = 'oneWay_matchDict.pkl'
matchDict = {}
try:
pkl_file = open('oneWay_matchDict.pkl', 'rb')
pkl_load = pickle.Unpickler(pkl_file)
matchDict = pkl_load.load()
pkl_file.close()
except:
matchDict = {}
if os.path.getsize(input_file) > 0:
pkl_file = open(input_file, 'rb')
pkl_load = pickle.Unpickler(pkl_file)
matchDict = pkl_load.load()
pkl_file.close()
except OSError as e:
print(e)

for tid in matchDict:
if 'recurs' not in matchDict[tid].keys():
Expand Down Expand Up @@ -660,10 +663,9 @@ def update_hab_matchDict(hab_tasks, matchDict):
expired_tids.append(tid)

for tid in expired_tids:
if tid not in matchDict.keys():
continue
else:
matchDict.pop(tid)
if tid in matchDict.keys():
if not matchDict[tid]['hab'].completed:
matchDict.pop(tid)

return matchDict

Expand Down
52 changes: 36 additions & 16 deletions source/oneWaySync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import main
from todo_task import TodTask
from hab_task import HabTask
from todo_api_plus import TodoAPIPlus
import config
import habitica
Expand All @@ -29,6 +30,18 @@ def get_tasks(token):
return tasks, api


def completeTodoist(todo_api, task_id):
try:
is_success = todo_api.close_task(task_id=task_id)
except Exception as e:
print(e)
else:
if is_success:
print("INFO: Update Todoist task to done, task_id=" + str(task_id))
else:
print("ERROR: Unable to update todoist task, task_id=" + str(task_id))


def sync_todoist_to_habitica():
# todayFilter = todo_api.filters.add('todayFilter', 'today')

Expand Down Expand Up @@ -88,19 +101,26 @@ def sync_todoist_to_habitica():
response = main.write_hab_task(new_dict)
if not response.ok:
# TODO: check ['errors'], due to it sometimes not having it
try:
json_str = response.json()
except:
print("Unknown json error!")
else:
if 'errors' in json_str.keys():
err_msg = json_str['errors'][0]['message']
alias = json_str['errors'][0]['value']
elif 'error' in json_str.keys():
breakpoint()
err_msg = json_str['message']
msg = "Error Code " + str(response.status_code) + ": \"" + err_msg + "\", Task alias: " + alias
print(msg)
if response.status_code == 400 and response.reason == 'Bad Request':
try:
json_str = response.json()
except:
print("Unknown json error!")
else:
if 'errors' in json_str.keys():
alias = json_str['errors'][0]['value']
elif 'error' in json_str.keys():
err_msg = json_str['message']
print(err_msg)
print("WARNING: Bad request, already existing task - " + alias)
completeTodoist(todo_api, alias)
hab = HabTask()
hab.task_dict['completed'] = True
hab.task_dict['alias'] = alias

match_dict[alias] = {}
match_dict[alias]['tod'] = tod
match_dict[alias]['hab'] = hab
else:
print("Added hab to %s!" % tod.name)
fin_hab = main.get_hab_fromID(tid)
Expand Down Expand Up @@ -172,8 +192,8 @@ def sync_todoist_to_habitica():
matched_hab = main.sync_hab2todo(hab, tod)
response = main.update_hab(matched_hab)
elif hab.completed:
fix_tod = todo_api.items.get_by_id(tid)
fix_tod.close()
# fix_tod = todo_api.items.get_by_id(tid)
# fix_tod.close()
print('completed tod %s' % tod.name)
else:
print("ERROR: check HAB %s" % tid)
Expand Down Expand Up @@ -202,7 +222,7 @@ def sync_todoist_to_habitica():
# match_dict[tid]['hab'].task_dict['date'] = dueNow
# response = main.update_hab(match_dict[tid]['hab'])

pkl_file = open('oneWay_match_dict.pkl', 'wb')
pkl_file = open('oneWay_matchDict.pkl', 'wb')
pkl_out = pickle.Pickler(pkl_file, -1)
pkl_out.dump(match_dict)
pkl_file.close()
Expand Down
1 change: 1 addition & 0 deletions source/todo_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def hardness(self):
def is_completed(self):
return self.__task_dict['is_completed']

# TODO: No longer works
@is_completed.setter
def complete(self, status):
self.__task_dict['checked'] = status
Expand Down
Loading

0 comments on commit 9e736f3

Please sign in to comment.