Skip to content

Commit

Permalink
Merge pull request #30 from maormagori/style/add-linting
Browse files Browse the repository at this point in the history
Style: add linting rules and workflows
  • Loading branch information
maormagori committed May 20, 2024
2 parents b169f94 + 17520d3 commit ff25976
Show file tree
Hide file tree
Showing 11 changed files with 3,171 additions and 164 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
.idea/
.vscode/
.github/
35 changes: 35 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
parserOptions: {
ecmaVersion: 2020,
},
plugins: ['eslint-plugin-prettier', 'unicorn'],
env: {
node: true,
es6: true,
},
rules: {
'prefer-const': 2,
'no-var': 2,
'no-unused-vars': 'error',
quotes: [
'error',
'single',
{ avoidEscape: true, allowTemplateLiterals: true },
],
'class-methods-use-this': 0,
'object-curly-spacing': [2, 'always'],
semi: 2,
'no-restricted-syntax': 0,
'space-before-function-paren': 0,
'quote-props': ['error', 'as-needed'],
'require-atomic-updates': 0,
'one-var': ['error', 'never'],
'padding-line-between-statements': [
'error',
{ blankLine: 'always', prev: ['if', 'while', 'for'], next: '*' },
{ blankLine: 'always', prev: '*', next: 'function' },
],
'unicorn/prefer-date-now': 'error',
},
};
23 changes: 23 additions & 0 deletions .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# A github workflow that make sure the PR passes linting

name: PR Validation

on:
pull_request:
branches:
- main

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '20.9.0'
- name: Install dependencies
run: npm install
- name: Lint
run: npm run lint
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
Expand Down
30 changes: 15 additions & 15 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@
* Contains all the info the addon needs about server addresses and ports.
*/

let env = process.env.DEPLOYMENT ?? "local";
const env = process.env.DEPLOYMENT ?? 'local';
const config = {
wizdom_host: "wizdom.xyz",
wizdom_url: "https://wizdom.xyz/",
wizdom_api: "https://wizdom.xyz/api/",
wizdom_title_info: "https://wizdom.xyz/api/releases/",
wizdom_sub_download_url: "https://wizdom.xyz/api/files/sub/",
stremio_server_subtitle_url: "http://127.0.0.1:11470/subtitles.vtt",
srt_unzipper_path: "/srt/",
wizdom_host: 'wizdom.xyz',
wizdom_url: 'https://wizdom.xyz/',
wizdom_api: 'https://wizdom.xyz/api/',
wizdom_title_info: 'https://wizdom.xyz/api/releases/',
wizdom_sub_download_url: 'https://wizdom.xyz/api/files/sub/',
stremio_server_subtitle_url: 'http://127.0.0.1:11470/subtitles.vtt',
srt_unzipper_path: '/srt/',
remoteLogging: process.env.LOGGER ?? false,
};

