lyy_dealer_stats.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os,json
  4. import urllib, urllib2, sys
  5. import ssl
  6. import xlrd
  7. from xlrd import xldate_as_tuple
  8. from collections import OrderedDict
  9. from apps.web.core.utils import generate_excel_report
  10. # 统计乐摇摇的经销商数据,按照设备量的大小,从高统计 https://m.leyaoyao.com/customer/message/t/81202242
  11. rootdir = u'F:/Temp/lyy'
  12. # 查询手机归属地的代码
  13. def get_tel_zone(tel):
  14. if len(tel) <= 7:
  15. return '',''
  16. tel = tel.replace('-','')
  17. tel = tel.replace(' ','')
  18. print tel
  19. host = 'https://api04.aliyun.venuscn.com'
  20. path = '/mobile'
  21. method = 'GET'
  22. appcode = '8296fcd952e34713ba91fbdceb13e915'
  23. querys = 'mobile=%s' % tel
  24. bodys = {}
  25. url = host + path + '?' + querys
  26. try:
  27. request = urllib2.Request(url)
  28. request.add_header('Authorization', 'APPCODE ' + appcode)
  29. ctx = ssl.create_default_context()
  30. ctx.check_hostname = False
  31. ctx.verify_mode = ssl.CERT_NONE
  32. response = urllib2.urlopen(request, timeout = 15, context=ctx)
  33. content = response.read()
  34. if (content):
  35. result = json.loads(content)
  36. if result['msg'] != 'success':
  37. return '', ''
  38. return result['data']['prov'],result['data']['city']
  39. except Exception,e:
  40. return '',''
  41. # 遍历设备,并登记到经销商
  42. dealerDict = {}
  43. allCount = 0
  44. listFile = os.listdir(rootdir) # 列出文件夹下所有的目录与文件
  45. for i in range(0, len(listFile)):
  46. path = os.path.join(rootdir, listFile[i])
  47. try:
  48. book = xlrd.open_workbook(path)
  49. sheet = book.sheet_by_name("sheet1")
  50. except Exception,e:
  51. print 'open excel file error =%s' % e
  52. continue
  53. nrows = sheet.nrows
  54. rows = []
  55. for i in range(1, nrows):
  56. row = sheet.row_values(i)
  57. allCount += 1
  58. try:
  59. tel = row[1]
  60. if tel not in dealerDict:
  61. dealerDict[tel] = {'count':1,'name':row[3],'devList':[row[0]]}
  62. else:
  63. dealerDict[tel]['count'] += 1
  64. if len(dealerDict[tel]['devList']) < 10:
  65. dealerDict[tel]['devList'].append(str(row[0]))
  66. except Exception,e:
  67. print 'some error when update e=%s' % (e)
  68. dealerList = []
  69. for k,v in dealerDict.items():
  70. tempStr = ''
  71. try:
  72. tempStr = ','.join(v['devList'])
  73. except Exception,e:
  74. tempStr = ''
  75. dealerList.append((k,v['count'],v['name'],tempStr))
  76. def cmp_xy(x,y):
  77. if x[1] < y[1]:
  78. return -1
  79. elif x[1] == y[1]:
  80. return 0
  81. return 1
  82. dealerList.sort(cmp=cmp_xy, reverse=True)
  83. records = []
  84. for dealer in dealerList:
  85. try:
  86. print u'电话:%s,数量:%s,设备名称:%s,设备编号举例:%s' % (dealer[0],dealer[1],dealer[2],dealer[3])
  87. except Exception,e:
  88. continue
  89. province,city = get_tel_zone(dealer[0])
  90. dataList = [
  91. (u'电话', dealer[0]),
  92. (u'数量', dealer[1]),
  93. (u'设备名称', dealer[2]),
  94. (u'编号举例', dealer[3]),
  95. (u'手机归属省份', province),
  96. (u'手机归属城市', city),
  97. ]
  98. records.append(OrderedDict(dataList))
  99. generate_excel_report('F:/Temp/lyy/202009.xlsx', records, localSave = True)
  100. print u'共计:%s' % allCount