-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
179 lines (166 loc) · 8.97 KB
/
api.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
require_once('vendor/autoload.php');
// Calculate Fridays
$given_year = strtotime("1 January 2018");
$for_start = strtotime('Friday', strtotime("1 January 2018"));
$for_end = strtotime('+1 year', strtotime("1 January 2027"));
$fridays = [];
for ($i = $for_start; $i <= $for_end; $i = strtotime('+1 week', $i)) {
// if the date is friday add it to the array
if (date('l', $i) == 'Friday') {
$fridays[] = date('Y-m-d', $i);
}
}
$iteratorMp3 = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('downloads/mp3'));
$mp3s = array_keys(array_filter(iterator_to_array($iteratorMp3), function($file) {
return $file->isFile();
}));
$iteratorPdf = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('downloads/pdf'));
$pdfs = array_keys(array_filter(iterator_to_array($iteratorPdf), function($file) {
return $file->isFile();
}));
$iteratorDoc = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('downloads/doc'));
$docs = array_keys(array_filter(iterator_to_array($iteratorDoc), function($file) {
return $file->isFile();
}));
$sermons = [];
prep($mp3s, 'mp3', $fridays, $sermons);
prep($docs, 'doc', $fridays, $sermons);
prep($pdfs, 'pdf', $fridays, $sermons);
$sources = [
['name' => 'General Authority of Islamic Affairs and Endowments, UAE', 'handle' => 'uae-awqaf', 'years' => [2015, 2016, 2018, 2019, 2020, 2021, 2022, 2023, 2024]]
];
$languages = [
['code' => 'ur', 'name' => 'Urdu'],
['code' => 'ar', 'name' => 'Arabic'],
['code' => 'en', 'name' => 'English'],
['code' => 'es', 'name' => 'Spanish'],
];
foreach($sermons as $year => $type) {
foreach ($type as $t => $month) {
foreach ($month as $m => $s) {
$sermons[$year][$t][$m] = array_values( $sermons[$year][$t][$m]);
}
}
}
// Normalise
$years = [2024];
$months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
// Now let's write the files
// By year.
!is_dir("api/uae-awqaf/") ? mkdir("api/uae-awqaf/"): "";
!is_dir ("yaml/uae-awqaf/") ? mkdir("yaml/uae-awqaf/") : "";
foreach ($years as $year) {
!is_dir("api/uae-awqaf/$year") ? mkdir("api/uae-awqaf/$year") : "";
!is_dir("yaml/uae-awqaf/$year") ? mkdir("yaml/uae-awqaf/$year") : "";
foreach ($months as $month) {
!is_dir("api/uae-awqaf/$year/$month") ? mkdir("api/uae-awqaf/$year/$month") : "";
!is_dir("yaml/uae-awqaf/$year/$month") ? mkdir("yaml/uae-awqaf/$year/$month") : "";
}
if (isset($sermons[$year]['friday'])) {
foreach($sermons[$year]['friday'] as $m => $x) {
// Append month details
$sermons[$year]['friday'][$m]['sermons'] = $sermons[$year]['friday'][$m];
$sermons[$year]['friday'][$m]['month'] = $sermons[$year]['friday'][$m][0]['date']['month'];
foreach ($sermons[$year]['friday'][$m] as $k => $v) {
if ($k !== 'month' && $k !== 'sermons') {
unset($sermons[$year]['friday'][$m][$k]);
}
}
file_put_contents("api/uae-awqaf/$year/$m/friday.json", json_encode($sermons[$year]['friday'][$m]));
file_put_contents("yaml/uae-awqaf/$year/$m/friday.yml", \Symfony\Component\Yaml\Yaml::dump($sermons[$year]['friday'][$m]));
}
file_put_contents("api/uae-awqaf/$year/friday.json", json_encode(array_values($sermons[$year]['friday'])));
file_put_contents("yaml/uae-awqaf/$year/friday.yml", \Symfony\Component\Yaml\Yaml::dump(array_values($sermons[$year]['friday'])));
}
if (isset($sermons[$year]['other'])) {
foreach($sermons[$year]['other'] as $m => $x) {
$sermons[$year]['other'][$m]['sermons'] = $sermons[$year]['other'][$m];
$sermons[$year]['other'][$m]['month'] = $sermons[$year]['other'][$m][0]['date']['month'];
foreach ($sermons[$year]['other'][$m] as $k => $v) {
if ($k !== 'month' && $k !== 'sermons') {
unset($sermons[$year]['other'][$m][$k]);
}
}
file_put_contents("api/uae-awqaf/$year/$m/other.json", json_encode($sermons[$year]['other'][$m]));
file_put_contents("yaml/uae-awqaf/$year/$m/other.yml", \Symfony\Component\Yaml\Yaml::dump($sermons[$year]['other'][$m]));
}
file_put_contents("api/uae-awqaf/$year/other.json", json_encode(array_values($sermons[$year]['other'])));
file_put_contents("yaml/uae-awqaf/$year/other.yml", \Symfony\Component\Yaml\Yaml::dump(array_values($sermons[$year]['other'])));
}
foreach ($sermons[$year] as $st => $sb) {
//$sermons[$year][$st]['type'] = $st;
foreach ($sermons[$year][$st] as $mx => $mk) {
file_put_contents("api/uae-awqaf/$year/$mx.json", json_encode([$mk]));
file_put_contents("yaml/uae-awqaf/$year/$mx.yml", \Symfony\Component\Yaml\Yaml::dump([$mk]));
$sermons[$year][] = $mk;
unset($sermons[$year][$st]);
}
}
file_put_contents("api/uae-awqaf/$year.json", json_encode(array_values($sermons[$year])));
file_put_contents("yaml/uae-awqaf/$year.yml", \Symfony\Component\Yaml\Yaml::dump(array_values($sermons[$year])));
}
file_put_contents("api/sources.json", json_encode($sources));
file_put_contents("yaml/sources.yml", \Symfony\Component\Yaml\Yaml::dump($sources));
file_put_contents("api/languages.json", json_encode($languages));
file_put_contents("yaml/languages.yml", \Symfony\Component\Yaml\Yaml::dump($languages));
function prep($fx, $format, $fridays, &$sermons) {
$source = 'uae-awqaf';
foreach($fx as $file) {
if (strpos($file, '.mp3') !== false || strpos($file, '.docx') !== false || strpos($file, '.doc') !== false || strpos($file, '.pdf') !== false) {
$parts = explode("/", $file);
//$df = $parts[2];
$dx = explode("-", $parts[2]);
$datex = $dx[0] . '-' . $dx[1] . '-' . $dx[2];
$lang = $dx[3];
$title = trim(str_replace("_", " ", explode('.', $dx[4])[0]));
$url = "https://cdn.islamic.network/sermons/uae-awqaf/$format/" . $parts[2];
$date = new DateTime(str_replace("-", "/", $datex), new DateTimeZone('Asia/Dubai'));
$dateObj = [
'iso8601' => $date->format('c'),
'month' => ['name' => $date->format('F'), 'shortname' => $date->format('M'), 'number' => $date->format('m')],
'day' => ['name' => $date->format('l'), 'shortname' => $date->format('D'), 'number' => $date->format('d')],
'year' => $date->format('Y')
];
if (in_array($datex, $fridays)) {
// Friday sermon
if (isset($sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)])) {
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['editions'][] = [
'language' => $lang,
'format' => $format,
'url' => $url
];
} else {
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['title'] = $title;
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['date'] = $dateObj;
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['type'] = 'friday';
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['source'] = $source;
$sermons[$date->format('Y')]['friday'][$date->format('m')][md5($title)]['editions'][] = [
'language' => $lang,
'format' => $format,
'url' => $url
];
}
} else {
// Other / Eid sermon
if (isset($sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)])) {
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['editions'][] = [
'language' => $lang,
'format' => $format,
'url' => $url
];
} else {
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['title'] = $title;
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['date'] = $dateObj;
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['type'] = 'other';
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['source'] = $source;
$sermons[$date->format('Y')]['other'][$date->format('m')][md5($title)]['editions'][] = [
'language' => $lang,
'format' => $format,
'url' => $url
];
}
}
}
}
}