Create utility function to convert API dates to datetime objects

This commit is contained in:
Mathieu Comandon 2023-09-14 15:06:28 -07:00
parent 59151f0cbe
commit 85bf1dba8d
2 changed files with 9 additions and 6 deletions

View file

@ -1,5 +1,6 @@
"""Functions to interact with the Lutris REST API""" """Functions to interact with the Lutris REST API"""
import functools import functools
import time
import json import json
import os import os
import re import re
@ -21,6 +22,11 @@ API_KEY_FILE_PATH = os.path.join(settings.CACHE_DIR, "auth-token")
USER_INFO_FILE_PATH = os.path.join(settings.CACHE_DIR, "user.json") USER_INFO_FILE_PATH = os.path.join(settings.CACHE_DIR, "user.json")
def get_time_from_api_date(date_string):
"""Convert a date string originating from the API and convert it to a datetime object"""
return time.strptime(date_string[:date_string.find(".")], "%Y-%m-%dT%H:%M:%S")
def load_runtime_versions() -> dict: def load_runtime_versions() -> dict:
"""Load runtime versions from json file""" """Load runtime versions from json file"""
if not system.path_exists(settings.RUNTIME_VERSIONS_PATH): if not system.path_exists(settings.RUNTIME_VERSIONS_PATH):

View file

@ -6,7 +6,7 @@ import time
from gi.repository import GLib from gi.repository import GLib
from lutris import settings from lutris import settings
from lutris.api import download_runtime_versions, load_runtime_versions from lutris.api import download_runtime_versions, load_runtime_versions, get_time_from_api_date
from lutris.util import http, jobs, system, update_cache from lutris.util import http, jobs, system, update_cache
from lutris.util.downloader import Downloader from lutris.util.downloader import Downloader
from lutris.util.extract import extract_archive from lutris.util.extract import extract_archive
@ -103,8 +103,7 @@ class Runtime:
def download(self, remote_runtime_info: dict): def download(self, remote_runtime_info: dict):
"""Downloads a runtime locally""" """Downloads a runtime locally"""
remote_updated_at = remote_runtime_info["created_at"] remote_updated_at = get_time_from_api_date(remote_runtime_info["created_at"])
remote_updated_at = time.strptime(remote_updated_at[:remote_updated_at.find(".")], "%Y-%m-%dT%H:%M:%S")
if not self.should_update(remote_updated_at): if not self.should_update(remote_updated_at):
return None return None
downloader = self.get_downloader(remote_runtime_info) downloader = self.get_downloader(remote_runtime_info)
@ -139,9 +138,7 @@ class Runtime:
components = self.get_runtime_components() components = self.get_runtime_components()
downloads = [] downloads = []
for component in components: for component in components:
modified_at = time.strptime( modified_at = get_time_from_api_date(component["modified_at"])
component["modified_at"][:component["modified_at"].find(".")], "%Y-%m-%dT%H:%M:%S"
)
if not self.should_update_component(component["filename"], modified_at): if not self.should_update_component(component["filename"], modified_at):
continue continue
downloads.append(component) downloads.append(component)