forked from saeedesmaili/iranian-businesses-data-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.php
46 lines (36 loc) · 1.48 KB
/
indexer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
function readFilesAndCreateJSON($directory) {
$files = scandir($directory);
// Read existing data from output.json if it exists
$outputFile = 'output.json';
$jsonData = [];
if (file_exists($outputFile)) {
$jsonData = json_decode(file_get_contents($outputFile), true);
}
// Create an associative array for quick lookup by 'url'
$existingUrls = array_column($jsonData, null, 'url');
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$filePath = $directory . '/' . $file;
$fileName = basename($file, '.pdf'); // Remove extension
// Extract year from filename
$year = preg_match('/(\d{4})/', $fileName, $matches) ? $matches[1] : '';
// Create tags array
$tags = explode('-', $fileName);
// Check if the 'url' already exists in the existing data
if (!isset($existingUrls[$file])) {
// Add file data to JSON array only if the 'url' doesn't exist
$jsonData[] = [
'title' => ucwords( str_replace('-', ' ',$fileName)),
'url' => $file,
'year' => $year,
'tags' => $tags
];
}
}
}
// Write JSON data to file
file_put_contents($outputFile, json_encode($jsonData, JSON_PRETTY_PRINT));
}
// Replace 'your_directory_path' with the actual directory path
readFilesAndCreateJSON('reports');