Skip to content

Commit

Permalink
Add search command
Browse files Browse the repository at this point in the history
  • Loading branch information
TakayasuKoura committed Aug 4, 2022
1 parent e099d98 commit da2e824
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
3 changes: 3 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ export * as encoding from "https://cdn.skypack.dev/encoding-japanese";
export { decompress } from "https://deno.land/x/zip@v1.2.3/mod.ts";

export { readXLSX, xlsx } from "https://deno.land/x/flat@0.0.14/mod.ts";

import ky from "https://cdn.skypack.dev/ky@0.31.0?dts";
export { ky };
15 changes: 15 additions & 0 deletions dim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
InitAction,
InstallAction,
ListAction,
SearchAction,
UninstallAction,
UpdateAction,
} from "./libs/actions.ts";
Expand Down Expand Up @@ -89,6 +90,20 @@ await new Command()
],
}),
)
.command(
"search",
new Command()
.option(
"-n, --number <number:integer>",
"Specify the number of data to get by option -n (default 10).",
{
default: 10,
},
)
.arguments("<keyword:string>")
.description("Search data from package_search CKAN API")
.action(new SearchAction().execute),
)
.command("help", new HelpCommand())
.command("complete", new CompletionsCommand())
.parse(Deno.args);
107 changes: 105 additions & 2 deletions libs/actions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Colors, ensureDir, ensureFile, existsSync } from "../deps.ts";
import { Colors, ensureDir, ensureFile, existsSync, ky } from "../deps.ts";
import {
DEFAULT_DATAFILES_PATH,
DEFAULT_DIM_FILE_PATH,
DEFAULT_DIM_LOCK_FILE_PATH,
DEFAULT_SEARCH_ENDPOINT,
DIM_FILE_VERSION,
DIM_LOCK_FILE_VERSION,
} from "./consts.ts";
import { Downloader } from "./downloader.ts";
import { ConsoleAnimation } from "./console_animation.ts";
import { DimFileAccessor, DimLockFileAccessor } from "./accessor.ts";
import { Content, DimJSON, DimLockJSON, LockContent } from "./types.ts";
import {
CkanApiResponse,
Content,
DimJSON,
DimLockJSON,
LockContent,
} from "./types.ts";
import { Encoder } from "./postprocess/encoder.ts";
import { Unzipper } from "./postprocess/unzipper.ts";
import { XLSXConverter } from "./postprocess/xlsx_converter.ts";
Expand Down Expand Up @@ -426,3 +433,99 @@ export class UpdateAction {
}
}
}

export class SearchAction {
async execute(
options: { number: number },
keyword: string,
) {
if (options.number <= 0 || options.number > 100) {
console.error(
Colors.red("Failed to search."),
Colors.red("Please enter a number between 1 and 100"),
);
Deno.exit(1);
}

const keywords = keyword.trim().split(/\s+/);
let searchWord: string;
if (keywords.length === 1) {
searchWord = `*${keywords[0]}*`;
} else {
searchWord = "(" + keywords.map((keyword) =>
`*${keyword}*`
).join(" AND ") + ")";
}

const searchParams = new URLSearchParams(
{
fq:
`xckan_title:${searchWord} OR tags:${searchWord} OR x_ckan_description:${searchWord}`,
rows: options.number.toString(),
},
);

let response: CkanApiResponse;
try {
response = await ky.get(
DEFAULT_SEARCH_ENDPOINT,
{ searchParams },
).json<CkanApiResponse>();
} catch (error) {
console.error(
Colors.red("Failed to search."),
Colors.red(error.message),
);
Deno.exit(1);
}
const datasets = response.result.results;

let i = 1;
for (const dataset of datasets) {
console.log(dataset.xckan_title);
console.log(
" - Package URL :",
Colors.green(dataset.xckan_site_url),
);
console.log(
" - Package Description:",
Colors.green(
dataset.xckan_description == null
? ""
: dataset.xckan_description.replace(/\r(?!\n)/g, "\n"),
),
);
console.log(
" - Package License :",
Colors.green(
dataset.license_title == null ? "" : dataset.license_title,
),
);
for (const resource of dataset.resources) {
console.log(` ${i}.`, resource.name);
console.log(
" * Resourse URL :",
Colors.green(resource.url == null ? "" : resource.url),
);
console.log(
" * Resource Description:",
Colors.green(
resource.description == null
? ""
: resource.description.replace(/\r(?!\n)/g, "\n"),
),
);
console.log(
" * Created :",
Colors.green(resource.created == null ? "" : resource.created),
);
console.log(
" * Format :",
Colors.green(resource.format == null ? "" : resource.format),
);
i++;
}
console.log();
}
}
}
2 changes: 2 additions & 0 deletions libs/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const DEFAULT_DIM_FILE_PATH = "./dim.json";
export const DEFAULT_DIM_LOCK_FILE_PATH = "./dim-lock.json";
export const DIM_FILE_VERSION = "1.1";
export const DIM_LOCK_FILE_VERSION = "1.1";
export const DEFAULT_SEARCH_ENDPOINT =
"https://search.ckan.jp/backend/api/package_search";
22 changes: 22 additions & 0 deletions libs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ export interface DimLockJSON {
lockFileVersion: string;
contents: LockContent[];
}

export interface Resource {
name: string;
url: string;
description: string;
created: string;
format: string;
}

export interface Dataset {
xckan_title: string;
xckan_site_url: string;
xckan_description: string;
license_title: string;
resources: Resource[];
}

export interface CkanApiResponse {
result: {
results: Dataset[];
};
}

0 comments on commit da2e824

Please sign in to comment.