Created
September 9, 2022 20:52
-
-
Save pjaudiomv/a7b05e6102eef56f8b0eac85592c21b5 to your computer and use it in GitHub Desktop.
bmlt2csv.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
bmlt2csv.php | |
This file converts a JSON BMLT GetSearchResults query to a CSV | |
To use run `php bmlt2csv.php` or place on your webserver and | |
specify a query param with a url encoded bmlt query url. | |
https://domain.com/bmlt2csv.php?query=https%3A%2F%2Flatest.aws.bmlt.app%2Fmain_server%2Fclient_interface%2Fjson%2F%3Fswitcher%3DGetSearchResults%26services%3D1010%26weekdays%3D2 | |
*/ | |
$cli_query = null; | |
if (!isset($_GET['query'])) { | |
echo "\e[1;35m\nEnter Your BMLT Url Query: \e[0m"; | |
$cli_query = rtrim(fgets(STDIN)); | |
} | |
if (!isset($cli_query) && !isset($_GET['query'])) { | |
echo "You must provide url to bmlt query"; | |
exit(0); | |
} | |
$query = $cli_query ?? urldecode($_GET['query']); | |
if (!str_contains($query, "/client_interface/json/?switcher=GetSearchResults")) { | |
echo "BMLT query url is invalid"; | |
exit(0); | |
} | |
$meetings = file_get_contents($query); | |
$meetings = json_decode($meetings, true); | |
$filename = 'BMLT' . date('_Y_m_d_H_i_s') . '.csv'; | |
$file = isset($cli_query) ? $filename : 'php://output'; | |
$file_pointer = fopen($file, 'w'); | |
ob_start(); | |
fputcsv($file_pointer, array_keys($meetings[0])); | |
foreach ($meetings as $i) { | |
fputcsv($file_pointer, $i); | |
} | |
if (isset($_GET['query'])) { | |
$data = ob_get_clean(); | |
header("Content-type: text/csv"); | |
header("Content-Disposition: attachment; filename=$filename"); | |
echo $data; | |
ob_end_flush(); | |
} | |
if (isset($cli_query)) { | |
echo "\e[1;35m\nFile has been outputted to current directory:\e[0m $filename"; | |
} | |
fclose($file_pointer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with jq