"""This is a file download program via sakura.io."""
# -*- coding: utf-8 -*-
import sys
import time
#import struct
from datetime import datetime
from sakuraio.hardware.rpi import SakuraIOGPIO
#from sakuraio.hardware.rpi import SakuraIOSMBus

FILEID = 1
FILENAME = "downloaded_file"

def get_file_via_sakuraio(channel):
    """ Single file downlod function. """
    CHUNK_SIZE = [16]
    DELAY_SEC = 1
    filesize = 0
    receivedsize = 0
    filedata = []

    # インスタンス作成
    sakuraio = SakuraIOGPIO()
    #sakuraio = SakuraIOSMBus

    # 一連のファイルダウンロード処理を開始(センター側でダウンロードファイルを取得)
    sakuraio.start_file_download([channel])

    time.sleep(DELAY_SEC)

    # メタデータ(ファイルサイズ等)を取得
    try:
        response = sakuraio.get_file_metadata()
    except:
        #sakuraio.cancel_file_download()
        #del sakuraio
        #sys.exit()
        raise
    else:
        print('Get_file_metadata: Status: {0:x}'.format(response["status"]))
        if response["status"] == 0x81 or response["status"] == 0x82:
            sakuraio.cancel_file_download()
            del sakuraio
            sys.exit()

        filesize = response["size"]
        print('Get_file_metadata: Filesize: {0}'.format(filesize))
        print('Get_file_metadata: Timestamp: {0}'.format(datetime.utcfromtimestamp(response["timestamp"]/1000)))
    
    # ファイルの実体を取得
    while receivedsize < filesize:
        time.sleep(DELAY_SEC)
        try:
            result = sakuraio.get_file_data(CHUNK_SIZE)
        except:
            #sakuraio.cancel_file_download()
            #del sakuraio
            #sys.exit()
            raise
        else:
            filedata.extend(result["data"])
            receivedsize += len(result["data"])
            print('Get_file_data: Length: {0}'.format(len(result["data"])))
            print('Get_file_data: Receivedsize: {0}'.format(receivedsize))
        '''
        try:
            response = sakuraio.get_file_download_status()
        except:
            print('Get_file_download_status: Error')
            raise
        else:
            print('Get_file_download_status: Status: {0:x}'.format(response["status"]))
            print('Get_file_download_status: Receivedsize: {0}'.format(response["size"]))
        '''

    #print('Get_file_data: Received_data: {0}'.format(filedata))

    del sakuraio

    return filedata

if __name__ == "__main__":

    # ファイルIDを指定してファイル内容をダウンロード
    content = []
    content = get_file_via_sakuraio(FILEID)

    # 取得したファイルの中身を、指定したファイル名で書き込み
    with open(FILENAME, "wb") as fout:
        fout.write(bytearray(content))

@@ -1846,383 +1846,8 @@
 ze))
-%0A        '''%0A        try:%0A            response = sakuraio.get_file_download_status()%0A        except:%0A            print('Get_file_download_status: Error')%0A            raise%0A        else:%0A            print('Get_file_download_status: Status: %7B0:x%7D'.format(response%5B%22status%22%5D))%0A            print('Get_file_download_status: Receivedsize: %7B0%7D'.format(response%5B%22size%22%5D))%0A        '''
 %0A%0A  

delete unnecessary code/comment