-
Notifications
You must be signed in to change notification settings - Fork 6
Parsers
Drew Winstel edited this page Jun 9, 2019
·
2 revisions
Parsers are a way of taking the data provided by the various API providers and converting them into our format. Some are user-friendly (e.g. they give us raw JSON), but some are not so nice (HTML embedded within a JS object).
All parsers inherit from the BaseTapListProvider class in
tap_list_providers/base.py
.
To make your own, you need to:
- Declare a string that identifies your provider, e.g.
mybeer
. - In
venues/models.py
, edit theTAP_LIST_PROVIDERS
attribute of theVenue
class to add an entry for your provider in the form (internal_name
,Friendly display name
). - If necessary, add fields to the
VenueAPIConfiguration
model that detail what configuration data is required for your provider (URL, API key, etc.). - Create a migration for both of the above:
pipenv run ./manage.py makemigrations
- Create a file named after your provider in
tap_list_providers/parsers/
. - Your class should inherit from BaseTapListProvider and define the class-level
(not instance-level) attribute
provider_name
. That attribute must match theinternal_name
from step 2. - You need to implement the method
def handle_venue(self, venue)
. From there, iterate over the rooms in that venue and fill in data from what you get from the API provider. - Utilize the
get_beer
andget_manufacturer
methods from the base class to simplify beer and manufacturer lookup, respectively. Those will automatically create the beers/manufacturers if no match is found (and eventually put them in a moderation queue). - If your provider has some sort of unique beer/manufacturer ID (or URL):
- Add that field to the beer/manufacturer model and create a migration for it.
- Create a migration for that new field.
- For beer fields, add that field to the
unique_fields
local variable in theget_beer()
method of the base class. - For manufacturer fields, either implement a special case as in the
if untappd_url
block of theget_manufacturer()
method or (preferably) refactor the method to use the similarunique_fields
logic as inget_beer()
and then update these docs accordingly.
- Create a management command to parse all venues for your provider:
- Create a file called
parse<myprovider>.py
intap_list_providers/management/commands/
. - Copy and paste the logic from one of the other files in that directory and change the references to refer to your code.
- Save example data in
tap_list_providers/example_data
. This should be the same data you would get from making the HTTP call to get your data. This way you can use theresponses
library to fake the result of the HTTP call. - Create test code in
tap_list_providers/test/
that parses real data (calling the management command) and validates a few of the taps to make sure you get the right example data. - Schedule your task:
- Open http://localhost:8000/admin/ and under Periodic Tasks, select
the
Add
link next to Periodic Tasks. - Fields to fill in:
- Name:
Parse <my provider>
- Task (registered):
tap_list_providers.tasks.parse_provider
- Description:
Update all venues from <my provider>
- Schedule block:
- Crontab: click the green plus icon
- Set minute to some value that is not currently used (0, 15, 30, 45)
- Leave the rest as defaults
- Click Save
- Crontab: click the green plus icon
- Click
show
next toArguments
. - Set the arguments block to
["internal_name"]
(double quotes are essential) 3. ClickSAVE
- Name:
- Export the updated schedule:
pipenv run ./manage.py dumpdata --indent 2 -o tap_list_providers/fixtures/scheduled_tasks.json django_celery_beat.solarschedule django_celery_beat.periodictasks django_celery_beat.periodictask django_celery_beat.intervalschedule django_celery_beat.crontabschedule
- Commit your changes and create a PR!