Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removing MOTM workflow and adding Mod Spotlights instead #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/add-mod-spotlights-video.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Add New Mod Spotlights Video

on:
workflow_dispatch:
inputs:
link:
description: 'YouTube Video URL'
default: ''
required: true
type: string

jobs:
Add-Mod-Spotlights-Video:
runs-on: ubuntu-latest

env:
EXT_LINK: ${{ inputs.link }}

steps:
- name: Show Inputs
run: echo "${{ toJSON(github.event.inputs) }}"

- uses: actions/checkout@v4
with:
submodules: "recursive"

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '18.x'
cache: "yarn"

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run
run: yarn add-mod-spotlights-video

# commit and push if not dry run
- name: Git commit and push
env:
CI_COMMIT_AUTHOR: Vortex Backend
CI_COMMIT_EMAIL: [email protected]
CI_COMMIT_MESSAGE: Update Mod Spotlights Videos
run: |
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}"
git config --global user.email "${{ env.CI_COMMIT_EMAIL }}"
git pull
git commit -a -m "${{ env.CI_COMMIT_MESSAGE }}"
git push
92 changes: 0 additions & 92 deletions .github/workflows/add-mods-of-the-month.yml

This file was deleted.

1 change: 1 addition & 0 deletions out/modspotlights.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"dev": "ts-node-dev --exit-child src/index.ts",
"update-extensions-manifest": "ts-node src/update-extensions-manifest.ts",
"add-extension": "ts-node src/add-extension.ts",
"add-mods-of-month": "ts-node src/add-mods-of-month.ts",
"add-mod-spotlights-video": "ts-node src/add-mod-spotlights-video.ts",
"test-slack": "ts-node src/test-slack.ts",
"test-github": "ts-node src/test-github.ts",
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
125 changes: 125 additions & 0 deletions src/add-mod-spotlights-video.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { IMOTMEntry, ModSpotlightEntry, VideoEntryType } from './types';
import * as fs from 'fs-extra';
import * as nanoid from 'nanoid';
import * as path from 'path';
import 'dotenv/config';
import { getFormattedDate } from './utils';

/**
* History lesson: this workflow was originally written to add the MOTM videos to the below JSON file.
* Even though we no longer add any entries to it, the file MUST NOT BE REMOVED to maintain integrity
* for users that are using Vortex 1.11.x - 1.12.x.
*
* And yes I left this stale code here just to mention that.
*/
const DEPRECATED_MOTM_FILENAME = 'modsofthemonth.json';
const DEPRECATED_MONTH_DICT: { [key: string]: number } = {
'January': 0,
'February': 1,
'March': 2,
'April': 3,
'May': 4,
'June': 5,
'July': 6,
'August': 7,
'September': 8,
'October': 9,
'November': 10,
'December': 11
};

const MOD_SPOTLIGHTS_FILENAME = 'modspotlights.json';

const REPO_ROOT_PATH: string = path.join(__dirname, '/../');

const OUT_PATH: string = path.join(REPO_ROOT_PATH, 'out');
const ARCHIVE_PATH: string = path.join(REPO_ROOT_PATH, 'archive');

// env variables
const EXT_LINK = process.env.EXT_LINK || '';

async function start() {

console.log('Start Program');

[EXT_LINK].forEach((envVar) => {
if (envVar === '') {
console.error(`No ${envVar} found in env`);
process.exit(1);
}
});

const driver = new Driver();
await driver.process();
}

class Driver {
private mEntries: IMOTMEntry[];

constructor() {
this.mEntries = [];
}

/**
*
* @deprecated Use readSpotlightsFile()
*/
private async readMOTMFile(): Promise<IMOTMEntry[]> {
return JSON.parse(await fs.readFile(path.join(OUT_PATH, DEPRECATED_MOTM_FILENAME), { encoding: 'utf8' }));
}

private async readSpotlightsFile(): Promise<ModSpotlightEntry[]> {
return JSON.parse(await fs.readFile(path.join(OUT_PATH, MOD_SPOTLIGHTS_FILENAME), { encoding: 'utf8' }));
}

private async readAll(): Promise<ModSpotlightEntry[]> {
return Promise.all([await this.readMOTMFile(), await this.readSpotlightsFile()].flat());
}

private extractVideoIdFromYouTubeUrl(url: string): string {
let videoId: string;
if (url.includes("watch?v=")) {
videoId = url.split("watch?v=")[1].split("&")[0];
} else {
throw new Error('Invalid URL');
}
return videoId;
}

private async writeToFile(data: any, type: VideoEntryType) {
// create folder just in case doesn't exist
if (!fs.existsSync(ARCHIVE_PATH)) {
fs.mkdirSync(ARCHIVE_PATH, { recursive: true });
}

// This should always resolve to modspotlights.json
const fileName = (type === 'modsofthemonth') ? DEPRECATED_MOTM_FILENAME : MOD_SPOTLIGHTS_FILENAME;

// write an archive file too?
await fs.writeFile(path.join(ARCHIVE_PATH, `${getFormattedDate(new Date())}_${fileName}`), JSON.stringify(data, undefined, 2), 'utf-8');

// write the main file
await fs.writeFile(path.join(OUT_PATH, fileName), JSON.stringify(data, undefined, 2), 'utf-8');
}

public async process() {
console.log('main env variables', { EXT_LINK });
console.log('Processing Mod Spotlights...');
this.mEntries = await this.readSpotlightsFile();
const timestampMS = Date.now();
const newEntry: ModSpotlightEntry = {
date: timestampMS,
id: nanoid.nanoid(),
videoid: this.extractVideoIdFromYouTubeUrl(EXT_LINK)
}
const existingEntry = this.mEntries.find(e => e.videoid === newEntry.videoid);
if (existingEntry) {
console.log('Entry already exists, rejecting...');
throw new Error('Entry already exists');
}
this.mEntries.push(newEntry);
await this.writeToFile(this.mEntries, 'modspotlights');
}
}

start();
106 changes: 0 additions & 106 deletions src/add-mods-of-month.ts

This file was deleted.

Loading