Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## geodecode
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2014 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8.  
  9. # Python modules
  10. import re
  11. import requests
  12. # NOC modules
  13. from noc.sa.models import *
  14. from noc.inv.models import *
  15. from noc.inv.models.object import *
  16.  
  17. rx_city = re.compile(
  18.         r"^(г\.|пгт\.|пос\.\.)+(?P<city>(\S+ \S+ \S+|\S+ \S+|\S+))")
  19. rx_street = re.compile(
  20.         r"^ ул\.+(?P<street>(\S+ \S+ \S+|\S+ \S+|\S+))")
  21. rx_house = re.compile(
  22.         r"^ д\.+(?P<house>(\d+\S+|\d+))")
  23.  
  24. rx_room = re.compile(
  25.         r"^ п\.+(?P<room>(\d+\S+|\d+))")
  26.  
  27. K = 0
  28. k = 0
  29.  
  30. for mo in tuple(ManagedObject.objects.filter(
  31.     administrative_domain=1)):
  32.     mo_id = mo.id
  33.     name = mo.name
  34.     postaddress = mo.postaddress.encode("UTF-8")
  35. #
  36. #    print ""
  37. #    print postaddress
  38. #
  39.     postaddress = postaddress.split(',')
  40.     match = rx_city.search(postaddress[0])
  41.     if match:
  42.         city = match.group("city")
  43.     else:
  44.         continue
  45.     try:
  46.         match = rx_street.search(postaddress[1])
  47.     except IndexError:
  48.         continue
  49.     if match:
  50.         street = match.group("street")
  51.     else:
  52.         continue
  53.     try:
  54.         match = rx_house.search(postaddress[2])
  55.     except IndexError:
  56.         continue
  57.     if match:
  58.         house = match.group("house")
  59.     else:
  60.         continue
  61.     try:
  62.         match = rx_room.search(postaddress[3])
  63.     except IndexError:
  64.         continue
  65.     if match:
  66.         room = match.group("room")
  67.     else:
  68.         continue
  69.  
  70.     pop =  'г.'.decode('UTF-8') + (city).decode('UTF-8') + ' | ' + 'ул.'.decode('UTF-8') + (street).decode('UTF-8') + ', д.'.decode('UTF-8') + (house).decode('UTF-8')
  71.     room = pop + ' | ' + "Подъезд №".decode('UTF-8') + room
  72.     rack = room + ' | ' + "Ящик №1".decode('UTF-8')
  73.  
  74.     if city:
  75. #
  76.  
  77.         try:
  78.             print rack
  79.             url="http://geocode-maps.yandex.ru/1.x/?geocode=" + city + ",+" + street.replace(' ', '+') + ",+дом+" + house +"&format=json"
  80.             urlopen = requests.get(url)
  81.             obj = urlopen.json()
  82.             coord = obj["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos"].split(' ')
  83.             x = coord[0]
  84.             y = coord[1]
  85.             print str(y), str(x)
  86.  
  87.             if not Object.objects.filter(name = pop):
  88.                 Object(
  89.                     name = pop,
  90.                     model = 'PoP | Access',
  91.                     data = {"container": {"container": True},
  92.                         "geopoint":{
  93.                             "x": x,
  94.                             "y": y},
  95.                         "pop":{"level": 20}}
  96.                 ).save
  97.                 print pop
  98.  
  99.             if not Object.objects.filter(name = room):
  100.                 Object(
  101.                     name = room,
  102.                     model = 'Room',
  103.                     data = {"container": {"container": True}}
  104.                 ).save
  105.                 print room
  106.  
  107.             if not Object.objects.filter(name = rack):
  108.                 Object(
  109.                     name = rack,
  110.                     model = 'NoName | Rack | 19" 42U 1000mm Shelf',
  111.                     data = {"container": {"container": True}}
  112.                 ).save
  113.                 print rack
  114.  
  115.             # Уложить mo в Rack
  116.  
  117.             k += 1
  118.             print "\n\n"
  119.  
  120.             break
  121.         except:
  122.  
  123.             break
  124.             continue
  125.  
  126. print K, k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement