· 6 years ago · Apr 04, 2020, 08:12 PM
1/*
2package.json
3{
4 "name": "now-express",
5 "version": "1.0.0",
6 "description": "",
7 "main": "index.js",
8 "scripts": {
9 "start": "node index.js",
10 "dev": "nodemon index.js"
11 },
12 "keywords": [],
13 "author": "MaybePermanent",
14 "license": "MIT",
15 "dependencies": {
16 "express": "^4.17.1",
17 "nodemon": "^1.19.1",
18 "pretty-time": "^1.1.0",
19 "request": "^2.88.2"
20 }
21}
22*/
23
24const request = require('request');
25const prettyTime = require("pretty-time")
26const express = require("express");
27const app = express();
28
29// The port this instance will run on
30const port = 5000;
31
32// User ID and API Key
33const userId = "3oj8zcng6lbwmgd";
34const apiKey = "";
35
36const OneMinute = 60;
37const OneHour = 60 * 60;
38const OneDay = 60 * 60 * 24;
39const OneWeek = 60 * 60 * 24 * 7;
40
41const minRequirement = 1;
42const maxRequirement = 10;
43
44const rand = function (min, max) { // min and max included
45 return Math.floor(Math.random() * (max - min + 1) + min);
46};
47
48const getOptions = path => ({
49 url: `https://emlalock.com/${path}`,
50 json: true
51});
52
53const pretty = m => prettyTime([m, 0], 'm');
54
55const buildQueryPath = function (method) {
56
57 let setFrom = false;
58 let setTo = false;
59
60 let fromValue = 0;
61 let fromName = "";
62 let toValue = 0;
63
64 let message = "";
65
66 switch (method) {
67 case "info":
68 break;
69 case "add":
70 case "addminimum":
71 case "addmaximum":
72 setFrom = true;
73 fromName = "value";
74 fromValue = rand(OneHour, OneDay);
75
76 message = `Thank you for adding ${pretty(fromValue)} to my ${ method === "add" ? "duration" : method === "addmaximum" ? "maximum duration" : "minimum duration"}`;
77 break;
78 case "addrequirement":
79 setFrom = true;
80 fromName = "value";
81 fromValue = rand(minRequirement, maxRequirement);
82
83 message = `Thank you for giving me ${fromValue} extra ${fromValue === 1 ? "requirement" : "requirements"}`;
84 break;
85
86 case "addrandom":
87 case "addminimumrandom":
88 case "addmaximumrandom":
89 setFrom = true;
90 fromName = "from";
91 fromValue = rand(OneHour, OneDay);
92
93 setTo = true;
94 toValue = rand(OneDay, OneWeek);
95
96 message = `Thank you for adding somewhere between ${pretty(fromValue)} and ${pretty(toValue)} to my ${ method === "add" ? "duration" : method === "addmaximum" ? "maximum duration" : "minimum duration"}`;
97 break;
98 case "addrequirementrandom":
99 setFrom = true;
100 fromName = "from";
101 fromValue = minRequirement;
102
103 setTo = true;
104 toValue = maxRequirement;
105
106 message = `Thank you for giving me somwwhere between ${fromValue} and ${toValue} extra requirements`;
107 break;
108 default:
109 break;
110 }
111
112 let from = setFrom ? `&${fromName}=${fromValue}` : "";
113 let to = setTo ? `$to=${toValue}` : "";
114 return {
115 Url: `${method}?userid=${userId}&apikey=${apiKey}${from}${to}`,
116 Message: message
117 };
118
119}
120
121const run = function (res) {
122
123 let infoQuery = buildQueryPath("info");
124 request.get(getOptions(infoQuery.Url), (err, result, userInfo) => {
125
126 var allowedMethods = ["add", "addrandom"];
127
128 if (userInfo.chastitysession.minduration > 0) {
129 allowedMethods.push("addminimum");
130 allowedMethods.push("addminimumrandom");
131 }
132 if (userInfo.chastitysession.maxduration > 0) {
133 allowedMethods.push("addmaximum");
134 allowedMethods.push("addmaximumrandom");
135 }
136 if (userInfo.chastitysession.requirements > 0) {
137 allowedMethods.push("addrequirement");
138 allowedMethods.push("addrequirementrandom");
139 }
140
141 let selectedMethod = allowedMethods[rand(0, allowedMethods.length - 1)];
142
143 let queryProps = buildQueryPath(selectedMethod);
144 let query = getOptions(queryProps.Url);
145 request.get(query, function (x, y, z) { res.send(queryProps.Message); });
146
147 });
148
149}
150
151// Body parser
152app.use(express.urlencoded({ extended: false }));
153
154// Home route
155app.get("/", (req, res) => {
156 run(res);
157});
158
159// Listen on port 5000
160app.listen(port, () => {
161 console.log(`Running`);
162});