#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: Dongdong Tian @ USTC
#
# Revision History:
#   2014-12-05  Dongdong Tian   Initial Coding
#
import os
import shutil
import logging
import configparser

import requests

AUTH = "https://hinetwww11.bosai.go.jp/auth/"

def auth_check(user, passwd):
    ''' check authentication '''

    auth = {
        'auth_un': user,
        'auth_pw': passwd,
        }

    try:
        r = requests.post(AUTH, data=auth, verify=False,
                          allow_redirects=False, timeout=20)
    except requests.exceptions.ConnectTimeout:
        logging.error("ConnectTimeout in 20 seconds.")
        sys.exit()
    except requests.exceptions.ConnectionError:
        logging.error("Name or service not known")
        sys.exit()

    if r.status_code == requests.codes.ok:  # succeed
        logging.info("Username and Password is right.")
    elif r.status_code == requests.codes.found:  # redirect
        logging.error("Maybe unauthorized. Check your username and password!")
        sys.exit()
    else:
        logging.warning("Status code: {}".format(status_code))
        logging.warning("Report this status code to seisman.info@gmail.com")


def cmd_exists(cmd):
    ''' check if a cmd exists '''

    if shutil.which(cmd):
        logging.info("%s in your PATH.", cmd)
    else:
        logging.error("%s not in your PATH or not executable.", cmd)
        sys.exit()


if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(levelname)-7s %(message)s',
                        datefmt='%H:%M:%S')
    logging.getLogger("requests").setLevel(logging.WARNING)
    requests.packages.urllib3.disable_warnings()
    config = configparser.ConfigParser()
    config.read('Hinet.cfg')

    user = config['Account']['User']
    passwd = config['Account']['Password']
    auth_check(user, passwd)

    catwin32 = os.path.expanduser(config['Tools']['catwin32'])
    cmd_exists(catwin32)

@@ -153,16 +153,37 @@
 port os%0A
+import re%0Aimport sys%0A
 import s
@@ -287,16 +287,48 @@
 /auth/%22%0A
+CONT = AUTH + %22download/cont/%22%0A%0A
 %0Adef aut
@@ -335,28 +335,20 @@
 h_check(
-user, passwd
+auth
 ):%0A    '
@@ -380,84 +380,8 @@
 ''%0A%0A
-    auth = %7B%0A        'auth_un': user,%0A        'auth_pw': passwd,%0A        %7D%0A%0A
     
@@ -1391,16 +1391,655 @@
 xit()%0A%0A%0A
+def check_version(auth):%0A%0A    try:%0A        r = requests.post(CONT, data=auth, verify=False,%0A                          allow_redirects=False, timeout=20)%0A    except requests.exceptions.ConnectTimeout:%0A        logging.error(%22ConnectTimeout in 20 seconds.%22)%0A        sys.exit()%0A    except requests.exceptions.ConnectionError:%0A        logging.error(%22Name or service not known%22)%0A        sys.exit()%0A%0A    version = re.search(r'cont%5C.js%5C?(?P%3CVER%3E%5Cd%7B6%7D)', r.text).group('VER')%0A%0A    if version != '141201':%0A        logging.warning(%22Hi-net website seems to have been updated. %22%0A                        %22These scripts may be working or not working%22)%0A%0A%0A
 if __nam
@@ -2407,14 +2407,35 @@
     
-user =
+auth = %7B%0A        'auth_un':
  con
@@ -2460,21 +2460,28 @@
 er'%5D
+,
 %0A    
-passwd =
+    'auth_pw':
  con
@@ -2506,16 +2506,27 @@
 ssword'%5D
+,%0A        %7D
 %0A    aut
@@ -2537,20 +2537,37 @@
 eck(
-user, passwd
+auth)%0A%0A    check_version(auth
 )%0A%0A 

check hinet website updates