Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Script to download data from DMR-MARC "Last Heard" page(s) with filtering and limit. For use with Amateur radio DMR transcrievers
<?php
function removeTextBeforeEndOf($str,$findme)
{
return substr($str,(strpos($str,$findme)+strlen($findme)));
}
$csvFile ="";
$offsetID= 0;
$foundEnd=false;
if (!isset($_REQUEST["callsign"]))
{
echo "callsign paramater filter not defined e.g. callsign=VK*";
exit;
}
if (trim($_REQUEST["callsign"])=="")
{
echo "callsign paramater filter is empty";
exit;
}
$callsign = $_REQUEST["callsign"];
if (isset($_REQUEST["limit"]))
{
$limit=intval($_REQUEST["limit"]);
}
else
{
$limit=1000;
}
if (isset($_REQUEST["radio"]))
{
$radio=$_REQUEST["radio"];
}
else
{
$radio="";
}
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
for($page=0;$page<50&& $foundEnd == false;$page++)
{
// echo "http://ham-digital.org/dmr-lh.php?callsign=VK*&n=1000&s=". $page*100 . "\n";
$str = file_get_contents ("http://ham-digital.org/dmr-lh.php?callsign=" . $callsign . "&n=".$limit."&s=". $page*100);//"https://ham-digital.org/dmr-lh.php?callsign=VK*");
$pcount=0;
while($pcount++ < 100)
{
$prevLen = strlen($str);
$str=removeTextBeforeEndOf($str,"<tr bgcolor=\"#");// find row of data
if (strlen($str)== ($prevLen - strlen("<tr bgcolor=\"#")))
{
//echo "FOUND END\n";
$foundEnd=true;
break;
}
$str=removeTextBeforeEndOf($str,"CENTER>");
// Check if row contains valid data
if (strpos($str,"<a href")==0)
{
//echo "FOUND END\n";
$foundEnd=true;
break;
}
// Skip fields we don't need
$str=removeTextBeforeEndOf($str,"CENTER>");
$str=removeTextBeforeEndOf($str,"CENTER>");
$str=removeTextBeforeEndOf($str,"CENTER>");
// extract the ID
$str=trim($str);
$id = substr($str,0,(strpos($str,"</td>")));
// skip to where teh callsign is located
$str=removeTextBeforeEndOf($str,"target=\"QRZ.com\">");
// Extract the callsign
$str=trim($str);
$callAndName = substr($str,0,(strpos($str,"</a>")));
// skip to where the name is located
$str=removeTextBeforeEndOf($str,"CENTER>");
// Extract he name and append it to the callsign
$str=trim($str);
$callAndName = $callAndName." " . substr($str,0,(strpos($str,"</td>")));
switch($radio)
{
case "GD-77":
case "gd-77":
$rowStr = (($page*100) + $pcount + $offsetID)."," . $callAndName . "," . $id . ",Private Call,Off,None\n";
break;
default:
$rowStr = (($page*100) + $pcount + $offsetID)."," . $callAndName . "," . $id . "\n";
break;
}
echo $rowStr;
//$csvFile = $csvFile . $rowStr;
if ((($page*100) + $pcount + $offsetID) >= $limit)
{
$foundEnd=true;
break;
}
}
}
// Write to local file
// file_put_contents("contacts.csv",$csvFile);
?>
@rogerclarkmelbourne

This comment has been minimized.

Copy link
Owner Author

rogerclarkmelbourne commented Jan 11, 2018

Usage

Most active 500 VK callsigns, with output format for Radioddity GD-77

http://rogerclark.net/scripts/dmr-contacts.php?callsign=VK*&limit=500&radio=gd-77

Most active 100 French "F" stations, Format is just index, Callsign + name, ID

http://rogerclark.net/scripts/dmr-contacts.php?callsign=F*&limit=1000

@rogerclarkmelbourne

This comment has been minimized.

Copy link
Owner Author

rogerclarkmelbourne commented Jan 11, 2018

Note. The current code has a bug in it, caused by the pagination and the time to read each page via get_file_contents.

If a ID was one page 2 when the request for page 1 took place, but before page 2 is loaded, the same ID gets moved to page 1, it will not be on either page.

There seem to be some ways to make asynchronous calls to get files using CURL, however given how long it takes to render each page, I'm not convinced even that would be foolproof.

Probably the best way to work around this, is to build the list of callsigns <--> ID's once, then do it again, and check if any callsigns are in pass 1 and not in pass 2, in which case add those records to the list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.