-
Notifications
You must be signed in to change notification settings - Fork 3
/
بەڕۆژکردنەوە
executable file
·195 lines (181 loc) · 4.58 KB
/
بەڕۆژکردنەوە
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/php
<?php
/*
* دابەزاندنی شیعرەکانی ئاڵەکۆک
* بۆ زانیاری زیاتر سەردانی لاپەڕەی خوارەوە بکەن:
* https://allekok.ir/dev/tools/
*/
/* Constants */
const __POEMS_DIR = "شیعرەکان";
/* Run */
دابەزاندنی_هەموو_شیعرەکان();
لیست();
/* Functions */
function download_poets_info($poet_id) {
$url = "https://allekok.ir/dev/tools/poet.php?poet=$poet_id";
while(!$json = json_decode(file_get_contents($url), true))
sleep(1);
echo "Poets info downloaded.\n\n";
return $json;
}
function دابەزاندنی_هەموو_شیعرەکان() {
global $argv;
$arg = @$argv[1] ?
convert_numbers("ck", "en", $argv[1]) :
"all";
if($arg == "لیست")
return;
$poets = download_poets_info($arg);
$poets_count = count($poets);
foreach($poets as $p_num => $poet) {
foreach($poet['bks'] as $i => $book) {
$encoded_bk = urlencode($book);
$book = str_replace("/", "-", $book);
$path = __POEMS_DIR . "/" .
$poet['profname'] . "/" .
$book;
check_mkdir($path);
/* دابەزاندنی شیعرەکان */
$poems_uri = "https://allekok.ir/dev/tools/" .
"poem.php?poet=" . $poet['id'] .
"&book=" . $encoded_bk .
"&poem=all";
while(!$poems = json_decode(
file_get_contents($poems_uri),
true)["poems"])
{
sleep(1);
}
echo "(" . ($p_num+1) . " of {$poets_count}) ";
echo "Poet: " . $poet["id"] . "\tBook: " .
($i + 1) . "\tPoems downloaded.\n";
/* پاشەکەوت کردنی شیعرەکان */
$max_file_name_length = 50;
for($i = 0; $i < count($poems); $i++) {
$poems[$i]["hon"] = str_replace(
["\r", "'", """,
"«", "»",
" ", " ", " "],
["", "'", "\"", "«", "»",
" ", " ", " "],
$poems[$i]["hon"]);
$poems[$i]["hon"] = preg_replace(
"/\n\n+/u", "\n\n",
$poems[$i]["hon"]);
$poems[$i]["hon"] = trim(
$poems[$i]["hon"]);
$poems[$i]["name"] = trim(
$poems[$i]["name"]);
$filename = convert_numbers(
"en", "ck", $poems[$i]["id"]) .
". ";
$filename .= mb_substr(str_replace(
["/", "."],
["-", ""],
$poems[$i]["name"]
), 0, $max_file_name_length);
$towrite = "شاعیر: " .
$poet["profname"];
$towrite .= "\nکتێب: " . $book;
$towrite .= "\nسەرناو: " .
$poems[$i]["name"];
if(trim($poems[$i]["hdesc"])) {
$h = $poems[$i]["hdesc"];
$h = str_replace("<br>",
" - ",
$h);
$h = preg_replace("/\s\s+/u",
" ",
$h);
$h = filter_var(
$h,
FILTER_SANITIZE_STRING
);
$h = str_replace(
[""", "'"],
["\"", "'"],
$h);
$h = trim($h);
$towrite .= "\nلەبارەی شیعر: " .
$h;
}
$towrite .= "\n\n";
$towrite .= $poems[$i]["hon"];
file_put_contents($path . "/" .
$filename,
$towrite);
}
}
}
}
function check_mkdir($path) {
if(!file_exists($path))
mkdir($path, 0755, true);
}
function convert_numbers($f, $t, $string) {
$assoc = [
"en" => ['0','1','2','3','4','5','6','7','8','9'],
"ck" => ['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'],
];
return str_replace($assoc[$f], $assoc[$t], $string);
}
function لیست($path=".") {
$list = [];
$d = opendir($path);
while(false !== ($o = readdir($d))) {
if(in_array($o, [".", "..", ".git",
"بەڕۆژکردنەوە", "index.html",
"README.md", "دەیتابەیس"]))
{
continue;
}
$p = "$path/$o";
if(is_dir($p))
لیست($p);
$list[] = $o;
}
if(count(explode("/", "$path/$list[0]")) <= 4) {
/* Sorting Strings */
sort($list);
} else {
/* Sorting Numbers */
usort($list, function ($a, $b) {
$a_n = intval(convert_numbers("ck","en",$a));
$b_n = intval(convert_numbers("ck","en",$b));
return $a_n - $b_n;
});
}
$title = mb_substr($path, 2);
if(!$title)
$title = "ئاڵەکۆک";
$html = "<!DOCTYPE html>
<html dir='rtl' lang='ckb'>
\t<head>
\t\t<meta charset='utf-8'>
\t\t<meta name='viewport' content='width=device-width'>
\t\t<title>
\t\t\t$title
\t\t</title>
\t</head>
\t<body>
\t\t<h3>$title:</h3>\n";
foreach($list as $item) {
$R = rawurlencode($item);
$p = "/allekok-poems/" . mb_substr("$path/$R", 2);
if(count(explode("/",$p)) <= 5 && $item != "ئیجازەنامە")
$p .= "/";
$html .= "\t\t<p>
\t\t\t<a href='$p'>
\t\t\t\t$item
\t\t\t</a>
\t\t</p>\n";
}
$html .= "\t</body>\n</html>";
file_put_contents("$path/index.html", $html);
}
/*
* Local Variables:
* mode: web
* End:
*/
?>