-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from kmyk/issue/245
- Loading branch information
Showing
10 changed files
with
226 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Python Version: 3.x | ||
import onlinejudge | ||
import onlinejudge.type | ||
import onlinejudge.implementation.utils as utils | ||
import onlinejudge.implementation.logging as log | ||
import datetime | ||
import json | ||
import pathlib | ||
import time | ||
import traceback | ||
from typing import * | ||
|
||
class DownloadHistory(object): | ||
def __init__(self, path: pathlib.Path = utils.cache_dir / 'download-history.jsonl'): | ||
self.path = path | ||
|
||
def add(self, problem: onlinejudge.type.Problem, directory: pathlib.Path = pathlib.Path.cwd()) -> None: | ||
now = datetime.datetime.now(datetime.timezone.utc).astimezone() | ||
self.path.parent.mkdir(parents=True, exist_ok=True) | ||
with open(str(self.path), 'a') as fh: | ||
fh.write(json.dumps({ | ||
'timestamp': int(time.time()), # this should not be int, but Python's strptime is too weak and datetime.fromisoformat is from 3.7 | ||
'directory': str(directory), | ||
'url': problem.get_url(), | ||
}) + '\n') | ||
log.status('append history to: %s', self.path) | ||
self._flush() | ||
|
||
def _flush(self) -> None: | ||
# halve the size if it is more than 1MiB | ||
if self.path.stat().st_size >= 1024 * 1024: | ||
with open(str(self.path)) as fh: | ||
history_lines = fh.readlines() | ||
with open(str(self.path), 'w') as fh: | ||
fh.write(''.join(history_lines[: - len(history_lines) // 2])) | ||
log.status('halve history at: %s', self.path) | ||
|
||
def get(self, directory: pathlib.Path = pathlib.Path.cwd()) -> List[str]: | ||
if not self.path.exists(): | ||
return [] | ||
|
||
log.status('read history from: %s', self.path) | ||
found = set() | ||
with open(str(self.path)) as fh: | ||
for line in fh: | ||
try: | ||
data = json.loads(line) | ||
except json.decoder.JSONDecodeError as e: | ||
log.warning('corrupted line found in: %s', self.path) | ||
log.debug('%s', traceback.format_exc()) | ||
continue | ||
if pathlib.Path(data['directory']) == directory: | ||
found.add(data['url']) | ||
log.status('found urls in history:\n%s', '\n'.join(found)) | ||
return list(found) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import unittest | ||
|
||
import tests.command_download | ||
|
||
import os | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.