Skip to content

Commit

Permalink
Some more fixes to filesaving
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed Apr 30, 2024
1 parent fc857af commit 14c4956
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
5 changes: 4 additions & 1 deletion sync/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def dir_for_project(project) -> Path:

def create_project_dirs(projects):
for project in projects:
dir_for_project(project).mkdir(parents=True,exist_ok=True)
create_dir_if_not_exists(dir_for_project(project))

def create_dir_if_not_exists(path: Path):
path.mkdir(parents=True,exist_ok=True)

def run():
runner.run()
9 changes: 5 additions & 4 deletions sync/document_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def filename_from_url(url: str) -> str:
path = urlparse(url).path
return unquote(Path(path).name)

def last_modified_date(headers):
def last_modified_date(headers) -> str:
# https://stackoverflow.com/a/70641487/6830113
import collections
collections.Callable = collections.abc.Callable
Expand All @@ -19,9 +19,10 @@ def last_modified_date(headers):
def download(project: any, url: str):
# TODO: Get the latest document on disk
response = requests.get(url)
path = core.dir_for_project(project) / last_modified_date(response.headers) / filename_from_url(url)
print(f"Downloaded {url} to {path}")
filepath = core.dir_for_project(project) / last_modified_date(response.headers) / filename_from_url(url)
core.create_dir_if_not_exists(filepath.parent)
print(f"Downloaded {url} to {filepath}")
# TODO: Don't create day dir if no changes happened
# if changed_date > last_date, write new file
with open(path, 'wb') as f:
with open(filepath, 'wb') as f:
f.write(response.content)
5 changes: 3 additions & 2 deletions sync/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import json

def project_to_json(project: dict):
dest = core.dir_for_project(project) / date.today() / "project.json"
with open(dest, "w") as outfile:
filepath = core.dir_for_project(project) / date.today().strftime("%Y-%m-%d") / "project.json"
core.create_dir_if_not_exists(filepath.parent)
with open(filepath, "w") as outfile:
json.dump(project, outfile)

def run():
Expand Down

0 comments on commit 14c4956

Please sign in to comment.