Add parkio
This commit is contained in:
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;
|
||||
}
|
Reference in New Issue
Block a user