jdBill.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import csv
  4. import datetime
  5. import getopt
  6. import os
  7. import sys
  8. import requests
  9. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  10. sys.path.insert(0, PROJECT_ROOT)
  11. from script.base import init_env
  12. os.environ["DJANGO_SETTINGS_MODULE"] = "configs.production_local"
  13. init_env(False)
  14. from apps.web.merchant.utils import JDMerchant
  15. from django.conf import settings
  16. def download_bill_file(merchantNo, start, end):
  17. st = datetime.datetime.strptime(start, "%Y-%m-%d")
  18. if end:
  19. et = datetime.datetime.strptime(end, "%Y-%m-%d")
  20. else:
  21. et = datetime.datetime.now()
  22. _dirPath = os.path.join(settings.PROJECT_ROOT, "script/jdBill/{}".format(merchantNo))
  23. if not os.path.exists(_dirPath):
  24. os.makedirs(_dirPath)
  25. while True:
  26. if (et - st).days == 0:
  27. break
  28. billDate = st.strftime("%Y%m%d")
  29. st = st + datetime.timedelta(days=1)
  30. try:
  31. _downloadPath = JDMerchant.query_bill_file(merchantNo, billDate=billDate)
  32. except Exception as e:
  33. print "{} {}".format(billDate, e)
  34. continue
  35. response = requests.get(url=_downloadPath)
  36. content = response.content
  37. with open(os.path.join(_dirPath, "bill_{}.zip".format(billDate)), "wb") as f:
  38. f.write(content)
  39. def download_settle_file(merchantNo, start, end):
  40. dataRows = list()
  41. st = datetime.datetime.strptime(start, "%Y-%m-%d")
  42. if end:
  43. et = datetime.datetime.strptime(end, "%Y-%m-%d")
  44. else:
  45. et = datetime.datetime.now()
  46. _dirPath = os.path.join(settings.PROJECT_ROOT, "script/jdSettle/{}".format(merchantNo))
  47. if not os.path.exists(_dirPath):
  48. os.makedirs(_dirPath)
  49. days = (et - st).days
  50. for _index in range(days):
  51. _date = st + datetime.timedelta(_index)
  52. _dateStr = _date.strftime("%Y-%m-%d")
  53. _settleDataList = list()
  54. result = JDMerchant.query_settle(
  55. agentNo="112122276",
  56. startTime=_dateStr,
  57. endTime=_dateStr,
  58. merchantNo=merchantNo,
  59. )
  60. if "dataList" not in result or not result["dataList"]:
  61. continue
  62. _settleData = result["dataList"][0]
  63. dataRows.append(_settleData)
  64. with open(os.path.join(_dirPath, "settle.csv"), "w") as f:
  65. wr = csv.DictWriter(f, fieldnames=[
  66. "settleDate", "merchantNo", "merchantName", "settleType",
  67. "settlementAmount", "settleAccountNo", "orderStatus", "remark"
  68. ])
  69. wr.writeheader()
  70. wr.writerows(dataRows)
  71. if __name__ == '__main__':
  72. try:
  73. options, args = getopt.getopt(sys.argv[1:], 's:e:m:',
  74. ['start=', 'end=', 'merchantNo='])
  75. except getopt.GetoptError as e:
  76. print(str(e))
  77. sys.exit()
  78. start = None
  79. end = None
  80. merchantNo = None
  81. for name, value in options:
  82. if name in ('-s', '--start'):
  83. start = value
  84. if name in ('-e', '--end'):
  85. end = value
  86. if name in ('-m', '--merchantNo'):
  87. merchantNo = str(value)
  88. print 'start = {}'.format(start)
  89. print 'end = {}'.format(end)
  90. print 'merchant no = {}'.format(merchantNo)
  91. download_bill_file(merchantNo, start, end)
  92. download_settle_file(merchantNo, start, end)