Skip to content

Commit

Permalink
Don't write data for ended councils
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Sep 2, 2023
1 parent 2e046dd commit aba8d0a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: install
run: |
pip install boto3
pip install boto3 requests
- name: "Sync logbooks"
run: |
Expand Down
46 changes: 41 additions & 5 deletions write-data.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import json
from dataclasses import dataclass, field, asdict
from datetime import datetime, timedelta
from datetime import datetime, timedelta, date
from pathlib import Path
from typing import List
from urllib.parse import urljoin

import boto3
import requests

EE_BASE_URL = "https://elections.democracyclub.org.uk/"

client = boto3.client("codecommit", region_name="eu-west-2")

repos = [result["repositoryName"] for result in client.list_repositories()["repositories"]]
repos = [
result["repositoryName"] for result in client.list_repositories()["repositories"]
]

# LAST_N_RUNS = []
# for i in range(60, 0, -7):
Expand Down Expand Up @@ -61,7 +67,7 @@ def run_date(self):
def from_code_commit(cls: "LogRun", json_data):
data = json.loads(json_data)
status_code = None
if "status_code" in data:
if "status_code" in data:
status_code = data["status_code"]
return cls(
status_code=status_code,
Expand All @@ -76,14 +82,44 @@ def from_code_commit(cls: "LogRun", json_data):
logs: List[LogBook] = []


def make_ee_request(council_id: str) -> dict:
url = urljoin(EE_BASE_URL, f"/api/organisations/local-authority/{council_id}/")
results = requests.get(url).json()
return results["results"][0]


def council_past_end_date(council_id: str) -> bool:
"""
Check if the end date for the council is in the past, and if so, don't
add it to the report. We don't run scrapers for ended councils, so these
councils would just fill up the report with, probably, failing scrapers.
"""

metadata = make_ee_request(council_id)
end_date = metadata.get("end_date")
if not end_date:
return False
end_date_obj = datetime.strptime(end_date, '%Y-%m-%d').date()
if end_date_obj < date.today():
return True
return False

for repo in repos:
if repo in ["CouncillorsRepo", "test"]:
continue
print(repo)
if council_past_end_date(repo):
print(f"Skipping {repo} as it's end_date is in the past")
continue
try:
print(repo)
log_file = client.get_file(
filePath="Councillors/logbook.json",
repositoryName=repo,
)
except (client.exceptions.FileDoesNotExistException, client.exceptions.CommitDoesNotExistException):
except (
client.exceptions.FileDoesNotExistException,
client.exceptions.CommitDoesNotExistException,
):
log_file = {}

log_data = LogBook.from_codecommit(repo, log_file)
Expand Down

0 comments on commit aba8d0a

Please sign in to comment.