Explorar el Código

Replace request with got

ghorsington hace 4 años
padre
commit
1a8f9a9af3

+ 0 - 3
bot/package.json

@@ -32,7 +32,6 @@
       "@types/koa-router": "^7.4.1",
       "@types/lowdb": "^1.0.9",
       "@types/node-schedule": "^1.3.0",
-      "@types/request-promise-native": "^1.0.17",
       "@types/sha1": "^1.1.2",
       "@types/turndown": "^5.0.0",
       "@types/ws": "^7.2.4",
@@ -55,8 +54,6 @@
       "nodemailer": "^6.4.8",
       "pg": "^8.2.1",
       "reflect-metadata": "^0.1.13",
-      "request": "^2.88.2",
-      "request-promise-native": "^1.0.8",
       "rimraf": "^3.0.2",
       "rss-parser": "^3.8.0",
       "sha1": "^1.1.1",

+ 6 - 5
bot/src/commands/aggregators/com3d2_updates.ts

@@ -1,10 +1,9 @@
-import request from "request-promise-native";
-import { Response } from "request";
 import { IAggregator, INewsItem } from "./aggregator";
 import { getRepository } from "typeorm";
 import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
 import cheerio from "cheerio";
 import { logger } from "src/logging";
+import got from "got";
 
 const updatePage = "http://com3d2.jp/update/";
 const changeLogPattern = /\[\s*([^\s\]]+)\s*\]\s*((・.*)\s+)+/gim;
