haokuaichong.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os, sys,time,datetime
  4. import urllib
  5. import requests
  6. from mongoengine import register_connection, PointField, DynamicDocument, StringField
  7. import simplejson as json
  8. from django.db.models.fields import DateTimeField
  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. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing")
  12. from script.base import init_env
  13. init_env(interactive = False)
  14. from apps.web.core.db import Searchable
  15. register_connection(alias = 'spider',
  16. name = 'spider',
  17. host = '211.159.224.10',
  18. port = 27119,
  19. username = 'service',
  20. password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5',
  21. authentication_source = 'admin')
  22. class Goverment(Searchable):
  23. province = StringField(default = "")
  24. city = StringField(default = "")
  25. name = StringField(default = '')
  26. lat = StringField(default = '')
  27. lng = StringField(default = '')
  28. meta = {
  29. 'collection': 'Goverment',
  30. 'db_alias': 'spider',
  31. 'unique_together': {'lat', 'lng'}
  32. }
  33. class haokuaichongDevice(Searchable):
  34. meta = {
  35. 'collection': 'haokuaichong_device',
  36. 'db_alias': 'spider',
  37. }
  38. class haokuaichongOtherDevice(Searchable):
  39. meta = {
  40. 'collection': 'haokuaichong_other_device',
  41. 'db_alias': 'spider',
  42. }
  43. # 分析公众号
  44. # url = 'http://ap3202.haokuaichong.cn/quickuser50/charge/info?device=%s&port=FF&cmd=0'
  45. # cookie = 'JSESSIONID=8E5A27C36409661680FCADD921EF267A;tgw_l7_route=8601cd2f2bac0d2b07a50c969c168416'
  46. # auth = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoidXNlciIsImVuY3J5SWQiOiJOakEzTXpNMU1RPT0iLCJpc3MiOiJxdWlja191c2VyXzAwMSIsImlhdCI6MTY0OTQwMDIyNiwiYXVkIjoicXVpY2tfdXNlcl9hcGkiLCJleHAiOjE2NDk0MDM4MjYsIm5iZiI6MTY0OTQwMDIyNn0.C1KXB77mjkhsof01NGFoWM_TdSLeZ3OlWVuIiqLZXAw'
  47. # for ii in range(80,290000): #00,01,02,03
  48. # logicalId = ('%02xE1A704' % ii).upper()
  49. # url1 = url % logicalId
  50. # strhtml = requests.get(url1,headers = {'Cookie':cookie,'Authorization':auth}).text
  51. # result = json.loads(strhtml)
  52. # print result
  53. # if result['msg'] == u'请勿跨公众号使用':
  54. # print '------------------->',logicalId
  55. # 根据二维码编号,获取设备更详细的信息
  56. url = 'http://ap3202.haokuaichong.cn/quickuser50/charge/info?device=%s&port=00&cmd=1'
  57. cookie = 'JSESSIONID=8E5A27C36409661680FCADD921EF267A;tgw_l7_route=8601cd2f2bac0d2b07a50c969c168416'
  58. auth = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoidXNlciIsImVuY3J5SWQiOiJOakUzTVRVMU5nPT0iLCJpc3MiOiJxdWlja191c2VyXzAwMSIsImlhdCI6MTY0OTkwMzI0OSwiYXVkIjoicXVpY2tfdXNlcl9hcGkiLCJleHAiOjE2NDk5MDY4NDksIm5iZiI6MTY0OTkwMzI0OX0.CN-UApjFt7mPChSIxHspf7gQoIM0rd6wZmefVe3_FHo'
  59. for ii in range(0,390000): #7174
  60. try:
  61. logicalId = '11%06d' % ii
  62. url1 = url % logicalId
  63. strhtml = requests.get(url1,headers = {'Cookie':cookie,'Authorization':auth}).text
  64. result = json.loads(strhtml)
  65. if result['code'] == 500 :
  66. if result['msg'] == u'请勿跨公众号使用':
  67. haokuaichongOtherDevice.get_collection().update({'deviceId':logicalId},{'$set':{'deviceId':logicalId}},upsert = True)
  68. continue
  69. else:
  70. continue
  71. if result['code'] == 70004:
  72. print ii, 'token 过期'
  73. break
  74. dev = result['data']
  75. haokuaichongDevice.get_collection().update({'deviceId':dev['deviceId']},{'$set':dev},upsert = True)
  76. print ii,'catch new one!!!!'
  77. except Exception,e:
  78. print 'ExceptionExceptionExceptionExceptionException',e
  79. continue
  80. print('OK')