-
Notifications
You must be signed in to change notification settings - Fork 2
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
Kevin Wang
committed
Dec 29, 2023
1 parent
1e6754c
commit f885817
Showing
71 changed files
with
491,279 additions
and
476,836 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,9 @@ | ||
export enum Scraper { | ||
Twse = 'twse', | ||
Tpex = 'tpex', | ||
Taifex = 'taifex', | ||
Tdcc = 'tdcc', | ||
Mis = 'mis', | ||
Mops = 'mops', | ||
Isin = 'isin', | ||
} |
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 +1,2 @@ | ||
export * from './ticker.interface'; | ||
export * from './rate-limit-options.interface'; |
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,4 @@ | ||
export interface RateLimitOptions { | ||
ttl: number; | ||
limit: number; | ||
} |
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,15 +1,9 @@ | ||
import { IsinScraper as IsinScraperStatic } from './isin-scraper'; | ||
import { TwseScraper as TwseScraperStatic } from './twse-scraper'; | ||
import { TpexScraper as TpexScraperStatic } from './tpex-scraper'; | ||
import { MisScraper as MisScraperStatic } from './mis-scraper'; | ||
import { TdccScraper as TdccScraperStatic } from './tdcc-scraper'; | ||
import { MopsScraper as MopsScraperStatic } from './mops-scraper'; | ||
import { TaifexScraper as TaifexScraperStatic } from './taifex-scraper'; | ||
|
||
export const IsinScraper = new IsinScraperStatic(); | ||
export const TwseScraper = new TwseScraperStatic(); | ||
export const TpexScraper = new TpexScraperStatic(); | ||
export const MisScraper = new MisScraperStatic(); | ||
export const TdccScraper = new TdccScraperStatic(); | ||
export const MopsScraper = new MopsScraperStatic(); | ||
export const TaifexScraper = new TaifexScraperStatic(); | ||
export * from './scraper'; | ||
export * from './twse-scraper'; | ||
export * from './tpex-scraper'; | ||
export * from './taifex-scraper'; | ||
export * from './tdcc-scraper'; | ||
export * from './mis-scraper'; | ||
export * from './mops-scraper'; | ||
export * from './isin-scraper'; | ||
export * from './scraper-factory'; |
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
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,66 @@ | ||
import { TwseScraper } from './twse-scraper'; | ||
import { TpexScraper } from './tpex-scraper'; | ||
import { TaifexScraper } from './taifex-scraper'; | ||
import { TdccScraper } from './tdcc-scraper'; | ||
import { MisScraper } from './mis-scraper'; | ||
import { MopsScraper } from './mops-scraper'; | ||
import { IsinScraper } from './isin-scraper'; | ||
import { Scraper } from './scraper'; | ||
import { Scraper as ScraperType } from '../enums'; | ||
import { RateLimitOptions } from '../interfaces'; | ||
|
||
export class ScraperFactory { | ||
private readonly scrapers: Map<string, Scraper> = new Map(); | ||
|
||
constructor(private readonly options?: RateLimitOptions) {} | ||
|
||
get(type: ScraperType) { | ||
let scraper = this.scrapers.get(type); | ||
|
||
if (!scraper) { | ||
const scrapers = { | ||
[ScraperType.Twse]: TwseScraper, | ||
[ScraperType.Tpex]: TpexScraper, | ||
[ScraperType.Taifex]: TaifexScraper, | ||
[ScraperType.Tdcc]: TdccScraper, | ||
[ScraperType.Mis]: MisScraper, | ||
[ScraperType.Mops]: MopsScraper, | ||
[ScraperType.Isin]: IsinScraper, | ||
}; | ||
const ScraperClass = scrapers[type]; | ||
|
||
scraper = new ScraperClass(this.options); | ||
this.scrapers.set(type, scraper); | ||
} | ||
|
||
return scraper; | ||
} | ||
|
||
getTwseScraper() { | ||
return this.get(ScraperType.Twse) as TwseScraper; | ||
} | ||
|
||
getTpexScraper() { | ||
return this.get(ScraperType.Tpex) as TpexScraper; | ||
} | ||
|
||
getTaifexScraper() { | ||
return this.get(ScraperType.Taifex) as TaifexScraper; | ||
} | ||
|
||
getTdccScraper() { | ||
return this.get(ScraperType.Tdcc) as TdccScraper; | ||
} | ||
|
||
getMisScraper() { | ||
return this.get(ScraperType.Mis) as MisScraper; | ||
} | ||
|
||
getMopsScraper() { | ||
return this.get(ScraperType.Mops) as MopsScraper; | ||
} | ||
|
||
getIsinScraper() { | ||
return this.get(ScraperType.Isin) as IsinScraper; | ||
} | ||
} |
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,9 +1,14 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
import * as rateLimit from 'axios-rate-limit'; | ||
import { RateLimitOptions } from '../interfaces'; | ||
|
||
export abstract class Scraper { | ||
protected readonly httpService: AxiosInstance; | ||
|
||
constructor() { | ||
this.httpService = axios.create(); | ||
constructor(options?: RateLimitOptions) { | ||
const maxRequests = options?.limit ?? 3; | ||
const perMilliseconds = options?.ttl ?? 5000; | ||
// @ts-ignore | ||
this.httpService = rateLimit(axios.create(), { maxRequests, perMilliseconds }); | ||
} | ||
} |
Oops, something went wrong.