Skip to content

Commit

Permalink
More flexible dynamic parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
JexSrs committed Oct 24, 2023
1 parent ed22006 commit 55df144
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions docs/source_files/dynamic.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ return a unique string that will help Saffron to identify of the implementation.
}
```
Following, we will implement the `request` method, which is responsible to do
all the network requests. The response should include one or multiple objects of
type `AxiosResponse`.
all the network requests.

In cases where a login is required to the remote website, it can be done from here.

```ts
class Custom extends DynamicSourceFile {
// ...
request(utils: Utils): Promise<RequestsResult> {
request(utils: Utils): Promise<any> {
// Request using utils.get to assign the axios config
// passed in the global and/or source configurations
return utils.get(utils.url);
Expand All @@ -39,7 +38,7 @@ class Custom extends DynamicSourceFile {
}
```
Lastly, we are going to implement the `parse` method, which is responsible to do
all the parsing. It will receive the requests responses from the `request` method
all the parsing. It will receive the payload returned from the `request` method
and must return an array of Articles.

```ts
Expand All @@ -57,6 +56,8 @@ class Custom extends DynamicSourceFile {
## Scrape

### `implementation`
Default value: `<source name>`

The name of the implementation we have configured.

## Utils
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unistudents/saffron",
"version": "6.0.0",
"version": "6.1.0",
"description": "A fairly intuitive & powerful framework that enables you to collect & save articles and news from all over the web. ",
"license": "MIT",
"homepage": "https://github.com/unistudents/saffron#readme",
Expand Down
2 changes: 1 addition & 1 deletion src/components/parser.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type JsonSkipOptions = {
};

export type ScrapeDynamic = {
implementation: string;
implementation?: string;
};

export type ScrapeHTML = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type InstructionUrl = {

export type ParserResult = InstructionUrl & { articles: Article[]; };

export type RequestsResult = AxiosResponse | AxiosResponse[];
export type RequestsResult = any;

export type CallbackVoid = (...args: any[]) => void;

Expand Down Expand Up @@ -103,7 +103,7 @@ export type SourceFile = {
extra?: any;
} & ({
type: 'dynamic'
scrape: ScrapeDynamic;
scrape?: ScrapeDynamic;
} | {
type: 'html'
scrape: ScrapeHTML;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Worker {
utils.source = job.source;

if (instructions.parserType === ParserType.DYNAMIC) {
const impName = instructions.dynamic.implementation;
const impName = instructions.dynamic.implementation ?? job.source.name;
const dsf = dynamicSourceFiles.find(dsf => dsf.name() === impName);
if(!dsf) {
throw new Error(`could not find any implementation with name ${impName}`);
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/dynamic.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export class DynamicParser extends Parser {
// This exists only for typescript, it is not valid and will not run at runtime.
const scrape = s as ScrapeDynamic;

if (typeof scrape !== 'object' || Array.isArray(scrape))
if (typeof scrape !== 'undefined' && (typeof scrape !== 'object' || Array.isArray(scrape)))
throw new Error("must be a JSON object");

if (typeof scrape.implementation !== 'string' || scrape.implementation.length === 0)
if (typeof scrape.implementation !== 'undefined' && (typeof scrape.implementation !== 'string' || scrape.implementation.length === 0))
throw new Error(`implementation must be non empty string`);
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"ignoreDeprecations": "5.0",

"target": "ES6",
"module": "commonjs",

Expand Down

0 comments on commit 55df144

Please sign in to comment.