diff --git a/.github/workflows/main .yml b/.github/workflows/main .yml index a314a0f..ff50980 100644 --- a/.github/workflows/main .yml +++ b/.github/workflows/main .yml @@ -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: | diff --git a/.gitignore b/.gitignore index 44ba994..479b01e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ node_modules # Keep environment variables out of version control .env -# Ignore poetry lock file -poetry.lock \ No newline at end of file +pnpm-lock.yaml \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ff09ab4 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2243e6e..d45f51c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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 { diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index 08db55c..0000000 --- a/pyproject.toml +++ /dev/null @@ -1,16 +0,0 @@ -[tool.poetry] -name = "lavalink-list" -version = "0.1.0" -description = "" -authors = ["appujet "] -readme = "README.md" - -[tool.poetry.dependencies] -python = "^3.10" -asyncio = "^3.4.3" -prisma = "^0.13.1" - - -[build-system] -requires = ["poetry-core"] -build-backend = "poetry.core.masonry.api" diff --git a/update_nodes.py b/update_nodes.py deleted file mode 100644 index ca539d1..0000000 --- a/update_nodes.py +++ /dev/null @@ -1,62 +0,0 @@ -import json -from prisma import Prisma -import asyncio - -# Function to remove duplicate nodes -def remove_duplicates(data): - seen_identifiers = set() - unique_data = [] - for node in data: - identifier = node['identifier'] - if identifier not in seen_identifiers: - seen_identifiers.add(identifier) - unique_data.append(node) - return unique_data - -# Function to add restVersion if it is missing -def add_restVersion(data): - for node in data: - if 'restVersion' not in node: - node['restVersion'] = 'v4' - return data - -# Load data from nodes.json -with open('nodes.json') as f: - data = json.load(f) - -# Add restVersion if it is missing -data = add_restVersion(data) - -# Remove duplicates -unique_data = remove_duplicates(data) - -# Check if any nodes were removed -if len(unique_data) < len(data): - print("Duplicate nodes removed.") - - # Save updated data back to nodes.json - with open('nodes.json', 'w') as f: - json.dump(unique_data, f, indent=4) -else: - print("No duplicate nodes found.") - -# Save data with restVersion added -with open('nodes.json', 'w') as f: - json.dump(unique_data, f, indent=4) - -# Update database with unique nodes -async def update_nodes(nodes): - print("Updating nodes in database...") - prisma = Prisma() - await prisma.connect() - # delete all nodes - await prisma.nodes.delete_many() - # create new nodes - await prisma.nodes.create({ - "nodes": nodes - }) - await prisma.disconnect() - -json_nodes = json.dumps(unique_data) -# Call updateNodes with unique_data -asyncio.run(update_nodes(json_nodes)) diff --git a/update_nodes.ts b/update_nodes.ts new file mode 100644 index 0000000..e4ac771 --- /dev/null +++ b/update_nodes.ts @@ -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(); + 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 { + 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));