71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__.'/../includes/pexels.php';
|
|
|
|
$q = $_GET['query'] ?? 'nature';
|
|
// Adding negative keywords to filter results
|
|
$haram_elements = [
|
|
'bikini', 'uncovered woman', 'alcohol', 'bar', 'gambling', 'pork', 'haram',
|
|
'dating', 'romance', 'kissing', 'nightclub', 'party', 'dog' // dog is controversial for some, better to be safe
|
|
];
|
|
$negative_query = implode(' ', $haram_elements);
|
|
|
|
// Pexels API does not support negative keywords in the `query` parameter directly.
|
|
// The search will be broad and then filtered, which is not ideal but a limitation of the free API.
|
|
// A better approach is to append " halal" or similar positive keywords.
|
|
$query = $q . ' halal islamic';
|
|
|
|
$orientation = $_GET['orientation'] ?? 'portrait';
|
|
$url = 'https://api.pexels.com/videos/search?query=' . urlencode($query) . '&orientation=' . urlencode($orientation) . '&per_page=6';
|
|
|
|
$data = pexels_get($url);
|
|
|
|
if (!$data || empty($data['videos'])) {
|
|
echo json_encode(['error' => 'Failed to fetch videos for query: ' . $query, 'videos' => []]);
|
|
exit;
|
|
}
|
|
|
|
$videos = [];
|
|
foreach ($data['videos'] as $video) {
|
|
$video_file = null;
|
|
// Find a suitable video file link
|
|
foreach ($video['video_files'] as $file) {
|
|
if ($file['quality'] === 'hd' && (strpos($file['link'], '.mp4') !== false)) {
|
|
$video_file = $file;
|
|
break;
|
|
}
|
|
}
|
|
// Fallback to any mp4 file
|
|
if (!$video_file) {
|
|
foreach ($video['video_files'] as $file) {
|
|
if (strpos($file['link'], '.mp4') !== false) {
|
|
$video_file = $file;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($video_file) {
|
|
$videos[] = [
|
|
'id' => $video['id'],
|
|
'thumbnail' => $video['image'],
|
|
'download_url' => $video_file['link'],
|
|
'photographer' => $video['user']['name'] ?? 'Unknown',
|
|
'photographer_url' => $video['user']['url'] ?? '',
|
|
];
|
|
}
|
|
}
|
|
|
|
// Second-level filtering for haram terms in video tags, if available
|
|
$filtered_videos = array_filter($videos, function($video) use ($haram_elements) {
|
|
if (isset($video['tags']) && is_array($video['tags'])) {
|
|
foreach ($haram_elements as $haram) {
|
|
if (in_array($haram, $video['tags'])) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
|
|
echo json_encode(['videos' => array_values($filtered_videos)]); |