####################################use for all ,not use in crontab

import os
from typing import Optional
from dataset.util import ResultIter
from datetime import tzinfo
import datetime
import mysql.connector
from mysql.connector import Error
import dataset
import json
from pytube import extract
import requests
import json 

hhhMBPath = '../hhh-home-mb'
hhhPCPath = '../hhh-home-pc'

#connstr = 'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4'
#2022/12/13 change
connstr = 'mysql://hhh7796hhh:lYmWsu^ujcA1@ec2-3-35-26-49.ap-northeast-2.compute.amazonaws.com:3306/xoops?charset=utf8mb4'

""" db = dataset.connect(
    'mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4') """
#        charset='utf8',
#        collation='utf8_unicode_ci'

#2022/12/13 change
connobj = {
    'host': 'ec2-3-35-26-49.ap-northeast-2.compute.amazonaws.com',
    'database': 'xoops',
   'user': 'hhh7796hhh',
   'password': 'lYmWsu^ujcA1',
    'use_unicode': True   
    #'charset': 'utf8',
    #'collation': 'utf8_unicode_ci'

}

 #connobj = {
    # 'host': 'hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com',
     #'database': 'xoops',
     #'user': 'hhh7796hhh',
     #'password': 'lYmWsu^ujcA1',
     #'use_unicode': True
    ##'charset': 'utf8',
     ##'collation': 'utf8_unicode_ci'
 #}

# def ExecuteQuery(isql):
#     with mysql.connector.connect(**connobj) as connection :
#         with connection.cursor(dictionary=True) as cursor:
#     # connection.set_charset_collation('utf8','utf8_general_ci')
#             #cursor = connection.cursor(dictionary=True)
#             cursor.execute(isql)
#             return cursor.fetchall()

# records = ExecuteQuery("SELECT hcolumn_id FROM _hcolumn where onoff=1")
# list_use = []
# for i in records:
#     list_use.append(i['hcolumn_id'])
# #print(list_use)
# for j in list_use:
while True:
    try:
        a=requests.get('https://m3.hhh.com.tw:18673/case_search?page=1&page_size=18&order_by=new')
        break
    except:
        print('bad:',j)
        time.sleep(5)

d=a.json()
print(d)
with open('../cases/lists/json/cases_lists_data.json','w+',encoding="utf-8") as f:
    json.dump(d, f)

cmd="""curl -X POST "https://api.cloudflare.com/client/v4/zones/8cb58022ad5743cfd9f088d5e3a261eb/purge_cache" -H "X-Auth-Email: web.dept.hhh@gmail.com"  -H "X-Auth-Key:63e51d9a1638d66afcbfc3320aabec52304de" -H "Content-Type: application/json" --data '{"files":["https://hhh.com.tw/hhh-test/cases/lists/json/cases_lists_data.json"]}'"""
print(cmd)
os.system(cmd)
    #if str(j)=='5':
    #    break