-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtemplesync.py
More file actions
139 lines (123 loc) · 5.29 KB
/
templesync.py
File metadata and controls
139 lines (123 loc) · 5.29 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import requests
import tasklists
from datetime import datetime, timezone
from task_types import CollectionLogVerificationData, TaskData
def temple_player_data(username: str):
username = username.replace(' ', '+')
player_data = requests.get(f'https://templeosrs.com/api/collection-log/player_collection_log.php?player={username}&categories=all&itemsonly&includenames=1&onlyitems=1').json()
used_id = set()
cleaned_player_data = list()
if not player_data.get('data'):
return cleaned_player_data
for item in player_data['data']['items']:
if item['id'] not in used_id:
used_id.add(item['id'])
cleaned_player_data.append(item)
return cleaned_player_data
# def test():
# data = temple_player_data('Gerni Task')
# for item in data['data']['items']:
# print(item['name'])
# def get_unix_time(timestamp: str):
# datetime_format = "%Y-%m-%d %H:%M:%S"
# datetime_object = datetime.datetime.strptime(timestamp, datetime_format)
# return time.mktime(datetime_object.timetuple())
# def import_logs(player_name: str, site_tasks: list):
# player_data = temple_player_data(player_name)
# completed_tasks = list()
# for task in site_tasks:
# task_data = task.get('colLogData', None)
# if task_data:
# for item in task_data['include']:
# for log_slot in player_data['data']['items']:
# if item['id'] == log_slot['id']:
# completed_tasks.append(task['_id'])
# break
# return completed_tasks
def check_logs(username: str, site_tasks: list["TaskData"], action: str):
def find_by_id(items, target_id):
return [item for item in items if int(item['id']) == target_id]
def parse_completed_date(value):
if value is None:
return None
if isinstance(value, (int, float)):
# Handle both seconds and milliseconds epoch values
epoch_value = value / 1000 if value > 1e11 else value
return datetime.fromtimestamp(epoch_value, tz=timezone.utc)
if isinstance(value, str):
parsed = None
try:
parsed = datetime.fromisoformat(value.replace('Z', '+00:00'))
except ValueError:
parsed = None
if parsed is None:
try:
parsed = datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
except ValueError:
return None
if parsed.tzinfo is None:
return parsed.replace(tzinfo=timezone.utc)
return parsed.astimezone(timezone.utc)
return None
def get_item_completed_date(item):
possible_keys = [
'obtainedAt', 'obtained_at', 'obtainedDate', 'obtained_date',
'timestamp', 'date', 'updatedAt', 'updated_at', 'acquiredAt', 'acquired_at'
]
for key in possible_keys:
if key in item:
parsed = parse_completed_date(item.get(key))
if parsed is not None:
return parsed
return None
def format_completed_tasks(completed_tasks: dict):
formatted_tasks = []
for task_id in sorted(completed_tasks.keys()):
formatted_tasks.append({
'id' : task_id,
'completedDate': completed_tasks[task_id]['completedDate'],
'completedItemIds': completed_tasks[task_id]['completedItemIds'],
})
return formatted_tasks
cleaned_player_data = temple_player_data(username)
sync_completed_date = datetime.now(timezone.utc).isoformat()
missing_tasks = list()
completed_tasks = {}
recorded_item_ids_by_task = {}
for task in site_tasks:
verification_data = task.verification
if not isinstance(verification_data, CollectionLogVerificationData):
continue
log_count = 0
matched_dates = []
matched_item_ids = set()
for item_id in verification_data.item_ids:
matching_items = find_by_id(cleaned_player_data, item_id)
if matching_items:
log_count += 1
matched_item_ids.add(int(item_id))
for matched_item in matching_items:
item_completed_date = get_item_completed_date(matched_item)
if item_completed_date is not None:
matched_dates.append(item_completed_date)
if log_count >= verification_data.count:
completed_date = max(matched_dates).isoformat() if matched_dates else sync_completed_date
completed_tasks[task.id] = {
'completedDate': completed_date,
'completedItemIds': sorted(matched_item_ids),
}
if matched_item_ids:
recorded_item_ids_by_task[task.id] = sorted(matched_item_ids)
else:
missing_tasks.append(task.name)
if action == 'check':
return missing_tasks
if action == 'import-recorded':
return {
'completedTasks': format_completed_tasks(completed_tasks),
'recordedItemIdsByTask': recorded_item_ids_by_task,
}
else:
return format_completed_tasks(completed_tasks)
if __name__ == "__main__":
check_logs('Gerni Task', tasklists.list_for_tier('easy'), 'check')