Created
May 10, 2017 02:22
-
-
Save kaorun343/833eb2fc7cf4c09a095db13ff191db1f to your computer and use it in GitHub Desktop.
OMIMにアクセス
This file contains hidden or 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
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