Skip to content

Commit

Permalink
Improve functionality to update legislator records
Browse files Browse the repository at this point in the history
  • Loading branch information
waldoj committed Jan 11, 2024
1 parent d4e6f1e commit 40ec6cd
Showing 1 changed file with 52 additions and 2 deletions.
54 changes: 52 additions & 2 deletions cron/legislators.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
*/
$log = new Log;

/*
* Instantiate Memcached
*/
if (MEMCACHED_SERVER != '')
{
$mc = new Memcached();
$mc->addServer(MEMCACHED_SERVER, MEMCACHED_PORT);
}

/*
* Use a DOM parser for the screen-scraper.
*/
Expand Down Expand Up @@ -128,6 +137,45 @@
*/
$import = new Import($log);

/*
* Update legislators' records periodically
*
* Select a handful of currently-serving legislators and update their records based on a re-query
* of LIS et al. To avoid abusing the legislature's website, we select a small subset of
* legislators to update each time, so it may be a few days before a given legislator's record
* picks up changes.
*/
$sql = 'SELECT
id,
name,
chamber,
lis_id
FROM representatives
WHERE
date_ended IS NULL
ORDER BY RAND()
LIMIT 10';
$stmt = $db->prepare($sql);
$stmt->execute();
$legislators = $stmt->fetchAll(PDO::FETCH_OBJ);

foreach ($legislators as &$legislator)
{

$data = $import->fetch_legislator_data($legislator->chamber, $legislator->lis_id);
$data['id'] = $legislator->id;
$import->update_legislator($data);

/*
* Remove this legislator's cached record
*/
if (MEMCACHED_SERVER != '')
{
$mc->delete('legislator-' . $legislator->id);
}

}

/*
* First see if we have records of any legislators who are no longer in office.
*/
Expand Down Expand Up @@ -213,7 +261,7 @@
{

$log->put('Found a new senator: ' . $name . ' ('
. 'https://lis.virginia.gov/cgi-bin/legp604.exe?' . SESSION_LIS_ID . '+mbr+S117'
. 'https://lis.virginia.gov/cgi-bin/legp604.exe?' . SESSION_LIS_ID . '+mbr+'
. $lis_id . ')', 6);

$data = $import->fetch_legislator_data('senate', $lis_id);
Expand Down Expand Up @@ -339,7 +387,7 @@
if ($errors == false)
{

$log->put('Successfully created a new record for ' . $data['name_formatted'], 4);
$log->put('All required data found for ' . $data['name_formatted'], 4);

/*
* If there's a photo URL, save it in a separate variable and remove it from the
Expand All @@ -357,6 +405,8 @@
continue;
}

$log->put('Added ' . $data['name_formatted'] . ' to the database', 4);

$photo_success = $import->fetch_photo($photo_url, $data['shortname']);
if ($photo_success == false)
{
Expand Down

0 comments on commit 40ec6cd

Please sign in to comment.