Add parkio
This commit is contained in:
22
src/app.controller.spec.ts
Normal file
22
src/app.controller.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
});
|
12
src/app.controller.ts
Normal file
12
src/app.controller.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
}
|
11
src/app.module.ts
Normal file
11
src/app.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { ParkioModule } from './parkio/parkio.module';
|
||||
|
||||
@Module({
|
||||
imports: [ParkioModule],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
8
src/app.service.ts
Normal file
8
src/app.service.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
8
src/main.ts
Normal file
8
src/main.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
await app.listen(3000);
|
||||
}
|
||||
bootstrap();
|
33
src/parkio/parkio.controller.ts
Normal file
33
src/parkio/parkio.controller.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ParkioService } from './parkio.service';
|
||||
|
||||
@Controller('parkio')
|
||||
export class ParkioController {
|
||||
constructor(
|
||||
private readonly parkioService: ParkioService
|
||||
) {}
|
||||
|
||||
@Get('auctions')
|
||||
async getAuctions () {
|
||||
return this.parkioService.fetchAuctions();
|
||||
}
|
||||
|
||||
@Get('domains')
|
||||
async getDomains () {
|
||||
return this.parkioService.fetchAllDomains();
|
||||
}
|
||||
|
||||
@Get('short/:maxLength')
|
||||
async getShort (@Param('maxLength') maxLength: number) {
|
||||
return {
|
||||
maxLength,
|
||||
domains: (await this.parkioService.fetchAllDomains()).filter(
|
||||
parsedDomain => parsedDomain.domain_length <= maxLength
|
||||
),
|
||||
auctions: (await this.parkioService.fetchAuctions()).filter(
|
||||
parsedAuction => parsedAuction.domain_length <= maxLength
|
||||
),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
9
src/parkio/parkio.module.ts
Normal file
9
src/parkio/parkio.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ParkioService } from './parkio.service';
|
||||
import { ParkioController } from './parkio.controller';
|
||||
|
||||
@Module({
|
||||
providers: [ParkioService],
|
||||
controllers: [ParkioController]
|
||||
})
|
||||
export class ParkioModule {}
|
117
src/parkio/parkio.service.ts
Normal file
117
src/parkio/parkio.service.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
Auction,
|
||||
AuctionsResponse,
|
||||
Domain,
|
||||
DomainSet,
|
||||
DomainsResponse,
|
||||
ParkioTld,
|
||||
ParsedAuction,
|
||||
ParsedDomain,
|
||||
} from './types';
|
||||
import axios from 'axios';
|
||||
import { filter, map, pipe } from 'ramda';
|
||||
|
||||
const parkIoEndpoints = {
|
||||
domains: 'https://park.io/domains.json',
|
||||
auctions: 'https://park.io/auctions.json',
|
||||
tld: (tld: ParkioTld) => `https://park.io/domains/index/${tld}.json`,
|
||||
all: (limit: number = 10_000) =>
|
||||
`https://park.io/domains/index/all.json?limit=${limit}`,
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class ParkioService {
|
||||
fetchDomainsForTld = async (tld: ParkioTld) => {};
|
||||
|
||||
fetchAllDomains = async (limit: number = 10_000): Promise<ParsedDomain[]> => {
|
||||
let response = await axios.get<DomainsResponse>(parkIoEndpoints.all(limit));
|
||||
|
||||
// There may have been more than our set limit
|
||||
if (response.data.current === limit) {
|
||||
return await this.fetchAllDomains(limit + 200);
|
||||
}
|
||||
|
||||
return map(this.parseDomain, response.data.domains);
|
||||
};
|
||||
|
||||
fetchAuctions = async (): Promise<ParsedAuction[]> => {
|
||||
const response = await axios.get<AuctionsResponse>(
|
||||
parkIoEndpoints.auctions,
|
||||
);
|
||||
|
||||
return map(this.parseAuction, response.data.auctions);
|
||||
};
|
||||
|
||||
lengthFilter = (length: number) => (parsedDomain: ParsedDomain) =>
|
||||
parsedDomain.domain.length === length;
|
||||
|
||||
buildExtensions = (domains: ParsedDomain[], extensions: ParkioTld[]) =>
|
||||
pipe(
|
||||
map((extension: ParkioTld) =>
|
||||
pipe(
|
||||
filter(this.tldFilter(extension)),
|
||||
filter(this.maxLengthFilter(3)),
|
||||
)(domains),
|
||||
),
|
||||
)(extensions);
|
||||
|
||||
extractDomain = (domain: string): string =>
|
||||
domain.split('.').slice(0, -1).join('.');
|
||||
|
||||
extractTld = (domain: string): string =>
|
||||
domain.split('.').slice(-1).join('.');
|
||||
|
||||
parseDomain = (domain: Domain): ParsedDomain => {
|
||||
const domainName = this.extractDomain(domain.name);
|
||||
return {
|
||||
...domain,
|
||||
id: Number(domain.id),
|
||||
domain: domainName,
|
||||
domain_length: domainName.length,
|
||||
date_available: domain.date_available
|
||||
? this.parseDate(domain.date_available)
|
||||
: undefined,
|
||||
date_registered: domain.date_registered
|
||||
? this.parseDate(domain.date_registered)
|
||||
: undefined,
|
||||
};
|
||||
};
|
||||
|
||||
parseDate = (date: string) =>
|
||||
new Date(
|
||||
Date.parse(
|
||||
date.replace(
|
||||
/(\d+)-(\d+)-(\d+)UTC(\d+:\d+:\d{0,2})\d*/,
|
||||
'$1-$3-$2T$4Z',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
parseAuction = (auction: Auction): ParsedAuction => {
|
||||
const domain = this.extractDomain(auction.name);
|
||||
const tld = this.extractTld(auction.name);
|
||||
return {
|
||||
...auction,
|
||||
id: Number(auction.id),
|
||||
num_bids: Number(auction.num_bids),
|
||||
price: Number(auction.price),
|
||||
close_date: this.parseDate(auction.close_date),
|
||||
created: this.parseDate(auction.created),
|
||||
domain,
|
||||
domain_length: domain.length,
|
||||
tld,
|
||||
};
|
||||
};
|
||||
|
||||
domainToString = (domain: ParsedDomain) =>
|
||||
`${domain.name}\t${domain.date_available}`;
|
||||
|
||||
auctionToString = (auction: ParsedAuction) =>
|
||||
`${auction.name}\t$${auction.price}\t${auction.close_date}`;
|
||||
|
||||
tldFilter = (tld: ParkioTld) => (domain: ParsedDomain) => domain.tld === tld;
|
||||
|
||||
maxLengthFilter = (length: number) => (parsedDomain: ParsedDomain) =>
|
||||
parsedDomain.domain.length <= length;
|
||||
}
|
83
src/parkio/types.ts
Normal file
83
src/parkio/types.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
export const TLDs = [
|
||||
'io',
|
||||
'co',
|
||||
'us',
|
||||
'to',
|
||||
'gg',
|
||||
'ly',
|
||||
'vc',
|
||||
'me',
|
||||
'sh',
|
||||
'bz',
|
||||
'ag',
|
||||
'sc',
|
||||
'ac',
|
||||
'lc',
|
||||
'je',
|
||||
'mn',
|
||||
'pro',
|
||||
'info',
|
||||
'red',
|
||||
] as const;
|
||||
|
||||
export type ParkioTld = typeof TLDs[number];
|
||||
|
||||
export type DomainSet = { [key: string]: ParsedDomain[] };
|
||||
|
||||
export interface Domain {
|
||||
id: string;
|
||||
name: string;
|
||||
date_available: string;
|
||||
date_registered: string;
|
||||
tld: string;
|
||||
}
|
||||
|
||||
export interface Auction {
|
||||
id: string;
|
||||
name: string;
|
||||
num_bids: string;
|
||||
price: string;
|
||||
close_date: string;
|
||||
created: string;
|
||||
}
|
||||
|
||||
export interface ParsedAuction {
|
||||
id: number;
|
||||
name: string;
|
||||
domain: string;
|
||||
domain_length: number;
|
||||
tld: string;
|
||||
num_bids: number;
|
||||
price: number;
|
||||
close_date: Date;
|
||||
created: Date;
|
||||
}
|
||||
|
||||
export interface ParsedDomain {
|
||||
id: number;
|
||||
domain: string;
|
||||
domain_length: number
|
||||
name: string;
|
||||
tld: string;
|
||||
date_available?: Date;
|
||||
date_registered?: Date;
|
||||
}
|
||||
|
||||
export interface BaseResponse {
|
||||
page: number,
|
||||
current: number,
|
||||
count: number,
|
||||
prevPage: boolean,
|
||||
nextPage: boolean,
|
||||
pageCount: number;
|
||||
limit: number,
|
||||
success: boolean,
|
||||
}
|
||||
|
||||
export interface DomainsResponse extends BaseResponse{
|
||||
domains: Domain[]
|
||||
}
|
||||
|
||||
export interface AuctionsResponse extends BaseResponse {
|
||||
auctions: Auction[]
|
||||
}
|
Reference in New Issue
Block a user