Skip to content

Commit

Permalink
upload sources
Browse files Browse the repository at this point in the history
  • Loading branch information
khmyznikov committed Jan 22, 2023
1 parent acd6f2b commit 5291d77
Show file tree
Hide file tree
Showing 17 changed files with 5,067 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typings/

# Nuxt.js build / generate output
.nuxt
dist
# dist

# Gatsby files
.cache/
Expand Down
100 changes: 100 additions & 0 deletions dist/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import express from 'express';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
import { askGPT, initGPT } from './gpt.js';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
let GPTSesion = null;
app.get('/initAPI', async (req, res) => {
GPTSesion = await initGPT();
if (GPTSesion) {
res.status(200).send({ message: 'GPT initialized' });
}
else {
GPTSesion = null;
res.status(500).send({ error: 'GPT unavailable' });
}
});
app.get('/generateManifest', async (req, res) => {
if (!req.query.url) {
res.status(400).send({ error: 'URL not specified' });
return;
}
const request = await fetch(req.query.url.toString());
let rawHTML = await request.text();
let headerHTML;
if (typeof rawHTML === 'string') {
rawHTML = rawHTML.replace(/\r|\n/g, '').replace(/\s{2,}/g, '');
headerHTML = /<head>(.*)<\/head>/.test(rawHTML) ? rawHTML.match(/<head>(.*)<\/head>/)[0] : 'Head not found';
}
if (!GPTSesion) {
res.status(500).send({ error: 'API not initialized' });
return;
}
if (headerHTML) {
const manifest = await askGPT(headerHTML, GPTSesion);
if (manifest) {
res.status(200).send({ manifest });
}
else
res.status(400).send({ error: 'GPT unavailable or failed to generate manifest' });
}
else {
res.status(400).send({ error: '<head> HTML not found' });
}
});
app.post('/generateWinPackage', async (req, res) => {
if (!req.body?.manifest) {
res.status(400).send({ error: 'Manifest not specified' });
return;
}
if (!req.query?.url) {
res.status(400).send({ error: 'URL not specified' });
return;
}
let manifest = null;
if (typeof req.body.manifest == 'object') {
manifest = req.body.manifest;
}
else {
res.status(400).send({ error: 'Manifest is not a valid JSON' });
return;
}
if (manifest?.icons?.length < 1) {
res.status(400).send({ error: 'Manifest has no icons' });
return;
}
// {"name":"Microsoft Apps","short_name":"Microsoft Apps","start_url":"/","display":"standalone","theme_color":"#ffffff","background_color":"#ffffff","description":"Make Microsoft Windows your own with apps and themes that help you personalise Windows and be more productive.","icons":[{"src":"/store/images/logo-16x16.png?v=lOdDASudWX5I0YkpCu1DicAbwXJ87mUk-1A_lczTIEc","sizes":"16x16","type":"image/png"},{"src":"/store/images/logo-32x32.png?v=gBZzyJYt3-JPdgfYPGGoJ0bEEL2ozU5k4XVSpXSC3Ts","sizes":"32x32","type":"image/png"},{"src":"/store/images/logo-64x64.png?v=goKXuTDLKbFD3lo_TP4Vtbi7zdyQKKDs7GzLl-kv7K4","sizes":"64x64","type":"image/png"},{"src":"/store/images/logo-128x128.png?v=Gw_i5pK1zHf0Cp_XZ8FyU3fYU3JgX9W0x8aZU0-L5j4","sizes":"128x128","type":"image/png"},{"src":"/store/images/logo-256x256.png?v=vIx_WpJnNgKj-L5o_v5S5MjK5S5Jm7z9aF5xV1-L5j4","sizes":"256x256","type":"image/png"},{"src":"/store/images/logo-512x512.png?v=YsT9zC0xhEZ3q3Zb1OJh1fKj-L5o_v5S5MjK5S5Jm7z9","sizes":"512x512","type":"image/png"}]}
// "backgroundColor":"#FFFFFF",
let payload = {
"name": (manifest.name || manifest.short_name || "Web Application"),
"packageId": "PWA.ChatGPT.PoC",
"url": req.query.url,
"version": "1.0.1",
"allowSigning": true,
"publisher": { "displayName": "PWABuilder", "commonName": "CN=3a54a224-05dd-42aa-85bd-3f3c1478fdca" },
"generateModernPackage": true,
"classicPackage": { "generate": false },
"edgeHtmlPackage": { "generate": false },
"manifest": manifest,
"images": { "baseImage": manifest.icons[manifest.icons.length - 1].src, "padding": 0 },
"resourceLanguage": "en", "targetDeviceFamilies": ["Desktop"]
};
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
};
const request = await fetch("https://pwabuilder-winserver.centralus.cloudapp.azure.com/msix/generatezip", options);
// res.setHeader('Content-disposition', `attachment; filename=${payload.name.trim()}.zip`);
res.attachment(`${payload.name.trim()}.zip`);
request.body?.pipe(res);
});
app.use('/', express.static('dist/ui'));
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});
29 changes: 29 additions & 0 deletions dist/gpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ChatGPTAPIBrowser } from 'chatgpt';
export async function initGPT() {
const api = new ChatGPTAPIBrowser({
email: '[email protected]',
password: 'u!qJ8P-M-uQzjXB',
});
try {
await api.initSession();
}
catch (error) {
await api.closeSession();
return null;
}
return api;
}
export async function askGPT(html, session) {
let gptAnswer = null;
try {
gptAnswer = await session.sendMessage(`parse this html and generate web manifest in JSON format, silent answer with no comments or explains. Take all information and icons from html tags, use only one biggest icon in array of icons. ${html}`);
}
catch (error) { }
let manifest = null;
if (gptAnswer)
try {
manifest = JSON.parse(gptAnswer.response);
}
catch (error) { }
return manifest;
}
Binary file added dist/ui/header_logo-fda684c8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions dist/ui/index-51e5d569.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5291d77

Please sign in to comment.