#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Author: Dongdong Tian @ USTC
#
# Revision History:
#   2014-08-31  Dongdong Tian   Initial Coding
#

"""Request arrival time data or focal mechanism catalog from Hi-net.

Usage:
    HinetJMARequest.py (--measure | --mecha) <yyyymmdd> <span> [--os=OS]
    HinetJMARequest.py -h

Options:
    -h --help     Show this help.
    --measure     Request arrival time data.
    --mecha       Request focal mechanism catalog.
    --os=OS       Line break format, choose from DOS or UNIX. [default: DOS]

"""
import sys
import configparser

import requests
from docopt import docopt

# specify user name and password
config = configparser.ConfigParser()
config.read("Hinet.cfg")
user = config['Account']['User']
passwd = config['Account']['Password']

# base url for continuous waveform data
base = "http://www.hinet.bosai.go.jp/REGS/JMA/"


def download(url, params):
    d = requests.get(url, params=params, auth=(user, passwd), stream=True)
    if d.status_code == 401:
        print("Unauthorized.")
        sys.exit()

    # file size
    size = int(d.headers['Content-Length'].strip())
    # file name
    disposition = d.headers['Content-Disposition'].strip()
    fname = disposition.split('filename=')[1].strip('\'"')

    with open(fname, "wb") as fd:
        self = 0
        for chunk in d.iter_content(chunk_size=1024):
            if chunk:  # filter out keep-alive new chunks
                fd.write(chunk)
                fd.flush()
            self += len(chunk)


if __name__ == '__main__':
    arguments = docopt(__doc__)

    if arguments['--measure']:
        data = "measure"
    elif arguments['--mecha']:
        data = "mecha"

    os = arguments['--os'][0:1]

    url = base + "dlDialogue.php"
    params = {
        "data": data,
        "rtm": arguments['<yyyymmdd>'],
        "span": arguments['<span>'],
        "os": arguments['--os'][:1],
    }
    download(url, params)

@@ -143,16 +143,72 @@
 Coding%0A#
+   2014-12-03  Dongdong Tian   Update to Hinet V141201%0A#
 %0A%0A%22%22%22Req
@@ -773,14 +773,31 @@
 g%22)%0A
-user =
+auth = %7B%0A    'auth_un':
  con
@@ -822,17 +822,24 @@
 er'%5D
-%0Apasswd =
+,%0A    'auth_pw':
  con
@@ -864,16 +864,23 @@
 ssword'%5D
+,%0A    %7D
 %0A%0A# base
@@ -929,20 +929,22 @@
 http
+s
 ://
-www.
 hinet
+www11
 .bos
@@ -956,12 +956,12 @@
 .jp/
-REGS
+auth
 /JMA
@@ -969,648 +969,8 @@
 %22%0A%0A%0A
-def download(url, params):%0A    d = requests.get(url, params=params, auth=(user, passwd), stream=True)%0A    if d.status_code == 401:%0A        print(%22Unauthorized.%22)%0A        sys.exit()%0A%0A    # file size%0A    size = int(d.headers%5B'Content-Length'%5D.strip())%0A    # file name%0A    disposition = d.headers%5B'Content-Disposition'%5D.strip()%0A    fname = disposition.split('filename=')%5B1%5D.strip('%5C'%22')%0A%0A    with open(fname, %22wb%22) as fd:%0A        self = 0%0A        for chunk in d.iter_content(chunk_size=1024):%0A            if chunk:  # filter out keep-alive new chunks%0A                fd.write(chunk)%0A                fd.flush()%0A            self += len(chunk)%0A%0A%0A
 if _
@@ -1023,16 +1023,65 @@
 __doc__)
+%0A    requests.packages.urllib3.disable_warnings()
 %0A%0A    if
@@ -1185,16 +1185,81 @@
 mecha%22%0A%0A
+    rtm = arguments%5B'%3Cyyyymmdd%3E'%5D%0A    span = arguments%5B'%3Cspan%3E'%5D%0A
     os =
@@ -1373,135 +1373,641 @@
 m%22: 
-arguments%5B'%3Cyyyymmdd%3E'%5D,%0A        %22span%22: arguments%5B'%3Cspan%3E'%5D,%0A        %22os%22: arguments%5B'--os'%5D%5B:1%5D,%0A    %7D%0A    download(url, params
+rtm,%0A        %22span%22: span,%0A        %22os%22: os,%0A    %7D%0A%0A    d = requests.post(url, params=params, data=auth, verify=False, stream=True)%0A    if d.status_code == requests.codes.found:%0A        print(%22Maybe unauthorized. Check your username and password.%22)%0A        sys.exit()%0A%0A    # file size%0A    size = int(d.headers%5B'Content-Length'%5D.strip())%0A    # file name%0A    print(data, rtm, span)%0A    fname = %22%7B%7D_%7B%7D_%7B%7D.txt%22.format(data, rtm, span)%0A%0A    with open(fname, %22wb%22) as fd:%0A        for chunk in d.iter_content(chunk_size=1024):%0A            if chunk:  # filter out keep-alive new chunks%0A                fd.write(chunk)%0A                fd.flush(
 )%0A

update for hinet v141201