Skip to content
Permalink
Browse files

Major changes (#2)

Major re-factoring
  • Loading branch information
Nevexo authored and crock committed Jan 9, 2019
1 parent 86ca1a8 commit 5e84181a720510395d7dc3f83cf793c683896ae6
Showing with 128 additions and 103 deletions.
  1. +29 −0 APICHANGES.md
  2. +47 −40 app.js
  3. +9 −12 libs/Instagram.js
  4. +3 −7 libs/Minecraft.js
  5. +4 −8 libs/Mixer.js
  6. +4 −15 libs/Steam.js
  7. +17 −0 libs/SteamGroup.js
  8. +3 −7 libs/Twitch.js
  9. +3 −7 libs/Twitter.js
  10. +3 −7 libs/Youtube.js
  11. +6 −0 services.json
@@ -0,0 +1,29 @@
# Suggested API changes

This would break the API so these features will not be upstreamed.

## API: /check/services

Having an extra object is kind of redundent, it would make more sense to just send a JSON array.

For example:
`
[
"twitter",
"instagram",
"steamid",
"steamgroup",
"mixer",
"youtube"
]
`

DIFF:
`
- var simple = {"services": []};
+ var simple = [];
var advanced = require('./services.json');
for (var key in advanced.services) {
- simple.services.push(advanced.services[key].slug)
+ simple.push(advanced.services[key].slug)
`
87 app.js
@@ -7,17 +7,15 @@ const router = express.Router();

const bodyParser = require('body-parser');
const timeout = require('connect-timeout');
const axios = require('axios');
const apicache = require('apicache');
const redis = require('redis');
const fs = require('fs');
const path = require('path');
const fs = require("fs")

const CheckTwitter = require('./libs/Twitter');
const CheckInstagram = require('./libs/Instagram');
const CheckSteam = require('./libs/Steam');
const CheckYoutube = require('./libs/Youtube');
const CheckMixer = require('./libs/Mixer');
// Load services.json
const advanced = require('./services.json');

// Used for storing active modules
let modules = []

app.use(cors())
app.use(router);
@@ -27,9 +25,20 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(haltOnTimedout);
app.use(function (req, res, next) {
res.header('Content-Type', 'application/json');
res.header('x-powered-by', 'github.com/checker')
next();
});

const loadLibs = (callback) => {
// This isn't great, but it works.
advanced["services"].forEach(service => {
console.log(`[BOOTSTRAP] Bootstrapping ${service.name} (${service.slug})`)
modules[service.slug] = {"manifest": service, "instance": require(`./${service.libPath}`)}
})
console.log(`[BOOTSTRAP] Module start finished, loaded ${modules.length} modules.`)
callback()
}

function goToDocs(req, res) {
const targetUrl = "https://app.swaggerhub.com/apis-docs/CrocBuzz/penguin-api/";
res.redirect(targetUrl);
@@ -42,6 +51,7 @@ function checkAuthKey(req, res, next) {
if (keys.authorized.includes(appkey)) {
next()
} else {
res.status(401) // http: UNAUTHORIZED
res.json({"status": "unauthorized"});
}
}
@@ -52,65 +62,62 @@ let cacheWithRedis = apicache.options({ redisClient: redis.createClient(process.
router.get('/', goToDocs);

router.get('/check/services', function(req, res) {
// Note: see APICHANGES.md for possible changes
var simple = {"services":[]};
var advanced = require('./services.json');
for (var key in advanced.services) {
simple.services.push(advanced.services[key].slug)
}
res.type('json');
res.json(200, simple);
res.json(simple);
});

router.get('/check/:service', function(req, res) {
var service = req.params.service;
var advanced = require('./services.json');
var json = {};
for (var key in advanced.services) {
if (service === advanced.services[key].slug) {
json = advanced.services[key];
break;
}
}
res.type('json');
res.json(200, json);
res.json(json)
});

router.get('/check/services/details', function(req, res) {
var json = require('./services.json');
res.type('json');
res.json(200, json);
res.json(json)
});

router.get('/check/:service/:word', [cacheWithRedis('6 hours')], function(req, res) {
var service = req.params.service;
var word = req.params.word;

switch (service) {
case "twitter":
CheckTwitter(service, word, res);
break;
case "instagram":
CheckInstagram(service, word, res);
break;
case "steamid":
CheckSteam(service, word, res);
break;
case "steamgroup":
CheckSteam(service, word, res);
break;
case "youtube":
CheckYoutube(service, word, res);
break;
case "mixer":
CheckMixer(service, word, res);
break;
}
var service = req.params.service;
var word = req.params.word;
if (modules[service] == undefined) {
res.status(400)
res.json({"error": "INVALID_SERVICE", "message": "Invalid service selected, please see /check/services for a list."})
}else {
// Seemingly valid service, request data on 'word':
modules[service].instance(word, (status, timestamp, statusBreakdown) => {
// Result will be an array containing [status, epoch, extra information]
const json = {
"service": modules[service]["manifest"]["slug"], // Use the slug, not what the user sent
"username": word, // Return what the user sent, I'm lazy.
"status": status, // Return the status "available"/"taken"/"unknown"
"timestamp": timestamp, // Return an MS epoch time
"statusBreakdown": statusBreakdown || undefined // Return the breakdown (or undefined)
}
res.json(json)
})
}
});

function haltOnTimedout (req, res, next) {
if (!req.timedout) next()
}

app.listen(process.env.PORT || 5000);
loadLibs(() => {
// Start server when modules have loaded
app.listen(process.env.PORT || 5000, () => {
console.log(`[SERVER] Started listening on ${process.env.PORT || 5000}`)
});
})

module.exports = app;
@@ -1,11 +1,8 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const faker = require('faker');

function CheckInstagram(service, word, res) {
res.type('json');

const check = (word, callback) => {

var token = "";
var mid = "";
var cookie = "";
@@ -26,25 +23,25 @@ function CheckInstagram(service, word, res) {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
}
}

var payload = {
"first_name": faker.name.firstName(),
"username": word,
"email": faker.internet.email(),
"password": faker.internet.password(16)
}

axios.post('https://www.instagram.com/accounts/web_create_ajax/attempt/', payload, config)
.then(function (response) {
var milliseconds = new Date().getTime();
var status = (response.data.dryrun_passed) ? "available" : "taken";
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
}).catch(console.error);

}).catch(console.error);



}

module.exports = CheckInstagram;
module.exports = check;
@@ -1,11 +1,7 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const faker = require('faker');

function CheckMinecraft(service, word, res) {
res.type('json');

const check = (word, callback) => {
var url = `https://api.mojang.com/users/profiles/minecraft/${word}`;
var status = "";

@@ -18,10 +14,10 @@ function CheckMinecraft(service, word, res) {
} catch(err) {
status = "available";
}
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
})
.catch(console.error)

}

module.exports = CheckMinecraft;
module.exports = check;
@@ -1,22 +1,18 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');

function CheckMixer(service, word, res) {
res.type('json');

const check = (word, callback) => {
const url = `https://mixer.com/api/v1/channels/${word}`;

var status = "";

axios.get(url).then(function (obj) {
var milliseconds = new Date().getTime();
status = ('statusCode' in obj.data) ? "available" : "taken";
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
}).catch(function () {
var milliseconds = new Date().getTime();
res.json({ service: service, username: word, status: "available", timestamp: milliseconds });
callback("available", milliseconds)
});
}

module.exports = CheckMixer;
module.exports = check;
@@ -1,28 +1,17 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const cheerio = require('cheerio');

function CheckSteam(service, word, res) {
res.type('json');
var url = "";

if (service == "steamid") {
url = `https://steamcommunity.com/id/${word}`;
}

if (service == "steamgroup") {
url = `https://steamcommunity.com/groups/${word}`;
}
const check = (word, callback) => {
var url = `https://steamcommunity.com/id/${word}`;

axios.get(url).then(function (response) {
var $ = cheerio.load(response.data);
var elem = $('body').find('h3').length;
var milliseconds = new Date().getTime();
var status = (elem == 1) ? "available" : "taken";
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
}).catch(console.error);

}

module.exports = CheckSteam;
module.exports = check;
@@ -0,0 +1,17 @@
const axios = require('axios');
const cheerio = require('cheerio');

const check = (word, callback) => {
var url = `https://steamcommunity.com/groups/${word}`;

axios.get(url).then(function (response) {
var $ = cheerio.load(response.data);
var elem = $('body').find('h3').length;
var milliseconds = new Date().getTime();
var status = (elem == 1) ? "available" : "taken";
callback(status, milliseconds)
}).catch(console.error);

}

module.exports = check;
@@ -1,10 +1,6 @@
const express = require('express');
const router = express.Router();
const twitch = require('twitch-api-v5');

function CheckTwitch(service, word, res) {
res.type('json');

const check = (word, callback) => {
twitch.clientID = process.env.TWITCH_CLIENT_ID;

twitch.users.usersByName({"users": [word]}, function(error, response) {
@@ -13,9 +9,9 @@ function CheckTwitch(service, word, res) {
} else {
var status = (response['_total'] == 1) ? "taken" : "available";
var milliseconds = new Date().getTime();
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
}
});
}

module.exports = CheckTwitch;
module.exports = check;
@@ -1,10 +1,6 @@
const express = require('express');
const router = express.Router();
const axios = require('axios');
const Twitter = require('twitter-lite');

function CheckTwitter(service, word, res) {
res.type('json');
const check = (word, callback) => {
var config = {
"consumer_key": process.env.TWTR_CONSUMER_KEY,
"consumer_secret": process.env.TWTR_CONSUMER_SECRET,
@@ -17,10 +13,10 @@ function CheckTwitter(service, word, res) {
.then((r) => {
var milliseconds = new Date().getTime();
var status = ('screen_name' in r) ? "taken" : "available";
res.json({ service: service, username: word, status: status, timestamp: milliseconds });
callback(status, milliseconds)
})
.catch(console.error)

}

module.exports = CheckTwitter;
module.exports = check;

0 comments on commit 5e84181

Please sign in to comment.
You can’t perform that action at this time.