Skip to content

Fetch individual translation archives for cache (#217) #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,35 +374,38 @@ def output(page: str, plain: bool = False) -> None:


def update_cache(language: Optional[List[str]] = None) -> None:
if language is None:
tldr_language = os.environ.get("TLDR_LANGUAGE", get_default_language())
language = tldr_language if tldr_language else 'en'
elif isinstance(language, list):
language = language[0]
try:
pages_dir = "pages"
if language and language != 'en':
pages_dir += "." + language
req = urlopen(Request(
DOWNLOAD_CACHE_LOCATION,
headers=REQUEST_HEADERS
), context=URLOPEN_CONTEXT)
zipfile = ZipFile(BytesIO(req.read()))
pattern = re.compile(r"{}/(.+)/(.+)\.md".format(pages_dir))
cached = 0
for entry in zipfile.namelist():
match = pattern.match(entry)
if match:
store_page_to_cache(
zipfile.read(entry),
match.group(2),
match.group(1),
language
)
cached += 1
print("Updated cache for {:d} entries".format(cached))
except Exception:
sys.exit("Error: Unable to update cache from " + DOWNLOAD_CACHE_LOCATION)
languages = get_language_list()
if language and language[0] not in languages:
languages.append(language[0])
for language in languages:
try:
cache_location = f"{DOWNLOAD_CACHE_LOCATION[:-4]}-pages.{language}.zip"
req = urlopen(Request(
cache_location,
headers=REQUEST_HEADERS
), context=URLOPEN_CONTEXT)
zipfile = ZipFile(BytesIO(req.read()))
pattern = re.compile(r"(.+)/(.+)\.md")
cached = 0
for entry in zipfile.namelist():
match = pattern.match(entry)
if match:
store_page_to_cache(
zipfile.read(entry),
match.group(2),
match.group(1),
language
)
cached += 1
print(
"Updated cache for language "
f"{language}: {cached} entries"
)
except Exception:
print(
"Error: Unable to update cache for language "
f"{language} from {cache_location}"
)


def create_parser() -> ArgumentParser:
Expand Down