rainfall_conditions_job.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from datetime import datetime, timedelta
  4. from sqlalchemy.orm import Session
  5. from common.BigDataCenterAPI import *
  6. from utils import *
  7. from models import *
  8. from exceptions import *
  9. from database import get_db_local
  10. from extensions import logger
  11. import pymysql
  12. from config import settings
  13. def get_stcd_data(stcd=''):
  14. # 获取当前时间
  15. now = datetime.now()
  16. # 计算 24 小时前的时间
  17. twenty_four_hours_ago = now - timedelta(hours=24)
  18. formatted_now = now.strftime('%Y-%m-%d %H')
  19. formatted_twenty_four_hours_ago = twenty_four_hours_ago.strftime('%Y-%m-%d %H')
  20. url = 'https://19.15.75.180:8581/GatewayMsg/http/api/proxy/invoke'
  21. service_code = 'P0138'
  22. passid = 'C90-44004428'
  23. passtoken = 'f539f616b2834160bc47fda5f298512c'
  24. signTime = str(GetTime() // 1000)
  25. nonce = GetNonce(5)
  26. sign = GetSign(signTime, nonce, passtoken)
  27. headers = {
  28. 'Content-Type': 'application/json',
  29. 'x-tif-signature': sign,
  30. 'x-tif-timestamp': signTime,
  31. 'x-tif-nonce': nonce,
  32. 'x-tif-paasid': passid,
  33. 'x-tif-serviceId': service_code
  34. }
  35. data = {
  36. "system_id": "C90-44004428",
  37. "vender_id": "xxx",
  38. "department_id": "xxx",
  39. "query_timestamp": str(GetTime()),
  40. "UUID": GetNonce(4),
  41. "query": {
  42. "stcd":stcd,
  43. "startTime":f"{formatted_twenty_four_hours_ago}:00:00",
  44. "endTime":f"{formatted_now}:00:00"
  45. },
  46. "audit_info": {
  47. "operator_id": "xxxx",
  48. "operator_name": "xxx",
  49. "query_object_id": "xxxx",
  50. "query_object_id_type": "01",
  51. "item_id": "xxxx",
  52. "item_code": "xxx",
  53. "item_sequence": "xxx",
  54. "terminal_info": "xxxx",
  55. "query_timestamp": "xxxx"
  56. }
  57. }
  58. response = requests.post(url=url, headers=headers, json=data, verify=False)
  59. if response.status_code == 200:
  60. try:
  61. return response.json()['data']['data']
  62. except:
  63. return []
  64. def put_data(db=get_db_local()):
  65. query = db.query(GovdataRainDataInfo)
  66. rainfull = get_stcd_data()
  67. if len(rainfull)>0:
  68. now = datetime.now()
  69. twenty_four_hours_ago = now - timedelta(hours=24)
  70. db.query(GovdataRainDataInfo).filter(GovdataRainDataInfo.create_time <twenty_four_hours_ago ).delete()
  71. db.commit()
  72. for info in rainfull:
  73. code = info['F3070220000034_000018001']
  74. create_time = info['F3070220000034_000018004']
  75. raininfo = query.filter(GovdataRainDataInfo.code == code).filter(GovdataRainDataInfo.create_time == create_time).first()
  76. if raininfo:
  77. raininfo.area_name = info['F3070220000034_000018002']
  78. raininfo.address = info['F3070220000034_000018003']
  79. raininfo.rainfall = info['F3070220000034_000018005']
  80. raininfo.update_time = info['F3070220000034_000018006']
  81. else:
  82. raindata=GovdataRainDataInfo(
  83. code = info['F3070220000034_000018001'],
  84. area_name = info['F3070220000034_000018002'],
  85. address = info['F3070220000034_000018003'],
  86. create_time = info['F3070220000034_000018004'],
  87. rainfall = info['F3070220000034_000018005'],
  88. update_time = info['F3070220000034_000018006']
  89. )
  90. db.add(
  91. raindata
  92. )
  93. db.commit()
  94. def proc():
  95. logger.info(datetime.now())
  96. try:
  97. put_data()
  98. except:
  99. pass