Skip to content

Commit

Permalink
Update .gitignore and pyproject.toml, add package.json, update prisma…
Browse files Browse the repository at this point in the history
…/schema.prisma, and modify .github/workflows/main.yml and update_nodes.py
  • Loading branch information
appujet committed Apr 24, 2024
1 parent 0f42e89 commit a1ac5d3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 93 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/main .yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Install Node.js and Yarn
uses: actions/setup-node@v2
with:
node-version: '18'
registry-url: 'https://registry.yarnpkg.com/'

- name: Install Dependencies
run: |
poetry install
run: yarn install

- name: Generate Prisma Client
run: |
poetry run prisma generate
run: npx prisma generate

- name: Run Python Script
- name: Run TypeScript Script
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
run: |
poetry run python update_nodes.py
TURSO_DATABASE_URL: ${{ secrets.TURSO_DATABASE_URL }}
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_AUTH_TOKEN }}
run: yarn start

- name: Commit Changes
run: |
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ node_modules
# Keep environment variables out of version control
.env

# Ignore poetry lock file
poetry.lock
pnpm-lock.yaml
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "lavalink-list",
"version": "1.0.0",
"description": "A list of free and available public Lavalink nodes with their live status. Feel free to make a pull request!",
"main": "update_nodes.js",
"scripts": {
"start": "ts-node update_nodes.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@libsql/client": "^0.6.0",
"@prisma/adapter-libsql": "^5.13.0",
"@prisma/client": "^5.13.0"
},
"devDependencies": {
"@types/node": "^20.12.7",
"prisma": "^5.13.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
}
}
5 changes: 3 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-py"
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
url = "file:./dev.db"
}

model Node {
Expand Down
16 changes: 0 additions & 16 deletions pyproject.toml

This file was deleted.

62 changes: 0 additions & 62 deletions update_nodes.py

This file was deleted.

87 changes: 87 additions & 0 deletions update_nodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as fs from 'fs';
import { PrismaClient } from '@prisma/client';
import { PrismaLibSQL } from "@prisma/adapter-libsql";
import { createClient } from "@libsql/client";

const libsql = createClient({
url: process.env.TURSO_DATABASE_URL || '',
authToken: process.env.TURSO_AUTH_TOKEN,
});

const adapter = new PrismaLibSQL(libsql);

// Define types for node objects
interface Node {
host: string;
identifier: string;
password?: string;
port?: number;
restVersion?: string;
secure?: boolean;
authorId?: string;
}

// Function to remove duplicate nodes
function removeDuplicates(data: Node[]): Node[] {
const seenIdentifiers = new Set<string>();
const uniqueData: Node[] = [];
for (const node of data) {
const { identifier } = node;
if (!seenIdentifiers.has(identifier)) {
seenIdentifiers.add(identifier);
uniqueData.push(node);
}
}
return uniqueData;
}

// Function to add restVersion if it is missing
function addRestVersion(data: Node[]): Node[] {
for (const node of data) {
if (!node.restVersion) {
node.restVersion = 'v4';
}
}
return data;
}

// Load data from nodes.json
const data: Node[] = JSON.parse(fs.readFileSync('nodes.json', 'utf8'));

// Add restVersion if it is missing
const dataWithRestVersion = addRestVersion(data);

// Remove duplicates
const uniqueData = removeDuplicates(dataWithRestVersion);

// Check if any nodes were removed
if (uniqueData.length < data.length) {
console.log("Duplicate nodes removed.");

// Save updated data back to nodes.json
fs.writeFileSync('nodes.json', JSON.stringify(uniqueData, null, 4));
} else {
console.log("No duplicate nodes found.");
}

// Update database with unique nodes
async function updateNodes(nodes: Node[]): Promise<void> {
console.log("Updating nodes in database...");
const prisma = new PrismaClient({ adapter });
await prisma.$connect();
// delete all nodes
await prisma.nodes.deleteMany();
// create new nodes

await prisma.nodes.createMany({
data: {
nodes: JSON.stringify(nodes)
}
});
await prisma.$disconnect();
}

// Call updateNodes with uniqueData
updateNodes(uniqueData)
.then(() => console.log("Nodes updated successfully."))
.catch(err => console.error("Error updating nodes:", err));

0 comments on commit a1ac5d3

Please sign in to comment.