Skip to content

Commit

Permalink
to import 2024 candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
kiang committed Nov 24, 2023
1 parent a0ab029 commit 5dc3fe1
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions Console/Command/CandidateShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,46 @@ class CandidateShell extends AppShell

public function main()
{
$this->import_2022_result();
$this->import_2024();
}

public function import_2024()
{
$fh = fopen('https://github.com/kiang/vote2024/raw/master/data/candidates.csv', 'r');
$head = fgetcsv($fh, 2048);
while ($line = fgetcsv($fh, 2048)) {
$candidate = $this->Candidate->find('first', [
'conditions' => [
'Candidate.election_id' => $line[7],
'Candidate.name' => $line[2],
],
]);
$gender = ($line[3] === '') ? 'm' : 'f';
if (!empty($candidate)) {
$this->Candidate->id = $candidate['Candidate']['id'];
$this->Candidate->save(['Candidate' => [
'election_id' => $line[7],
'name' => $line[2],
'party' => $line[4],
'gender' => $gender,
'stage' => 1,
]]);
} else {
$this->Candidate->create();
$this->Candidate->save(['Candidate' => [
'election_id' => $line[7],
'name' => $line[2],
'party' => $line[4],
'gender' => $gender,
'stage' => 1,
'education_level' => '',
'is_present' => 0,
'name_english' => '',
'birth_place' => '',
'platform' => '',
]]);
}
}
}

public function import_2022_result()
Expand Down Expand Up @@ -109,41 +148,6 @@ public function import_2022_number()
}
}

public function import_2022()
{
$fh = fopen('https://github.com/kiang/vote2022/raw/master/reports/links.csv', 'r');
while ($line = fgetcsv($fh, 2048)) {
$candidate = $this->Candidate->find('first', [
'conditions' => [
'Candidate.election_id' => $line[4],
'Candidate.name' => $line[2],
],
]);
if (!empty($candidate)) {
$this->Candidate->id = $candidate['Candidate']['id'];
$this->Candidate->save(['Candidate' => [
'election_id' => $line[4],
'name' => $line[2],
'party' => $line[3],
'stage' => 1,
]]);
} else {
$this->Candidate->create();
$this->Candidate->save(['Candidate' => [
'election_id' => $line[4],
'name' => $line[2],
'party' => $line[3],
'stage' => 1,
'education_level' => '',
'is_present' => 0,
'name_english' => '',
'birth_place' => '',
'platform' => '',
]]);
}
}
}

public function elections_2020()
{
$children = $this->Candidate->Election->children('5cbf16f4-d818-447e-b916-5ee30a8c0003');
Expand Down

0 comments on commit 5dc3fe1

Please sign in to comment.