Skip to content

Commit

Permalink
Fix url 404
Browse files Browse the repository at this point in the history
  • Loading branch information
harryitz committed Jul 10, 2024
1 parent 3d613a6 commit cfc7eb7
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 49 deletions.
2 changes: 1 addition & 1 deletion dist/EmojiKitchen.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { EmojiKitchenResult } from "./EmojiKitchenResult";
export declare function mergeEmoji(emoji_1: string, emoji_2: string): EmojiKitchenResult | undefined;
export declare function mergeEmoji(emoji_1: string, emoji_2: string): Promise<EmojiKitchenResult | undefined>;
88 changes: 69 additions & 19 deletions dist/EmojiKitchen.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,71 @@
import path from "path";
import { EmojiKitchenResult } from "./EmojiKitchenResult";
import { emojiToUnicode } from "./utils/EmojiUtils";
import * as fs from "fs";
export function mergeEmoji(emoji_1, emoji_2) {
const emoji_1_unicode = emojiToUnicode(emoji_1).toLowerCase();
const emoji_2_unicode = emojiToUnicode(emoji_2).toLowerCase();
if (!emoji_1_unicode || !emoji_2_unicode) {
return undefined;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
const dataPath = path.resolve(__dirname, 'resources', 'supported_emojis.json');
const supported_emojis = JSON.parse(fs.readFileSync(dataPath, 'utf-8'));
const emoji_1_finder = supported_emojis.find(emoji => emoji.emojiUnicode == emoji_1_unicode);
const emoji_2_finder = supported_emojis.find(emoji => emoji.emojiUnicode == emoji_2_unicode);
if (!emoji_1_finder || !emoji_2_finder) {
return undefined;
}
const created_date = emoji_1_finder.date;
const url = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_1_unicode}/${emoji_1_unicode}_${emoji_2_unicode}.png`;
return new EmojiKitchenResult(url);
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeEmoji = mergeEmoji;
const path_1 = __importDefault(require("path"));
const EmojiKitchenResult_1 = require("./EmojiKitchenResult");
const EmojiUtils_1 = require("./utils/EmojiUtils");
const fs = __importStar(require("fs"));
function mergeEmoji(emoji_1, emoji_2) {
return __awaiter(this, void 0, void 0, function* () {
const emoji_1_unicode = (0, EmojiUtils_1.emojiToUnicode)(emoji_1).toLowerCase();
const emoji_2_unicode = (0, EmojiUtils_1.emojiToUnicode)(emoji_2).toLowerCase();
if (!emoji_1_unicode || !emoji_2_unicode) {
return undefined;
}
const dataPath = path_1.default.resolve(__dirname, 'resources', 'supported_emojis.json');
const supported_emojis = JSON.parse(fs.readFileSync(dataPath, 'utf-8'));
const emoji_1_finder = supported_emojis.find(emoji => emoji.emojiUnicode == emoji_1_unicode);
const emoji_2_finder = supported_emojis.find(emoji => emoji.emojiUnicode == emoji_2_unicode);
if (!emoji_1_finder || !emoji_2_finder) {
return undefined;
}
const created_date = emoji_1_finder.date;
const url_1 = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_1_unicode}/${emoji_1_unicode}_${emoji_2_unicode}.png`;
const url_2 = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_2_unicode}/${emoji_2_unicode}_${emoji_1_unicode}.png`;
const [res_1, res_2] = yield Promise.all([
fetch(url_1),
fetch(url_2)
]);
const res_1_status = res_1.status;
const res_2_status = res_2.status;
if (res_1_status == 404 || res_2_status == 404)
return undefined;
const url = (res_1_status == 404) ? url_2 : url_1;
return new EmojiKitchenResult_1.EmojiKitchenResult(url);
});
}
85 changes: 62 additions & 23 deletions dist/EmojiKitchenResult.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,72 @@
import * as fs from "fs";
export class EmojiKitchenResult {
url;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmojiKitchenResult = void 0;
const fs = __importStar(require("fs"));
class EmojiKitchenResult {
constructor(url) {
this.url = url;
}
getURL() {
return this.url;
}
async toBuffer() {
try {
const response = await fetch(this.getURL());
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
toBuffer() {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield fetch(this.getURL());
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const buffer = Buffer.from(yield response.arrayBuffer());
return buffer;
}
catch (error) {
console.error('Error fetching image:', error);
throw error;
}
const buffer = Buffer.from(await response.arrayBuffer());
return buffer;
}
catch (error) {
console.error('Error fetching image:', error);
throw error;
}
});
}
async saveImageToFile(filePath) {
try {
const imageBuffer = await this.toBuffer();
fs.writeFileSync(filePath, imageBuffer);
}
catch (error) {
console.error('Error saving image:', error);
}
saveImageToFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const imageBuffer = yield this.toBuffer();
fs.writeFileSync(filePath, imageBuffer);
}
catch (error) {
console.error('Error saving image:', error);
}
});
}
}
exports.EmojiKitchenResult = EmojiKitchenResult;
20 changes: 18 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
export * from "./EmojiKitchen";
export * from "./EmojiKitchenResult";
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./EmojiKitchen"), exports);
__exportStar(require("./EmojiKitchenResult"), exports);
5 changes: 4 additions & 1 deletion dist/utils/EmojiUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export function emojiToUnicode(emoji) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.emojiToUnicode = emojiToUnicode;
function emojiToUnicode(emoji) {
const codePoints = [];
for (const char of emoji) {
codePoints.push('u' + char.codePointAt(0).toString(16).toUpperCase());
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@aurateam/emojikitchen",
"version": "1.0.1",
"version": "1.0.2",
"description": "Library to mix emojis use api https://emojikitchen.dev for NodeJS",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand Down
19 changes: 17 additions & 2 deletions src/EmojiKitchen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Emoji {
emojiUnicode: string;
}

export function mergeEmoji(emoji_1: string, emoji_2: string): EmojiKitchenResult | undefined {
export async function mergeEmoji(emoji_1: string, emoji_2: string): Promise<EmojiKitchenResult | undefined> {

const emoji_1_unicode = emojiToUnicode(emoji_1).toLowerCase()
const emoji_2_unicode = emojiToUnicode(emoji_2).toLowerCase()
Expand All @@ -27,6 +27,21 @@ export function mergeEmoji(emoji_1: string, emoji_2: string): EmojiKitchenResult
}

const created_date = emoji_1_finder.date
const url = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_1_unicode}/${emoji_1_unicode}_${emoji_2_unicode}.png`
const url_1 = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_1_unicode}/${emoji_1_unicode}_${emoji_2_unicode}.png`
const url_2 = `https://www.gstatic.com/android/keyboard/emojikitchen/${created_date}/${emoji_2_unicode}/${emoji_2_unicode}_${emoji_1_unicode}.png`
const [
res_1,
res_2
] = await Promise.all([
fetch(url_1),
fetch(url_2)
])

const res_1_status = res_1.status
const res_2_status = res_2.status
if (res_1_status == 404 || res_2_status == 404) return undefined;

const url = (res_1_status == 404) ? url_2 : url_1;

return new EmojiKitchenResult(url);
}

0 comments on commit cfc7eb7

Please sign in to comment.