# -*- coding: utf-8 -*-

import requests
import datetime
from pyquery import PyQuery as pq
# import cx_Oracle
import urllib3
urllib3.disable_warnings()
# conn = cx_Oracle.connect('cis', 'cis_zzsn9988', '114.116.91.1:1521/ORCL')
# cursor = conn.cursor()
result_list1 = [
    [
        '人民币',
        'CNY'],
    [
        '美元',
        'USD'],
    [
        '欧元',
        'EUR'],
    [
        '瑞士法郎',
        'CHF'],
    [
        '加元',
        'CAD'],
    [
        '波兰兹罗提',
        'PLN'],
    [
        '英镑',
        'GBP'],
    [
        '澳元',
        'AUD'],
    [
        '泰铢',
        'THB'],
    [
        '沙特里亚尔',
        'SAR'],
    [
        '巴西里亚伊',
        'BRL'],
    [
        '新土耳其新里拉',
        'TRY'],
    [
        '新台币',
        'TWD'],
    [
        '印度卢比',
        'INR'],
    [
        '墨西哥比索',
        'MXN'],
    [
        '日元',
        'JPY'],
    [
        '瑞典克朗',
        'SEK'],
    [
        '韩元',
        'KRW'],
    [
        '俄罗斯卢布',
        'RUB'],
    [
        '新加坡元',
        'SGD'],
    [
        '港币',
        'HKD']]
result_list2 = [
    'USD',
    'CNY']
headers = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'zh-CN,zh;q=0.9',
    'Cache-Control':'no-cache',
    'Connection':'keep-alive',
    'Cookie':'ASPSESSIONIDQQRTBSSA=CJNLDGJALHNJGKBKPEBCDCOE; Hm_lvt_ecdd6f3afaa488ece3938bcdbb89e8da=1692951904; Hm_lpvt_ecdd6f3afaa488ece3938bcdbb89e8da=1692951904',
    'Host':'qq.ip138.com',
    'Pragma':'no-cache',
    'Sec-Fetch-Dest':'document',
    'Sec-Fetch-Mode':'navigate',
    'Sec-Fetch-Site':'none',
    'Sec-Fetch-User':'?1',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    'sec-ch-ua':'"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"'
     }
for result1 in result_list1:
    currency_name = result1[0]
    currency = result1[1]
    to_USD = ''
    to_CNY = ''
    for i in range(len(result_list2)):
        result2 = result_list2[i]
        # https://qq.ip138.com/hl.asp?from=CNY&to=USD&q=1
        # url = f'''https://qq.ip138.com/hl.asp?from={currency}&to={result2}&q=1'''
        # res = requests.get(url, headers,verify=False)
        res = requests.get('https://qq.ip138.com/hl.asp?from=CNY&to=USD&q=1')
        resp_text=res.content
        doc_resp = pq(resp_text)
        money = doc_resp('table tr:nth-child(3) td:nth-child(3)').text()
        if money == '1':
            money_result = money
        else:
            try:
                money_result = round(float(money), 4)
            except:
                continue
        if i == 0:
            to_USD = money_result
        else:
            to_CNY = money_result
    now = datetime.datetime.now()
    now_time = now.strftime('%Y-%m-%d')
    if to_USD == '' or to_CNY == '':
        continue
    result_dict = {
        '币种': currency_name,
        '币简称': currency,
        '对美元': to_USD,
        '对人民币': to_CNY,
        '更新时间': now_time }
    print(result_dict)
    # sql = "insert into CIS_QCC_CURRENCY_RATE(ID, CURRENCY_NAME, CURRENCY_CODE, RATE_TO_USD, RATE_TO_CNY,CREATE_DATE) values (SEQ_CIS_QCC_CURRENCY_RATE.nextval,'{0}','{1}','{2}','{3}','{4}')".format(currency_name, currency, to_USD, to_CNY, now_time)
    # cursor.execute(sql)
    # conn.commit()

