298 lines
8.3 KiB
JavaScript
298 lines
8.3 KiB
JavaScript
const fs = require('fs')
|
|
const mysql = require('mysql')
|
|
const PDFParser = require('pdf-parse')
|
|
const listItemCheck = [
|
|
'S&P/ASX 20 ',
|
|
'S&P/ASX 50',
|
|
'S&P/ASX 100',
|
|
'S&P/ASX 200',
|
|
'S&P/ASX 300',
|
|
'All Ordinaries',
|
|
]
|
|
const months = [
|
|
'January',
|
|
'February',
|
|
'March',
|
|
'April',
|
|
'May',
|
|
'June',
|
|
'July',
|
|
'August',
|
|
'September',
|
|
'October',
|
|
'November',
|
|
'December',
|
|
]
|
|
const monthAbbreviations = [
|
|
'Jan',
|
|
'Feb',
|
|
'Mar',
|
|
'Apr',
|
|
'May',
|
|
'Jun',
|
|
'Jul',
|
|
'Aug',
|
|
'Sep',
|
|
'Oct',
|
|
'Nov',
|
|
'Dec',
|
|
]
|
|
const allMonths = months.concat(monthAbbreviations)
|
|
async function readPDF(filePath) {
|
|
try {
|
|
// Read the PDF file
|
|
const dataBuffer = fs.readFileSync(filePath)
|
|
|
|
// Parse the PDF content
|
|
const pdf = await PDFParser(dataBuffer)
|
|
|
|
// Extract text from the parsed PDF
|
|
const text = pdf.text
|
|
|
|
return text
|
|
} catch (error) {
|
|
console.error('Error:', error)
|
|
return null
|
|
}
|
|
}
|
|
//
|
|
const getDataToImportDB = async (text) => {
|
|
try {
|
|
let results = []
|
|
text = text.replace(/ /g, ' ')
|
|
const arrayContent = text
|
|
.toString()
|
|
.split('\n')
|
|
.filter(
|
|
(line) =>
|
|
line.trim() !== '' &&
|
|
!line.replace(/ /g, '').includes('INDEXANNOUNCEMENT') &&
|
|
!line.replace(/ /g, '').includes('ActionCodeCompany') &&
|
|
((line.replace(/ /g, '').includes('S&P') &&
|
|
line.replace(/ /g, '').includes('Index')) ||
|
|
line.includes('Addition') ||
|
|
line.includes('Removal') ||
|
|
line.includes('All Ordinaries ')),
|
|
)
|
|
const positionItem = []
|
|
listItemCheck.map((item) => {
|
|
arrayContent.map((line, index) => {
|
|
if (line.includes(item)) {
|
|
positionItem.push({ itemName: item, index: index })
|
|
}
|
|
|
|
if (arrayContent[index].includes('Addition')) {
|
|
arrayContent[index] = arrayContent[index].slice(
|
|
arrayContent[index].indexOf('Addition'),
|
|
)
|
|
}
|
|
|
|
if (arrayContent[index].includes('Removal')) {
|
|
arrayContent[index] = arrayContent[index].slice(
|
|
arrayContent[index].indexOf('Removal'),
|
|
)
|
|
}
|
|
})
|
|
})
|
|
|
|
positionItem.map((item, index) => {
|
|
let count = item.index
|
|
if (item.itemName === 'All Ordinaries') {
|
|
positionItem[index].itemName = 'S&P/ASX 500'
|
|
}
|
|
|
|
while (
|
|
arrayContent[count + 1] && (arrayContent[count + 1].includes('Addition') || arrayContent[count + 1].includes('Removal') )
|
|
) {
|
|
const line = arrayContent[count + 1]
|
|
let date = ''
|
|
// Regular expression pattern to match month names
|
|
const regex = new RegExp('(' + allMonths.join('|') + ')', 'i') // 'i' flag for case-insensitive matching
|
|
|
|
// Use match method to find the month
|
|
const match = arrayContent[item.index].replace(/ /g, '').match(regex)
|
|
|
|
if (match) {
|
|
const monthName = match[0]
|
|
const monthIndex =
|
|
(allMonths.findIndex(
|
|
(m) => m.toLowerCase() === monthName.toLowerCase(),
|
|
) %
|
|
12) +
|
|
1 // Take modulo to handle abbreviations
|
|
date = monthIndex + '-15'
|
|
} else {
|
|
console.log('Month not found in the string.')
|
|
}
|
|
// Regular expression pattern to match year (4 digits)
|
|
const regexYear = /[0-9][0-9][0-9][0-9]/
|
|
|
|
// Use match method to find the year
|
|
const matchYear = arrayContent[item.index]
|
|
.replace(/ /g, '')
|
|
.match(regexYear)
|
|
|
|
if (matchYear) {
|
|
date = matchYear[0] + '-' + date
|
|
} else {
|
|
console.log('Year not found in the string.')
|
|
}
|
|
|
|
if (line.includes('Addition')) {
|
|
results.push({
|
|
Idx: item.itemName.split('/')[1].replace(/ /g, '').trim(),
|
|
Code: line.split(' ')[1],
|
|
StartDate: date,
|
|
// EndDate: '2099-12-15',
|
|
})
|
|
} else {
|
|
results.push({
|
|
Idx: item.itemName.split('/')[1].replace(/ /g, '').trim(),
|
|
Code: line.split(' ')[1],
|
|
// StartDate: '1990-3-15',
|
|
EndDate: date,
|
|
})
|
|
}
|
|
count += 1
|
|
}
|
|
})
|
|
|
|
updateDB(results)
|
|
} catch (error) {
|
|
console.log(error)
|
|
}
|
|
}
|
|
|
|
// Function to read the directory asynchronously
|
|
async function readDirectory(path) {
|
|
return new Promise((resolve, reject) => {
|
|
fs.readdir(path, (err, files) => {
|
|
if (err) {
|
|
reject(err)
|
|
} else {
|
|
resolve(files)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
const directoryPath = 'E:\\Downloads\\marketindex\\rebalancePDFFile'
|
|
// Function to log files sequentially with random delays
|
|
async function logFiles(files) {
|
|
for (const file of files) {
|
|
await new Promise((resolve) => {
|
|
readPDF(directoryPath + '\\' + file)
|
|
.then((text) => {
|
|
console.log(directoryPath + '\\' + file)
|
|
getDataToImportDB(text)
|
|
setTimeout(() => {
|
|
resolve()
|
|
}, 5000);
|
|
})
|
|
.catch((error) => {
|
|
console.error('Error:', error)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
readDirectory(directoryPath)
|
|
.then((files) => {
|
|
return logFiles(files)
|
|
})
|
|
.catch((err) => {
|
|
console.error('Error reading directory:', err)
|
|
})
|
|
|
|
const pdfFilePath = 'E:\\Downloads\\marketindex\\2018-rebalance-march.pdf'
|
|
|
|
// Hàm thực hiện truy vấn với promise
|
|
const query = (connection, sql, values) => {
|
|
return new Promise((resolve, reject) => {
|
|
connection.query(sql, values, (error, results) => {
|
|
if (error) {
|
|
reject(error)
|
|
} else {
|
|
resolve(results)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
const updateDB = async (data) => {
|
|
// Tạo kết nối đến cơ sở dữ liệu MySQL
|
|
const connection = mysql.createConnection({
|
|
host: 'localhost', // Thay bằng địa chỉ máy chủ MySQL của bạn
|
|
user: 'root', // Thay 'username' bằng tên người dùng MySQL của bạn
|
|
password: 'Maypjtkh0ng', // Thay 'password' bằng mật khẩu của bạn
|
|
database: 'marketindex', // Thay 'database_name' bằng tên cơ sở dữ liệu của bạn
|
|
})
|
|
|
|
// Kết nối đến cơ sở dữ liệu MySQL
|
|
connection.connect((err) => {
|
|
if (err) {
|
|
console.error('Error connecting to MySQL:', err)
|
|
return
|
|
}
|
|
console.log('Connected to MySQL database.')
|
|
})
|
|
|
|
try {
|
|
for (const row of data) {
|
|
const results = await query(
|
|
connection,
|
|
'SELECT * FROM indice WHERE Code = ? AND Idx = ?',
|
|
[row.Code, row.Idx],
|
|
)
|
|
if (results.length > 0) {
|
|
if (row.StartDate) {
|
|
await query(
|
|
connection,
|
|
'UPDATE indice SET StartDate = ?, EndDate = "2099-12-15" WHERE Idx = ? AND Code = ?',
|
|
[row.StartDate, row.Idx, row.Code],
|
|
)
|
|
console.log(
|
|
`Update ${row.Code}-${row.Idx} (Add at ${row.StartDate}) success`,
|
|
)
|
|
}
|
|
if (row.EndDate) {
|
|
await query(
|
|
connection,
|
|
'UPDATE indice SET EndDate = ? WHERE Idx = ? AND Code = ?',
|
|
[row.EndDate, row.Idx, row.Code],
|
|
)
|
|
console.log(
|
|
`Update ${row.Code}-${row.Idx} (Remove at ${row.EndDate}) success`,
|
|
)
|
|
}
|
|
} else {
|
|
if (row.StartDate) {
|
|
await query(
|
|
connection,
|
|
'INSERT INTO indice (Idx, Code, StartDate, EndDate) VALUES (?, ?, ?, "2099-12-15")',
|
|
[row.Idx, row.Code, row.StartDate],
|
|
)
|
|
console.log(
|
|
`Insert new code: ${row.Code}-${row.Idx} (Insert)(Add at ${row.StartDate}) success`,
|
|
)
|
|
}
|
|
if (row.EndDate) {
|
|
await query(
|
|
connection,
|
|
'INSERT INTO indice (Idx, Code, EndDate) VALUES (?, ?, ?)',
|
|
[row.Idx, row.Code, row.EndDate],
|
|
)
|
|
console.log(
|
|
`Insert new code: ${row.Code}-${row.Idx} (Insert)(Remove at ${row.EndDate}) success`,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error:', error)
|
|
} finally {
|
|
// Đóng kết nối đến cơ sở dữ liệu MySQL
|
|
console.log(`\nUpdated ${data.length} code!`)
|
|
connection.end()
|
|
}
|
|
}
|