-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from zooniverse/inaturalist
Add iNaturalist class & import method
- Loading branch information
Showing
4 changed files
with
109 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from panoptes_client.panoptes import Panoptes | ||
|
||
|
||
class Inaturalist(object): | ||
""" | ||
The class that interacts with the Panoptes' iNaturalist functionality. | ||
Currently, this includes a single route that allows the importing of | ||
iNaturalist Observations as Zooniverse Subjects. | ||
""" | ||
|
||
def inat_import( | ||
taxon_id, | ||
subject_set_id, | ||
updated_since=None | ||
): | ||
""" | ||
Begins an import of iNaturalist Observations as Zooniverse Subjects. | ||
Response is a 200 if Panoptes begins the import successfully. | ||
Requires owner or collaborator access to the subject set's linked project. | ||
Takes three arguments: | ||
taxon_id: the iNat taxon ID of a particular species | ||
subject_set_id: the Zoo subject set id subjects should be imported into. | ||
Updated observations will upsert their respective subjects. | ||
updated_since: a date range limiter on the iNat Observations query. | ||
Warning: defaults to None and will import ALL Observations | ||
by default. This will likely be a lot and take a while. | ||
Examples:: | ||
# Import gray squirrel observations updated during or after Halloween 2022 to subject set id 3: | ||
Inaturalist.inat_import(46017, 3, '2022-10-31') | ||
# Import all royal flycatcher observations to subject set id 4: | ||
Inaturalist.inat_import(16462, 4) | ||
""" | ||
|
||
return Panoptes.client().post( | ||
f'/inaturalist/import', | ||
json={ | ||
'taxon_id': taxon_id, | ||
'subject_set_id': subject_set_id, | ||
'updated_since': updated_since | ||
} | ||
) |
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,42 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import unittest | ||
import sys | ||
|
||
if sys.version_info <= (3, 0): | ||
from mock import patch, Mock | ||
else: | ||
from unittest.mock import patch, Mock | ||
|
||
from panoptes_client.inaturalist import Inaturalist | ||
|
||
|
||
class TestInaturalist(unittest.TestCase): | ||
|
||
def test_inat_import(self): | ||
with patch('panoptes_client.panoptes.Panoptes.client') as pc: | ||
pc().post = Mock(return_value=200) | ||
Inaturalist.inat_import(16462, 4) | ||
|
||
pc().post.assert_called_with( | ||
'/inaturalist/import', | ||
json={ | ||
'taxon_id': 16462, | ||
'subject_set_id': 4, | ||
'updated_since': None | ||
} | ||
) | ||
|
||
def test_inat_import_updated_since(self): | ||
with patch('panoptes_client.panoptes.Panoptes.client') as pc: | ||
pc().post = Mock(return_value=200) | ||
Inaturalist.inat_import(16462, 4, '2022-10-31') | ||
|
||
pc().post.assert_called_with( | ||
'/inaturalist/import', | ||
json={ | ||
'taxon_id': 16462, | ||
'subject_set_id': 4, | ||
'updated_since': '2022-10-31' | ||
} | ||
) |