@@ -32,10 +31,12 @@ async function aggregate() {
         });
     
     try {
-        const mainPageRes = await request(updatePage, {resolveWithFullResponse: true}) as Response;
+        const mainPageRes = await got.get(updatePage);
 
-        if(mainPageRes.statusCode != 200)
-            return;
+        if(mainPageRes.statusCode != 200) {
+            logger.error("[COM3D2 JP UPDATE] Failed to load page. Got response code: %s", mainPageRes.statusCode);
+            return [];
+        }
         
         const rootNode = cheerio.load(mainPageRes.body);
 

+ 6 - 5
bot/src/commands/aggregators/com3d2_world.ts

@@ -1,10 +1,9 @@
-import request from "request-promise-native";
-import { Response } from "request";
 import { INewsItem, IAggregator } from "./aggregator";
 import { getRepository } from "typeorm";
 import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
 import cheerio from "cheerio";
 import { logger } from "src/logging";
+import got from "got";
 
 const kissDiaryRoot = "https://com3d2.world/r18/notices.php";
 const FEED_NAME = "com3d2-world-notices";
@@ -24,10 +23,12 @@ async function aggregate() {
         });
 
     try {
-        const mainPageRes = await request(kissDiaryRoot, {resolveWithFullResponse: true}) as Response;
+        const mainPageRes = await got.get(kissDiaryRoot);
         
-        if(mainPageRes.statusCode != 200)
+        if(mainPageRes.statusCode != 200) {
+            logger.error("[COM3D2 WORLD BLOG] Failed to load page. Got response code: %s", mainPageRes.statusCode);
             return [];
+        }
 
         const rootNode = cheerio.load(mainPageRes.body);
 
@@ -54,7 +55,7 @@ async function aggregate() {
                 latestEntry = id;
 
             const diaryLink = `${kissDiaryRoot}?no=${id}`;
-            const res = await request(diaryLink, {resolveWithFullResponse: true}) as Response;
+            const res = await got.get(diaryLink);
             if(res.statusCode != 200)
                 continue;
 

+ 5 - 4
bot/src/commands/aggregators/kiss_diary.ts

@@ -1,10 +1,9 @@
-import request from "request-promise-native";
-import { Response } from "request";
 import { INewsItem, IAggregator } from "./aggregator";
 import { getRepository } from "typeorm";
 import { AggroNewsItem } from "@shared/db/entity/AggroNewsItem";
 import cheerio from "cheerio";
 import { logger } from "src/logging";
+import got from "got";
 
 const urlPattern = /diary\.php\?no=(\d+)/i;
 const kissDiaryRoot = "http://www.kisskiss.tv/kiss";
@@ -25,10 +24,12 @@ async function aggregate() {
         });
     
     try {
-        const mainPageRes = await request(`${kissDiaryRoot}/diary.php`, {resolveWithFullResponse: true}) as Response;
+        const mainPageRes = await got.get(`${kissDiaryRoot}/diary.php`);
         
-        if(mainPageRes.statusCode != 200)
+        if(mainPageRes.statusCode != 200) {
+            logger.error("[KISS DIARY] Failed to load page. Got response code: %s", mainPageRes.statusCode);
             return [];
+        }
 
         const rootNode = cheerio.load(mainPageRes.body);
         const diaryEntryNames = rootNode("table.blog_frame_top");

+ 6 - 2
bot/src/commands/facemorph.ts

@@ -1,7 +1,6 @@
 import { isValidImage } from "../util";
 import Jimp from "jimp";
 import { client } from "../client";
-import request from "request-promise-native";
 import { Message, MessageAttachment } from "discord.js";
 import { getRepository } from "typeorm";
 import { FaceCaptionMessage, FaceCaptionType } from "@shared/db/entity/FaceCaptionMessage";
@@ -170,7 +169,12 @@ export class Facemorph {
     }
 
     async processFaceSwap(message: Message, attachmentUrl: string, processor?: ImageProcessor, failMessage?: string, successMessage?: string): Promise<void> {
-        const data = await request(attachmentUrl, { encoding: null }) as Buffer;
+        const dataResponse = await got.get(attachmentUrl, { responseType: "buffer" });
+        if (dataResponse.statusCode != 200) {
+            logger.error("Failed to get attachment %s. Got code %s", attachmentUrl, dataResponse.statusCode);
+            return;
+        }
+        const data = dataResponse.body;
 
         const form = new FormData();
         form.append("img_data", data, {

+ 2 - 2
bot/src/commands/inspire.ts

@@ -1,12 +1,12 @@
-import request from "request-promise-native";
 import { Message } from "discord.js";
 import { CommandSet, Command } from "src/model/command";
+import got from "got";
 
 @CommandSet
 export class Inspire {
 
     async doInspire(msg: Message): Promise<void> {
-        const result = await request("https://inspirobot.me/api?generate=true");
+        const result = await got.get("https://inspirobot.me/api?generate=true");
         msg.channel.send(`${msg.author.toString()} Here is a piece of my wisdom:`, {
             files: [ result ]
         });

+ 30 - 5
bot/src/commands/rcg.ts

@@ -1,25 +1,50 @@
-import request from "request-promise-native";
 import { Message } from "discord.js";
 import { Command, CommandSet } from "src/model/command";
+import got from "got";
+import { logger } from "src/logging";
 
 const rcgRe = /<input id="rcg_image".+value="([^"]+)".*\/>/i;
 
+interface XkcdResponse {
+    img: string;
+}
+
 @CommandSet
 export class Rcg {
+    async sendErrorMessage(msg: Message): Promise<void> {
+        const xkcdResult = await got.get<XkcdResponse>("https://xkcd.com/info.0.json", { responseType: "json" });
+        if(xkcdResult.statusCode != 200) {
+            await msg.channel.send(`${msg.author.toString()} Sorry, I couldn't get any comics :(.`);
+            return;
+        }
+        await msg.channel.send(`${msg.author.toString()} Sorry, I couldn't get a random comic! Here is today's XKCD instead:`, {
+            files: [ xkcdResult.body.img ]
+        });
+    }
+
     @Command({
         pattern: "random comic",
         auth: false,
         documentation: {description: "Generates a comic just for you!", example: "random comic"}
     })
     async randomComic(msg: Message): Promise<void> {
-        const result = await request("http://explosm.net/rcg/view/?promo=false");
+        const result = await got.get("http://explosm.net/rcg/view/?promo=false");
     
-        const regexResult = rcgRe.exec(result);
+        if (result.statusCode != 200) {
+            logger.error("Failed to get RCG. Got status: %s", result.statusCode);
+            await this.sendErrorMessage(msg);
+            return;
+        }
+
+        const regexResult = rcgRe.exec(result.body);
 
-        if(!regexResult)
+        if(!regexResult) {
+            logger.error("Could not find RCG from body. Got response body: %s", result.body);
+            await this.sendErrorMessage(msg);
             return;
+        }
 
-        msg.channel.send(`${msg.author.toString()} I find this very funny:`, {
+        await msg.channel.send(`${msg.author.toString()} I find this very funny:`, {
             files: [ regexResult[1].trim() ]
         });
     }

+ 8 - 9
bot/src/xenforo.ts

@@ -1,6 +1,5 @@
-import request from "request-promise-native";
-import { Response } from "request";
 import { Dict } from "./util";
+import got, { Method } from "got";
 
 enum ReqMethod {
     GET = "get",
@@ -21,20 +20,20 @@ export class XenforoClient {
     constructor(private endpoint: string, private userKey: string) {
     }
 
-    private async makeRequest<TResult, TData>(uri: string, method: ReqMethod, data?: TData): Promise<TResult> {
-        const result = await request(`${this.endpoint}/${uri}`, {
+    private async makeRequest<TResult, TData>(uri: string, method: Method, data?: TData): Promise<TResult> {
+        const result = await got<TResult | RequestErrorSet>(`${this.endpoint}/${uri}`, {
+            responseType: "json",
             method: method,
             headers: {
                 "XF-Api-Key": this.userKey
             },
-            form: data,
-            resolveWithFullResponse: true
-        }) as Response;
+            json: data
+        });
 
         if (result.statusCode != 200) {
-            throw await JSON.parse(result.body) as RequestErrorSet;
+            throw result.body as RequestErrorSet;
         } else {
-            return await JSON.parse(result.body) as TResult;
+            return result.body as TResult;
         }
     }