File size: 4,303 Bytes
4cec618 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
from __future__ import print_function, unicode_literals, absolute_import, division
import sys
import re
import json
import requests
from bs4 import BeautifulSoup
from Levenshtein import distance
from tsammalexdata.util import DataProvider
def search_fuzzy(name):
"""
http://eol.org/search?q=Ammodaucus+leucothricus&search=Go
<div id='main'>
Did you mean: <a href="/search?q=ammodaucus+leucotrichus">Ammodaucus leucotrichus</a>, <a href="/search?q=ammodaucus+leucotrichus+coss.">Ammodaucus leucotrichus coss.</a>, <a href="/search?q=oreocereus+leucotrichus">Oreocereus leucotrichus</a>, <a href="/search?q=acrolophus+leucotricha">Acrolophus leucotricha</a>, <a href="/search?q=ammodiscus+catinus">Ammodiscus catinus</a>, <a href="/search?q=ammodiscus+excertus">Ammodiscus excertus</a> ?
<div class='filtered_search'>
"""
res = requests.get('http://eol.org/search', params=dict(q=name, search='Go'))
maindiv = BeautifulSoup(res.text).find('div', id='main')
candidate = None
if maindiv:
for a in maindiv.find_all('a'):
if a['href'].startswith('/search?q='):
if distance(name.decode('utf8'), a.text.strip()) < 3:
candidate = a.text
#print(a.text)
if candidate:
print(name.decode('utf8'), '->', candidate)
return candidate
class EOL(DataProvider):
host = 'eol.org'
def _path(self, name, id=None):
path = '1.0'
if id:
path += '/%s' % id
return 'api/%s/%s.json' % (name, path)
def _api(self, name, id=None, **kw):
try:
return self.get(self._path(name, id), **kw)
except ValueError:
return {}
def get_id(self, name):
res = self._api('search', q=name, page=1, exact='false')
if res.get('results'):
for result in res['results']:
if result['title'] == name:
return result['id']
return res['results'][0]['id']
def get_taxon_concept(self, data):
if data.get('taxonConcepts'):
# augment the data with complete classification info for one taxonomy:
for tc in data['taxonConcepts']:
if tc['nameAccordingTo'].startswith('Species 2000'):
# this is our preferred taxonomy ...
return tc
# ... but any will do :)
return data['taxonConcepts'][0]
def get_info(self, id):
"""Extract classification and synonym/common name data from EOL for a given taxon.
:param id: EOL id of a taxon.
:return: dict with information.
"""
kw = dict(
images=0,
videos=0,
sounds=0,
maps=0,
text=0,
iucn='true',
subjects='overview',
licenses='all',
details='true',
common_names='true',
synonyms='true',
references='false',
vetted=0)
data = self._api('pages', id, **kw)
if isinstance(data, list):
return {}
tc = self.get_taxon_concept(data)
if tc:
taxonomy = self._api('hierarchy_entries', tc['identifier'])
data.update(ancestors=taxonomy['ancestors'])
return data
def update(self, taxon, data):
for ancestor in data.get('ancestors', []):
if 'taxonRank' not in ancestor:
continue
for k in 'kingdom phylum class order family genus'.split():
if ancestor['taxonRank'] == k:
taxon[k] = ancestor['scientificName'].split()[0]
break
tc = self.get_taxon_concept(data)
if tc and 'taxonRank' in tc:
taxon['taxonRank'] = tc['taxonRank'].lower()
for vn in data.get('vernacularNames', []):
if vn.get('language') == 'en' and vn.get('eol_preferred'):
taxon['english_name'] = vn['vernacularName']
break
if __name__ == '__main__':
api = EOL()
#args = sys.argv[1:]
#if args:
# search_fuzzy(args[0])
if sys.argv[1:]:
if len(sys.argv[1:]) > 1:
api.refresh(*sys.argv[1:])
else:
print(json.dumps(api.cli(sys.argv[1]), indent=4))
|