shankailaidianExport.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os, sys,time,datetime
  4. import urllib, urllib2, sys
  5. import requests
  6. from mongoengine import register_connection, PointField, DynamicDocument, StringField
  7. import simplejson as json
  8. import ssl
  9. import xlrd
  10. from xlrd import xldate_as_tuple
  11. from collections import OrderedDict
  12. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  13. sys.path.insert(0, PROJECT_ROOT)
  14. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing")
  15. from script.base import init_env
  16. init_env(interactive = False)
  17. from apps.web.core.db import Searchable
  18. from apps.web.core.utils import generate_excel_report
  19. register_connection(alias = 'spider',
  20. name = 'spider',
  21. host = '211.159.224.10',
  22. port = 27119,
  23. username = 'service',
  24. password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5',
  25. authentication_source = 'admin')
  26. class shankailaidianDevice(Searchable):
  27. meta = {
  28. 'collection': 'shankailaidian_device',
  29. 'db_alias': 'spider',
  30. 'unique_together': {'vStationNo'}
  31. }
  32. # 先把所有的设备二维码编号爬下来
  33. # page=0
  34. # while True:
  35. # url = 'https://trade.api3.sdaascloud.com/trade/nearbyChargers?key=&page=%s&size=10&latitude=30.48029&longitude=114.42073&raidus=3000000000' % page
  36. # strhtml = requests.get(url,timeout = 15).text
  37. # result = json.loads(strhtml)
  38. # if result.has_key('data') and result['data'].has_key('content') and len(result['data']['content']) == 0:
  39. # break
  40. # for dev in result['data']['content']:
  41. # xzzDevice.get_collection().update({'qrId':dev['qrId']},{'$set':dev},upsert = True)
  42. # page += 1
  43. # 根据二维码编号,获取设备更详细的信息
  44. def get_tel_zone(tel):
  45. if not tel or len(tel) <= 7:
  46. return '',''
  47. tel = tel.replace('-','')
  48. tel = tel.replace(' ','')
  49. print tel
  50. host = 'https://api04.aliyun.venuscn.com'
  51. path = '/mobile'
  52. method = 'GET'
  53. appcode = '8296fcd952e34713ba91fbdceb13e915'
  54. querys = 'mobile=%s' % tel
  55. bodys = {}
  56. url = host + path + '?' + querys
  57. try:
  58. request = urllib2.Request(url)
  59. request.add_header('Authorization', 'APPCODE ' + appcode)
  60. ctx = ssl.create_default_context()
  61. ctx.check_hostname = False
  62. ctx.verify_mode = ssl.CERT_NONE
  63. response = urllib2.urlopen(request, timeout = 15, context=ctx)
  64. content = response.read()
  65. if (content):
  66. result = json.loads(content)
  67. if result['msg'] != 'success':
  68. return '', ''
  69. return result['data']['prov'],result['data']['city']
  70. except Exception,e:
  71. return '',''
  72. # ownerDict = {}
  73. # for shopInfo in pianyichongDevice.get_collection().find():
  74. # if not shopInfo.has_key('manager'):
  75. # continue
  76. # if ownerDict.has_key(shopInfo['manager']):
  77. # ownerDict[shopInfo['manager']] += 1
  78. # else:
  79. # ownerDict[shopInfo['manager']] = 1
  80. # records = []
  81. # ii = 0
  82. # for rcd in shankailaidianDevice.get_collection().find({}):
  83. # ii += 1
  84. # if ii % 100 == 0:
  85. # print ii
  86. # dataList = [
  87. # (u'编号', str(rcd['vStationNo'])),
  88. # (u'总收入', float(rcd['dTotalIncome'])),
  89. # (u'地域ID', str(rcd['iAreaId'])),
  90. # (u'businessType', str(rcd['iBusinessType'])),
  91. # (u'iManagerId', str(rcd['iManagerId'])),
  92. # (u'添加时间', str(rcd['tAddTime'])),
  93. # (u'打开时间', str(rcd['tOpenTime'])),
  94. # (u'地址', str(rcd['vAddress'])),
  95. # (u'地域', str(rcd['vAreaName'])),
  96. # (u'电话', rcd['vHelpMobile'] if rcd['vHelpMobile'] != '' else rcd['vTelephone']),
  97. # # (u'充电站名称', str(rcd['vStationName'])),
  98. # ]
  99. #
  100. # records.append(OrderedDict(dataList))
  101. #
  102. # generate_excel_report('F:/shankailaidian1.xlsx', records,True)
  103. records = []
  104. owerDict = {}
  105. ii = 0
  106. for rcd in shankailaidianDevice.get_collection().find({}):
  107. ii += 1
  108. if ii % 100 == 0:
  109. print ii
  110. if rcd['vHelpMobile'] in owerDict:
  111. owerDict[rcd['vHelpMobile']]['count'] += 1
  112. owerDict[rcd['vHelpMobile']]['income'] += float(rcd['dTotalIncome'])
  113. else:
  114. owerDict[rcd['vHelpMobile']] = {'count':1,'income':float(rcd['dTotalIncome'])}
  115. for mobile,info in owerDict.items():
  116. ii += 1
  117. if ii % 100 == 0:
  118. print ii
  119. dataList = [
  120. (u'电话', mobile),
  121. (u'数目', info['count']),
  122. (u'总收入', info['income']),
  123. (u'分配', info['income']*0.2),
  124. (u'单台设备均分', info['income']*0.2/info['count']),
  125. ]
  126. records.append(OrderedDict(dataList))
  127. generate_excel_report('F:/shankailaidian_tongji2.xlsx', records,True)
  128. print('OK')