Skip to content

Instantly share code, notes, and snippets.

@kaorun343
Created May 10, 2017 02:22
Show Gist options
  • Save kaorun343/833eb2fc7cf4c09a095db13ff191db1f to your computer and use it in GitHub Desktop.
Save kaorun343/833eb2fc7cf4c09a095db13ff191db1f to your computer and use it in GitHub Desktop.
OMIMにアクセス
import json
import urllib.parse
import urllib.request
class OMIM:
def __init__(self, api_key):
self.api_key = api_key
self.base_url = "http://api.omim.org/api/"
def entry(self, mim_number, params = {}):
values = {
"apiKey": self.api_key,
"mimNumber": mim_number
}
values.update(params)
data = urllib.parse.urlencode(values)
print(data)
def entry_search(self, search, params = {}):
limit = 20
start_index = 0
values = {
"apiKey": self.api_key,
"search": search,
"format": "json",
"limit": limit
}
values.update(params)
def access(values, start):
values["start"] = start
url_values = urllib.parse.urlencode(values)
full_url = self.base_url + "entry/search?" + url_values
with urllib.request.urlopen(full_url) as response:
result = json.loads(response.read())["omim"]["searchResponse"]
return (result, result["startIndex"], result["totalResults"])
(result, start_index, total_results) = access(values, start_index)
yield result
while start_index < total_results:
start_index += limit
(result, start_index, total_results) = access(values, start_index)
yield result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment