1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MalScraper\Helper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Helper class. |
7
|
|
|
*/ |
8
|
|
|
class Helper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Convert return result into easy-to-read result. |
12
|
|
|
* |
13
|
|
|
* @param string|array $response |
14
|
|
|
* |
15
|
|
|
* @return string|array |
16
|
|
|
*/ |
17
|
|
|
public static function toResponse($response) |
18
|
|
|
{ |
19
|
|
|
switch ($response) { |
20
|
|
|
case 400: |
21
|
|
|
return 'Search query needs at least 3 letters'; |
22
|
|
|
case 403: |
23
|
|
|
return 'Private user list'; |
24
|
|
|
case 404: |
25
|
|
|
return 'Page not found'; |
26
|
|
|
default: |
27
|
|
|
return $response; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Convert return result into http response. |
33
|
|
|
* |
34
|
|
|
* @param string|array $response |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public static function response($response) |
39
|
|
|
{ |
40
|
|
|
$result = []; |
41
|
|
|
if (is_numeric($response)) { |
42
|
|
|
header('HTTP/1.1 '.$response); |
43
|
|
|
$result['status'] = $response; |
44
|
|
|
$result['status_message'] = self::toResponse($response); |
45
|
|
|
$result['data'] = []; |
46
|
|
|
} else { |
47
|
|
|
header('HTTP/1.1 '. 200); |
48
|
|
|
$result['status'] = 200; |
49
|
|
|
$result['status_message'] = 'Success'; |
50
|
|
|
$result['data'] = self::superEncode($response); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$json_response = json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
54
|
|
|
$json_response = str_replace('\\\\', '', $json_response); |
55
|
|
|
|
56
|
|
|
return $json_response; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Convert characters to UTF-8. |
61
|
|
|
* |
62
|
|
|
* @param array $array |
63
|
|
|
* |
64
|
|
|
* @return array|string |
65
|
|
|
*/ |
66
|
|
|
private static function superEncode($array) |
67
|
|
|
{ |
68
|
|
|
if (is_array($array) && !empty($array)) { |
69
|
|
|
foreach ($array as $key => $value) { |
70
|
|
|
if (is_array($value)) { |
71
|
|
|
$array[$key] = self::superEncode($value); |
72
|
|
|
} else { |
73
|
|
|
$array[$key] = mb_convert_encoding($value, 'UTF-8', 'UTF-8'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $array; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get top anime code. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public static function getTopAnimeType() |
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'', |
90
|
|
|
'airing', |
91
|
|
|
'upcoming', |
92
|
|
|
'tv', |
93
|
|
|
'movie', |
94
|
|
|
'ova', |
95
|
|
|
'special', |
96
|
|
|
'bypopularity', |
97
|
|
|
'favorite', |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get top manga code. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public static function getTopMangaType() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
'', |
110
|
|
|
'manga', |
111
|
|
|
'novel', |
112
|
|
|
'oneshots', |
113
|
|
|
'doujin', |
114
|
|
|
'manhwa', |
115
|
|
|
'manhua', |
116
|
|
|
'bypopularity', |
117
|
|
|
'favorite', |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get current season. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public static function getCurrentSeason() |
127
|
|
|
{ |
128
|
|
|
$currentMonth = date('m'); |
129
|
|
|
|
130
|
|
|
if ($currentMonth >= '01' && $currentMonth < '04') { |
131
|
|
|
return 'winter'; |
132
|
|
|
} |
133
|
|
|
if ($currentMonth >= '04' && $currentMonth < '07') { |
134
|
|
|
return 'spring'; |
135
|
|
|
} |
136
|
|
|
if ($currentMonth >= '07' && $currentMonth < '10') { |
137
|
|
|
return 'summer'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return 'autumn'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Clean image URL. |
145
|
|
|
* |
146
|
|
|
* @param string $str |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public static function imageUrlCleaner($str) |
151
|
|
|
{ |
152
|
|
|
preg_match('/(questionmark)|(qm_50)/', $str, $temp_image); |
153
|
|
|
$str = $temp_image ? '' : $str; |
154
|
|
|
$str = str_replace('v.jpg', '.jpg', $str); |
155
|
|
|
$str = str_replace('_thumb.jpg', '.jpg', $str); |
156
|
|
|
$str = str_replace('userimages/thumbs', 'userimages', $str); |
157
|
|
|
$str = preg_replace('/r\/\d{1,3}x\d{1,3}\//', '', $str); |
158
|
|
|
$str = preg_replace('/\?.+/', '', $str); |
159
|
|
|
|
160
|
|
|
return $str; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Clean video URL. |
165
|
|
|
* |
166
|
|
|
* @param string $str |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public static function videoUrlCleaner($str) |
171
|
|
|
{ |
172
|
|
|
$str = preg_replace('/\?.+/', '', $str); |
173
|
|
|
$str = str_replace('embed/', 'watch?v=', $str); |
174
|
|
|
|
175
|
|
|
return $str; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|