//Public server build.
if (env === "beamup") {
if (env === 'beamup') {
config.port = process.env.PORT;
config.local = "https://4b139a4b7f94-wizdom-stremio-v2.baby-beamup.club";
config.local = 'https://4b139a4b7f94-wizdom-stremio-v2.baby-beamup.club';
}
//Locally running
else {
config.port = 7000;
config.local = "http://127.0.0.1:" + config.port;
config.local = 'http://127.0.0.1:' + config.port;
}

//Prod logs are sent to log server
config.logger = {
token: process.env.HEC_TOKEN ?? "",
host: process.env.INDEX_HOST ?? "",
path: "/splunk",
protocol: "http",
token: process.env.HEC_TOKEN ?? '',
host: process.env.INDEX_HOST ?? '',
path: '/splunk',
protocol: 'http',
port: 80,
batchInterval: 5000,
maxBatchCount: 10,
Expand Down
68 changes: 34 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const express = require("express"),
config = require("./config"),
cors = require("cors"),
{ getSubs, downloadSubZip } = require("./wizdom"),
landing = require("./landingTemplate"),
unzipper = require("unzipper"),
morganBody = require("morgan-body"),
logger = require("./logger"),
requestId = require("express-request-id");
const express = require('express');
const config = require('./config');
const cors = require('cors');
const { getSubs, downloadSubZip } = require('./wizdom');
const landing = require('./landingTemplate');
const unzipper = require('unzipper');
const morganBody = require('morgan-body');
const logger = require('./logger');
const requestId = require('express-request-id');

const addon = express();
addon.use(cors());
Expand All @@ -15,32 +15,32 @@ addon.use(requestId({ setHeader: false }));
// Setting up the logger
morganBody(addon, {
prettify: false,
timezone: "Israel",
timezone: 'Israel',
logRequestBody: false,
stream: config.remoteLogging ? logger : null,
noColors: true,
includeNewLine: false,
logIP: true,
logRequestId: true,
skip: (req, res) => {
return !req.path.includes("subtitles");
skip: (req) => {
return !req.path.includes('subtitles');
},
});

/**
* The addon manifest: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/responses/manifest.md
*/
const manifest = {
id: "xyz.stremio.wizdom",
contactEmail: "maor@magori.online",
id: 'xyz.stremio.wizdom',
contactEmail: 'maor@magori.online',
version: process.env.npm_package_version,
catalogs: [],
resources: ["subtitles"],
types: ["movie", "series"],
name: "Wizdom Subtitles",
resources: ['subtitles'],
types: ['movie', 'series'],
name: 'Wizdom Subtitles',
description:
"An unofficial Stremio addon for Hebrew subtitles from wizdom.xyz. Developed by Maor Development",
logo: "https://i.ibb.co/KLYK0TH/wizdon256.png",
'An unofficial Stremio addon for Hebrew subtitles from wizdom.xyz. Developed by Maor Development',
logo: 'https://i.ibb.co/KLYK0TH/wizdon256.png',
};

/**
Expand All @@ -49,52 +49,52 @@ const manifest = {
* @param {*} data The data to respond with.
*/
const respondWithHeaders = function (res, data) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
res.setHeader("Content-Type", "application/json");
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Content-Type', 'application/json');
res.send(data);
};

//manifest request.
addon.get("/manifest.json", function (req, res) {
addon.get('/manifest.json', function (req, res) {
respondWithHeaders(res, manifest);
});

//Landing page request.
addon.get("/", function (req, res) {
res.set("Content-Type", "text/html");
addon.get('/', function (req, res) {
res.set('Content-Type', 'text/html');
res.send(landing(manifest));
});

//Addon's readme request
addon.get("/README.md", (req, res) => {
addon.get('/README.md', (req, res) => {
res.sendFile(`${__dirname}/README.md`);
});

//Subtitles request.
addon.get("/subtitles/:type/:imdbId/:query?.json", async (req, res) => {
addon.get('/subtitles/:type/:imdbId/:query?.json', async (req, res) => {
try {
let filename;
if (req.params.query) filename = req.params.query.split("=").pop();
if (req.params.query) filename = req.params.query.split('=').pop();

const subtitles = await getSubs(req.params.imdbId, filename);
respondWithHeaders(res, { subtitles: subtitles });
} catch (err) {
console.error("get subs error: ", err);
console.error('get subs error: ', err);
}
});

/**
* unzips Wizdom zip files and send the srt file in it.
*/
addon.get("/srt/:id.srt", (req, res) => {
addon.get('/srt/:id.srt', (req, res) => {
try {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
res.setHeader("Content-Type", "application/octet-stream; charset=utf-8");
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Content-Type', 'application/octet-stream; charset=utf-8');
downloadSubZip(req.params.id).pipe(unzipper.ParseOne()).pipe(res);
} catch (err) {
console.error("error occurred while sending unzipped srt file: ", err);
console.error('error occurred while sending unzipped srt file: ', err);
}
});

Expand Down
27 changes: 15 additions & 12 deletions landingTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,24 @@ button:active {
.separator {
margin-bottom: 4vh;
}
`
`;

function landingTemplate(manifest) {
const background = manifest.background || 'https://dl.strem.io/addon-background.jpg'
const logo = manifest.logo || 'https://dl.strem.io/addon-logo.png'
const contactHTML = manifest.contactEmail ?
`<div class="contact">
const background =
manifest.background || 'https://dl.strem.io/addon-background.jpg';
const logo = manifest.logo || 'https://dl.strem.io/addon-logo.png';
const contactHTML = manifest.contactEmail
? `<div class="contact">
<p>Contact ${manifest.name} addon creator:</p>
<a href="mailto:${manifest.contactEmail}">${manifest.contactEmail}</a>
</div>` : ''
</div>`
: '';

const stylizedTypes = manifest.types
.map(t => t[0].toUpperCase() + t.slice(1) + (t !== 'series' ? 's' : ''))
const stylizedTypes = manifest.types.map(
(t) => t[0].toUpperCase() + t.slice(1) + (t !== 'series' ? 's' : '')
);

return `
return `
<!DOCTYPE html>
<html style="background-image: url(${background})">
Expand Down Expand Up @@ -253,7 +256,7 @@ function landingTemplate(manifest) {
<h3 class="gives">This addon has more :</h3>
<ul>
${stylizedTypes.map(t => `<li>${t}</li>`).join('')}
${stylizedTypes.map((t) => `<li>${t}</li>`).join('')}
</ul>
<div class="separator"></div>
Expand All @@ -278,7 +281,7 @@ function landingTemplate(manifest) {
</body>
</html>`
</html>`;
}

module.exports = landingTemplate
module.exports = landingTemplate;
12 changes: 6 additions & 6 deletions logger.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const config = require("./config");
const config = require('./config');

const splunkLogger = require("splunk-logging").Logger;
const splunkLogger = require('splunk-logging').Logger;

const logger = new splunkLogger(config.logger);
logger.error = (err, ctx) => {
console.error("Error occurred while sending data to splunk", err);
logger.error = (err) => {
console.error('Error occurred while sending data to splunk', err);
};

const streamLogs = {
write: (msg) => {
let requestId = msg.match(/(?<=\[)[a-z0-9|-]*?(?=\])/gm)[0];
let payload = msg.replace(`[${requestId}]`, "");
const requestId = msg.match(/(?<=\[)[a-z0-9|-]*?(?=\])/gm)[0];
const payload = msg.replace(`[${requestId}]`, '');

logger.send({
message: {
Expand Down
Loading

0 comments on commit ff25976

Please sign in to comment.