-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update-requirements
- Loading branch information
Showing
13 changed files
with
208 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Lint and Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- update-requirements | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: Set up Python 3.7 | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: 3.7 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
pip install -r dev_requirements.txt | ||
- name: Lint with flake8 | ||
run: | | ||
# stop the build if there are Python syntax errors or undefined names | ||
flake8 . --count --ignore E501,W503,E203 --show-source --statistics | ||
- name: Lint with Black | ||
run: | | ||
black . | ||
- name: Test with django | ||
run: | | ||
python manage.py test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
apps/civic_pulse/management/commands/create_scraper_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Idempotent management command to create the scraper user with a DRF token | ||
""" | ||
from django.core.management.base import BaseCommand | ||
from django.contrib.auth.models import User | ||
from rest_framework.authtoken.models import Token | ||
|
||
SCRAPER_USERNAME = "scraper" | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Get or create a scraper user with a Django REST Framework token" | ||
|
||
def add_arguments(self, parser): | ||
pass | ||
|
||
def handle(self, *args, **options): | ||
user, created = User.objects.get_or_create(username=SCRAPER_USERNAME) | ||
user.save() | ||
|
||
if created: | ||
self.stdout.write(f"Created new user with username {SCRAPER_USERNAME}") | ||
else: | ||
self.stdout.write(f"User {SCRAPER_USERNAME} already exists.") | ||
|
||
token, created = Token.objects.get_or_create(user=user) | ||
self.stdout.write(f"The token for the user {SCRAPER_USERNAME} is {token}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
black | ||
flake8 | ||
coloredlogs==10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ simplejson==3.16.0 | |
sqlparse==0.3.0 | ||
urllib3==1.24.2 | ||
apscheduler==3.6.0 | ||
python-dotenv==0.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
import os | ||
import logging | ||
|
||
import requests | ||
|
||
from . import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AgencyApiService: | ||
def __init__(self): | ||
# If environment variable is set, we use the corresponding api(usually local). otherwise govlens api | ||
if os.environ.get("govlens_api", None) is None: | ||
self.base_url = ( | ||
"http://govlens.us-east-2.elasticbeanstalk.com/api/agencies/" | ||
) | ||
else: | ||
self.base_url = os.environ["govlens_api"] | ||
self.base_url = settings.GOVLENS_API_ENDPOINT | ||
|
||
def get_all_agencies(self): | ||
try: | ||
all_agency_list = self._get(self.base_url) | ||
return all_agency_list | ||
except Exception as ex: | ||
print(f"Error while retrieving all the agency information: {str(ex)}") | ||
logger.error(ex, "Error while retrieving all the agency information") | ||
|
||
def _get(self, url): | ||
response = requests.get(url, headers={"Content-type": "application/json"}) | ||
response = requests.get( | ||
url, | ||
headers={ | ||
"Content-type": "application/json", | ||
"Authorization": "Token {}".format(settings.GOVLENS_API_TOKEN), | ||
}, | ||
) | ||
response.raise_for_status() | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,30 @@ | ||
import os | ||
import logging | ||
from .process_agency_info import AgencyInfo | ||
from .agency_api_service import AgencyApiService | ||
|
||
from . import settings | ||
|
||
settings.setup_logging() | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# method invoked by lambda | ||
def scrape_data(event, context=None): | ||
agencies = event["agencies"] | ||
if event.get("agencies", None) is None or len(agencies) <= 0: | ||
print("No Agency information was passed to scrape") | ||
logger.warning("No Agency information was passed to scrape") | ||
return | ||
|
||
for agency in agencies: | ||
agency_instance = AgencyInfo(agency) | ||
agency_instance.process_agency_info() | ||
|
||
|
||
# if running from local, we get the list of agencies and scrape one by one. | ||
if __name__ == "__main__": | ||
# If running from local, set the environment variable to your local | ||
logging.basicConfig( | ||
filename="Scraper_Errors.log", | ||
level=logging.ERROR, | ||
format="%(asctime)s %(message)s", | ||
) | ||
os.environ[ | ||
"govlens_api" | ||
] = "http://govlens.us-east-2.elasticbeanstalk.com/api/agencies/" | ||
os.environ["GOOGLE_API_KEY"] = "" | ||
|
||
agency_api_service = AgencyApiService() | ||
agencies = agency_api_service.get_all_agencies() | ||
event = {"agencies": agencies} | ||
scrape_data(event) | ||
print("SCRAPED") | ||
logger.info("Finished scraping") |
Oops, something went wrong.