ujing.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. def get_shops_info(lat,lng):
  23. url = 'https://u.zhinengxiyifang.cn/api/Stores/findNearStoreInc'
  24. payload = {"lat":lat,"lont":lng,"skip":0,"limit":10}
  25. # url = 'https://www.baidu.com'
  26. # payload = {}
  27. proxies = {'http':'http://140.246.89.239:19382','https':'https://140.246.89.239:19382'}
  28. strhtml = requests.post(url, json = payload,proxies = proxies,timeout = 15).text
  29. result = json.loads(strhtml)
  30. return result
  31. class School(Searchable):
  32. province = StringField(default = "")
  33. city = StringField(default = "")
  34. name = StringField(default = '')
  35. lat = StringField(default = '')
  36. lng = StringField(default = '')
  37. meta = {
  38. 'collection': 'School',
  39. 'db_alias': 'spider',
  40. 'unique_together': {'lat', 'lng'}
  41. }
  42. class ujing(Searchable):
  43. meta = {
  44. 'collection': 'ujing',
  45. 'db_alias': 'spider',
  46. 'unique_together': {'_id'}
  47. }
  48. class spiderLog(Searchable):
  49. lat = StringField(default = '')
  50. lng = StringField(default = '')
  51. addedTime = DateTimeField(default = datetime.datetime.now())
  52. meta = {
  53. 'collection': 'spiderLog',
  54. 'db_alias': 'spider',
  55. 'unique_together': {'lat','lng'}
  56. }
  57. def upsert_shops(shopsInfo):
  58. if not (shopsInfo.has_key('data') and shopsInfo['data'].has_key('storeList') and len(shopsInfo['data']['storeList']) > 0):
  59. return
  60. for shop in shopsInfo['data']['storeList']:
  61. try:
  62. ujing.get_collection().update({'_id':shop['_id']},{'$set':shop},upsert = True)
  63. except Exception,e:
  64. continue
  65. locations = School.get_collection().find()
  66. for loc in locations:
  67. try:
  68. count = spiderLog.objects.filter(lat = loc['lat'],lng = loc['lng']).count()
  69. if count > 0:
  70. continue
  71. shopsInfo = get_shops_info(loc['lat'], loc['lng'])
  72. spiderLog(lat = loc['lat'],lng = loc['lng']).save()
  73. print shopsInfo
  74. time.sleep(1)
  75. upsert_shops(shopsInfo)
  76. except Exception,e:
  77. print('error!!!!!!!!!!!!!!!!!!!',e)
  78. continue