Skip to content
This repository was archived by the owner on Mar 16, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "15:00"
timezone: Europe/Vienna
open-pull-requests-limit: 20
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"repository": "https://github.com/iiAbady/blocksmc",
"author": "Abady <gamersspeaks@gmail.com>",
"license": "Apache-2.0",
"private": false,
"keywords": [
"scrapper",
"blocksmc",
Expand All @@ -25,16 +24,16 @@
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"node-fetch": "^2.6.0"
"node-fetch": "^3.1.0"
},
"devDependencies": {
"@types/cheerio": "^0.22.11",
"@types/node-fetch": "^2.3.7",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^6.2.1",
"eslint-config-marine": "^6.0.0",
"typescript": "^4.2.3"
"@types/node-fetch": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"eslint": "^8.4.0",
"eslint-config-marine": "^9.1.0",
"typescript": "^4.5.2"
},
"eslintConfig": {
"extends": "marine/node"
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Blocks from './structures/Blocks';
const { version }: { version: string } = require('../package.json'); // eslint-disable-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
import { version, } from '../package.json';

export default Blocks;

export {
Blocks, version
Blocks, version,
};
93 changes: 42 additions & 51 deletions src/structures/Blocks.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
import fetch from 'node-fetch';
import * as cheerio from 'cheerio';
import { Player, Top } from '../responses/Responses';

interface DataOptions {
type: 'player' | 'top';
username?: string;
game?: string;
}
import type { Player, Top, } from '../responses/Responses';

export default class Blocks {
public async player(username: string): Promise<Player> {
if (typeof username !== 'string') throw new TypeError(`[BlocksMC] expected string on username, got ${typeof username}`);
return this._getData({ type: 'player', username });
public async player(username: string,): Promise<Player> {
if (typeof username !== 'string') throw new TypeError(`[BlocksMC] expected string on username, got ${typeof username}`,);
return this._getPlayer(username,);
}

public async top(game: string): Promise<Top> {
if (typeof game !== 'string') throw new TypeError(`[BlocksMC] expected string on game, got ${typeof game}`);
return this._getData({ type: 'top', game });
public async top(game: string,): Promise<Top> {
if (typeof game !== 'string') throw new TypeError(`[BlocksMC] expected string on game, got ${typeof game}`,);
return this._getLeader(game,);
}

private async _getData(options: DataOptions): Promise<any> {
if (options.type === 'player') {
const data: any = { games: [] };
const res = await fetch(`https://blocksmc.com/player/${options.username}`);
const rawData = await res.text();
const $ = cheerio.load(rawData);
const name = $('.profile-header h1').text().trim();
const rank = $('.profile-rank').text().replace('\n', '')
.trim();
const timePlayed = $('h1[dir=ltr]').text().replace('\n', '')
.trim();
Object.assign(data, { name, rank, timePlayed });
private async _getPlayer(player: string,): Promise<any> {
const data: any = { games: [], };
const res = await fetch(`https://blocksmc.com/player/${player}`,);
const rawData = await res.text();
const $ = cheerio.load(rawData,);
const name = $('.profile-header h1',).text().trim();
const rank = $('.profile-rank',).text().replace('\n', '',)
.trim();
const timePlayed = $('h1[dir=ltr]',).text().replace('\n', '',)
.trim();
Object.assign(data, { name, rank, timePlayed, },);
// eslint-disable-next-line func-names
$('div.col-xl-4',).each(() => {
const stats = {};
// eslint-disable-next-line func-names
$('div.col-xl-4').each(function(this: any) {
const stats = {};
// eslint-disable-next-line func-names
$(this).find('li').each(function(this: any) {
Object.assign(stats, { [$(this).find('div.key').text()]: $(this).find('div.val').text() });
});
data.games.push({
game: $(this).find('div.title').text()
.trim(),
stats
});
});
return data;
}
$(this,).find('li',).each(() => {
Object.assign(stats, { [$(this,).find('div.key',).text()]: $(this,).find('div.val',).text(), },);
},);
data.games.push({
game: $(this,).find('div.title',).text()
.trim(),
stats,
},);
},);
return data;
}

if (options.type === 'top') {
const data: any = [];
const res = await fetch(`https://blocksmc.com/${options.game!.toLowerCase().split(' ').join('-')
.trim()}`);
const rawData = await res.text();
const $ = cheerio.load(rawData);
// eslint-disable-next-line func-names
$('tbody').find('a').each(function(this: any) {
data.push($(this).text());
});
return data;
}
private async _getLeader(game: string,) {
const data: any = [];
const res = await fetch(`https://blocksmc.com/${game.toLowerCase().split(' ',).join('-',)
.trim()}`,);
const rawData = await res.text();
const $ = cheerio.load(rawData,);
$('tbody',).find('a',).each(() => {
data.push($(this,).text(),);
},);
return data;
}
}
21 changes: 13 additions & 8 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const { Blocks } = require('../dist');
const blocks = new Blocks();
blocks.player('iAbady').then(player => {
console.log(player.name);
});
const { Blocks } = require('../package.json');
console.log(Blocks)
// const blocks = new Blocks();

blocks.top('Sky Wars').then(leader => {
console.log(leader)
})
// // eslint-disable-next-line arrow-parens
// blocks.player('iAbady',).then((player,) => {
// console.log(player,);
// },);


// // eslint-disable-next-line arrow-parens
// blocks.top('sky-wars',).then((leader,) => {
// console.log(leader,);
// },);
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"strict": true,
"module": "commonjs",
"moduleResolution": "node",
"target": "es2017",
"target": "ES2020",
"lib": [
"esnext",
"esnext.array",
"esnext.asynciterable",
"esnext.intl",
"esnext.symbol"
],
"resolveJsonModule": true,
"declaration": true,
"sourceMap": false,
"inlineSources": true,
Expand Down
Loading