-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
35 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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
import { $ } from 'bun'; | ||
import { exists } from 'node:fs/promises'; | ||
import { join } from 'path'; | ||
|
||
const ARCHIVE = (lang: string) => | ||
`short-abstracts_lang=${lang.toLowerCase()}.ttl.bz2`; | ||
const FILE = (lang: string) => `short-abstracts_lang=${lang.toLowerCase()}.ttl`; | ||
const URL = (lang: string, date: string) => | ||
`https://databus.dbpedia.org/dbpedia/text/short-abstracts/${date}/${ARCHIVE( | ||
lang | ||
)}`; | ||
const DOWNLOAD_DIR = './download'; | ||
|
||
export async function downloadDumps(lang: string, date: string) { | ||
// Replace - with . in date | ||
date = date.replace(/-/g, '.'); | ||
// Check if lang is in format YYYY.MM.DD | ||
if (!/^\d{4}\.\d{2}\.\d{2}$/.test(date)) { | ||
throw new Error(`Invalid date format: ${date}`); | ||
} | ||
|
||
// TODO: Check lang | ||
|
||
// Check if download directory exists | ||
if (!(await exists(DOWNLOAD_DIR))) { | ||
await $`mkdir ${DOWNLOAD_DIR}`; | ||
} | ||
|
||
const archivePath = join(DOWNLOAD_DIR, ARCHIVE(lang)); | ||
const filePath = join(DOWNLOAD_DIR, FILE(lang)); | ||
const archiveExists = await exists(archivePath); | ||
const fileExists = await exists(filePath); | ||
|
||
// Download the archive if neither the file nor archive exists | ||
if (!fileExists && !archiveExists) { | ||
const url = URL(lang, date); | ||
console.log(`Downloading ${url}`); | ||
await $`wget ${url} -O ${archivePath}`; | ||
} | ||
|
||
// Extract the archive if it does not exist | ||
if (!fileExists) { | ||
console.log(`Extracting ${archivePath}`); | ||
await $`bzip2 -dc ${archivePath} >${filePath}`; | ||
} | ||
|
||
return filePath; | ||
} |