public inbox for [email protected]
 help / color / mirror / Atom feed
From: Taufiq Pohan <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Taufiq Pohan <[email protected]>,
	Aldy Prastyo <[email protected]>,
	VNLX Kernel Department <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [RESEND PATCH v1 2/4] Initial VNDB scraper and storage management
Date: Sun, 27 Nov 2022 02:37:22 +0700	[thread overview]
Message-ID: <TY0PR06MB5427116C2E8128A94E58BE83E2119@TY0PR06MB5427.apcprd06.prod.outlook.com> (raw)
In-Reply-To: <[email protected]>

Signed-off-by: Ammar Faizi <[email protected]>
Co-authored-by: Ammar Faizi <[email protected]>
Signed-off-by: Aldy Prastyo <[email protected]>
Co-authored-by: Aldy Prastyo <[email protected]>
Signed-off-by: Taufiq Pohan <[email protected]>
---
 .gitignore   |  7 ++--
 VNDBModel.js | 34 +++++++++++++++++++
 index.js     | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 package.json |  3 ++
 4 files changed, 135 insertions(+), 5 deletions(-)
 create mode 100644 VNDBModel.js

diff --git a/.gitignore b/.gitignore
index b855cbc..29ea801 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
-.env
-node_modules/
-yarn.lock
\ No newline at end of file
+/.env
+/node_modules/
+/yarn.lock
+/vn-stats.json
\ No newline at end of file
diff --git a/VNDBModel.js b/VNDBModel.js
new file mode 100644
index 0000000..4ba11a0
--- /dev/null
+++ b/VNDBModel.js
@@ -0,0 +1,34 @@
+import mongoose from "mongoose";
+
+// Schema
+
+const VisualNovel = mongoose.Schema({
+    code: {
+        type: String,
+        required: true
+    },
+    title: {
+        type: String,
+        required: true
+    },
+    alias: {
+        type: String
+    },
+    length: {
+        type: Number
+    },
+    rating: {
+        type: Number,
+    },
+    description: {
+        type: String,
+        required: true
+    },
+    image: {
+        type: String
+    }
+}, {
+    timestamps: true
+});
+
+export default mongoose.model('vndb', VisualNovel);
\ No newline at end of file
diff --git a/index.js b/index.js
index a3dadfb..9ceee76 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,96 @@
-import VNDB from "vndb-api";
-
+import VNDB from 'vndb-api';
 const vndb = new VNDB('atri_api');
+import mongoose from "mongoose";
+import { config } from "dotenv";
+import model from './VNDBModel.js';
+import fs from 'fs';
+
+config();
+
+mongoose.connect(process.env.MONGODB_URI, {
+	useNewUrlParser: true,
+	useUnifiedTopology: true
+});
+
+const init_db = () =>
+	mongoose.connection
+		.on('error', (error) => console.error(error))
+		.once('open', () => console.log('Database Connected'));
+
+async function get_vn_by_code(code)
+{
+	return await vndb.query(`get vn details,basic,stats (id = ${code})`);
+}
+
+async function insert_to_db(result)
+{
+	const body = {
+		code: result.id,
+		title: result.title,
+		alias: result.alias,
+		length: result.length,
+		rating: result.rating,
+		description: result.image,
+		image: result.image
+	};
+	const response = await model(body);
+	await response.save();
+}
+
+async function scrape_vn_and_save_to_db(code)
+{
+	const result = await get_vn_by_code(code);
+	if (!result) {
+		console.log("Internal error");
+		return false;
+	}
+
+	if (result.items.length == 0) {
+		console.log(`VN ${code} is not found`);
+		return false;
+	}
+
+	insert_to_db(result.items[0]);
+	return true;
+}
+
+function save_last_id(id)
+{
+	const jsonVal = {
+		last_vn_id: id
+	};
+	fs.writeFileSync('vn-stats.json', JSON.stringify(jsonVal)+"\n");
+	return true;
+}
+
+function get_last_id()
+{
+	if (fs.existsSync('./vn-stats.json')) {
+		const jsonVal = require('./vn-stats.json');
+		return jsonVal['last_vn_id'];
+	}
+
+	return 1;
+}
+
+async function main()
+{
+	init_db();
+
+	let code = 40029;
+	let i;
+
+	i = code - 5;
+	while (i++) {
+		console.log(`Scraping VN ${i}...`);
+		let ret = await scrape_vn_and_save_to_db(i);
+		if (!ret)
+			break;
+		console.log(`Successfully scraped VN ${i}`);
+	}
+	console.log(`Last VN ID is ${code}`);
+	save_last_id(i);
+	process.exit();
+}
 
+main();
\ No newline at end of file
diff --git a/package.json b/package.json
index 7ef6a42..e5e9912 100644
--- a/package.json
+++ b/package.json
@@ -2,12 +2,15 @@
   "name": "vndb_scraper",
   "version": "1.0.0",
   "description": "VNDB Scrapper for ATRI",
+  "type": "module",
   "main": "index.js",
   "repository": "[email protected]:vnlx2/vndb_scraper.git",
   "author": "Taufiq Pohan <[email protected]>",
   "license": "GPLv2",
   "dependencies": {
     "dotenv": "^16.0.3",
+    "fs": "^0.0.1-security",
+    "mongoose": "^6.7.3",
     "mongose": "^0.0.2-security",
     "vndb-api": "^1.0.3"
   }
-- 
Taufiq Pohan


  parent reply	other threads:[~2022-11-26 19:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <[email protected]>
2022-11-26 19:37 ` [RESEND PATCH v1 1/4] Add vndb-api, mongose, and dotenv module Taufiq Pohan
2022-11-26 20:11   ` Ammar Faizi
2022-11-26 19:37 ` Taufiq Pohan [this message]
2022-11-26 19:37 ` [RESEND PATCH v1 3/4] index: Integrate vn-stats.json with the scraper Taufiq Pohan
2022-11-26 19:37 ` [RESEND PATCH v1 4/4] .gitignore: Add *.patch file to .gitingore Taufiq Pohan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=TY0PR06MB5427116C2E8128A94E58BE83E2119@TY0PR06MB5427.apcprd06.prod.outlook.com \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox