Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Created September 9, 2022 20:52
Show Gist options
  • Save pjaudiomv/a7b05e6102eef56f8b0eac85592c21b5 to your computer and use it in GitHub Desktop.
Save pjaudiomv/a7b05e6102eef56f8b0eac85592c21b5 to your computer and use it in GitHub Desktop.
bmlt2csv.php
<?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);
@pjaudiomv
Copy link
Author

with jq

curl -s 'https://latest.aws.bmlt.app/main_server/client_interface/json/?switcher=GetSearchResults&services=1010&weekdays=2' | jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[ $keys[] ]])[] | @csv'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment