yunyichong.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = '116.62.228.194',
  18. port = 27017,
  19. username = 'dba',
  20. password = 'dayuan@2020..',
  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 yunyichongDevice(Searchable):
  34. uuid = StringField(default = '')
  35. meta = {
  36. 'collection': 'yunyichong_device',
  37. 'db_alias': 'spider',
  38. 'unique_together': {'uuid'}
  39. }
  40. class errorLog(Searchable):
  41. meta = {
  42. 'collection': 'wanzhuang_err_location',
  43. 'db_alias': 'spider',
  44. 'unique_together': {'lat', 'lng'}
  45. }
  46. # 根据二维码编号,获取设备更详细的信息
  47. url = 'https://wx-yyc.dianxiaomei.com.cn/ui/nearby?lat=%s&lng=%s&city='
  48. cookie1 = 'PHPSESSID=0vcenbi6pgu6stup1brlelmo42; yyc_auth_v3=HNfsUaXyxVUL68umVR23r1JDNJd%2BwEEMXCH1oSMEbg%2FE08uwJGjseQE%2F7QigXiBx; uid=19599418;'
  49. govs = Goverment.get_collection().find()
  50. ii = 0
  51. for gov in govs[19970:]:
  52. try:
  53. ii += 1
  54. print ii,gov['name']
  55. if gov['name'][-2:] != u'政府':
  56. continue
  57. url1 = url % (gov['lat'],gov['lng'])
  58. cookies = cookie1 + '_lat=%s; _lng=%s;' % (gov['lat'],gov['lng']) + 'j_w_city=%E6%AD%A6%E6%B1%89%E5%B8%82'
  59. strhtml = str(requests.get(url1,verify=False,headers = {'Cookie':cookies}).text)
  60. startIndex = strhtml.find('device:[')
  61. endIndex = strhtml.find(']',startIndex)
  62. if not startIndex or not endIndex:
  63. continue
  64. devListStr = strhtml[startIndex+7:endIndex+1]
  65. devListDict = json.loads('{"device":%s}' % devListStr)
  66. for dev in devListDict['device']:
  67. yunyichongDevice.get_collection().update({'uuid':dev['uuid']},{'$set':dev},upsert = True)
  68. except Exception,e:
  69. continue
  70. print('OK')