class Fetcher:
    def __init__(self):
        openid = input('Your OpenID (like http://example.myid.net/) : ')
        print('''Go to following URL and get the user key:
https://api.openmaru.com/delegate_key/springnote?app_id=71fcb7c8'''
            '&openid={}'.format(openid))
        key = input('Key: ')
        print('Connecting...')
        self.api = Springnote(openid, key)

    SAVE_PATH = '_api'

    def fetch(self, path):
        assert '..' not in path
        cache_path = os.path.normpath(self.SAVE_PATH + path)
        if os.access(cache_path, os.F_OK):
            return open(cache_path, 'rb').read()
        makedirs(os.path.split(cache_path)[0], exist_ok=True)
        try:
            data = self.api._fetch('GET', path)
        except SpringnoteException as ex:
            if ex.status in [404]:
                return None
            raise
        open(cache_path, 'wb+').write(data)
        return data
class Fetcher:
    def __init__(self):
        openid = input("Your OpenID (e.g. http://example.myid.net/) : ")
        print("""다음 주소로 가서 API 키를 받으십시오.
Go to following URL and get the user key:
https://api.openmaru.com/delegate_key/springnote?app_id=71fcb7c8"""
            '&openid={}'.format(openid))
        key = input("Key: ")
        print("질문 하나만 더 받으면 됩니다. 접속될 때까지 채널 고정!")
        print("Connecting. Please wait...")
        self.api = Springnote(openid, key)
        print("Getting information...")
        pages = json.loads(
            self.api._fetch('GET', '/pages.json').decode('ascii'))
        self.subdomain = re.search(r'(\w+).springnote.com',
                                   pages[0]['page']['uri']).group(1)

    def fetch(self, path, force=False):
        assert '..' not in path
        cache_path, _, _ = path.partition('?')
        cache_path = os.path.normpath(
            os.path.join(SAVE_PATH, self.subdomain) + cache_path)
        if '?' in path:
            path += '&domain={}'.format(self.subdomain)
        else:
            path += '?domain={}'.format(self.subdomain)
        if not force and os.access(cache_path, os.F_OK):
            return open(cache_path, 'rb').read()
        makedirs(os.path.split(cache_path)[0], exist_ok=True)
        try:
            data = self.api._fetch('GET', path)
        except SpringnoteException as ex:
            if ex.status in [403, 404]:
                return None
            raise
        open(cache_path, 'wb+').write(data)
        return data

    def fetch_data(self, path, force=False):
        return json.loads(self.fetch(path, force=force).decode('ascii'))