SHOW:
|
|
- or go back to the newest paste.
| 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 | from noc.lib.nosql import get_db | |
| 17 | ||
| 18 | rx_city = re.compile( | |
| 19 | r"^(г\.|пгт\.|пос\.|с\.)+(?P<city>(\S+ \S+ \S+|\S+ \S+|\S+))") | |
| 20 | rx_street = re.compile( | |
| 21 | r"^ ул\.+(?P<street>(\S+ \S+ \S+|\S+ \S+|\S+))") | |
| 22 | rx_house = re.compile( | |
| 23 | r"^ д\.+(?P<house>(\d+\S+|\d+))") | |
| 24 | ||
| 25 | rx_room = re.compile( | |
| 26 | r"^ п\.+(?P<room>(\d+\S+|\d+))") | |
| 27 | ||
| 28 | db = get_db() | |
| 29 | om = db.noc.objectmodels | |
| 30 | ||
| 31 | K = 0 | |
| 32 | k = 0 | |
| 33 | ||
| 34 | for mo in tuple(ManagedObject.objects.filter( | |
| 35 | administrative_domain=1)): | |
| 36 | mo_id = mo.id | |
| 37 | name = mo.name | |
| 38 | postaddress = mo.postaddress.encode("UTF-8")
| |
| 39 | # | |
| 40 | # print "" | |
| 41 | # print postaddress | |
| 42 | # | |
| 43 | postaddress = postaddress.split(',')
| |
| 44 | match = rx_city.search(postaddress[0]) | |
| 45 | if match: | |
| 46 | city = match.group("city")
| |
| 47 | # | |
| 48 | # | |
| 49 | else: | |
| 50 | continue | |
| 51 | try: | |
| 52 | match = rx_street.search(postaddress[1]) | |
| 53 | except IndexError: | |
| 54 | continue | |
| 55 | if match: | |
| 56 | street = match.group("street")
| |
| 57 | else: | |
| 58 | continue | |
| 59 | try: | |
| 60 | match = rx_house.search(postaddress[2]) | |
| 61 | except IndexError: | |
| 62 | continue | |
| 63 | if match: | |
| 64 | house = match.group("house")
| |
| 65 | else: | |
| 66 | continue | |
| 67 | try: | |
| 68 | match = rx_room.search(postaddress[3]) | |
| 69 | except IndexError: | |
| 70 | continue | |
| 71 | if match: | |
| 72 | room = match.group("room")
| |
| 73 | else: | |
| 74 | continue | |
| 75 | ||
| 76 | pop = 'г.'.decode('UTF-8') + (city).decode('UTF-8') + ' | ' + 'ул.'.decode('UTF-8') + (street).decode('UTF-8') + ', д.'.decode('UTF-8') + (house).decode('UTF-8')
| |
| 77 | room = pop + ' | ' + "Подъезд №".decode('UTF-8') + room
| |
| 78 | rack = room + ' | ' + "Ящик №1".decode('UTF-8')
| |
| 79 | ||
| 80 | K += 1 | |
| 81 | if city: | |
| 82 | # | |
| 83 | ||
| 84 | # try: | |
| 85 | print rack | |
| 86 | url="http://geocode-maps.yandex.ru/1.x/?geocode=" + city + ",+" + street.replace(' ', '+') + ",+дом+" + house +"&format=json"
| |
| 87 | urlopen = requests.get(url) | |
| 88 | obj = urlopen.json() | |
| 89 | coord = obj["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos"].split(' ')
| |
| 90 | x = coord[0] | |
| 91 | y = coord[1] | |
| 92 | print str(y), str(x) | |
| 93 | ||
| 94 | if not Object.objects.filter(name = pop): | |
| 95 | Object( | |
| 96 | name = pop, | |
| 97 | model = om.find_one({"name": "PoP | Access"})["_id"],
| |
| 98 | data = {"container": {"container": True},
| |
| 99 | "geopoint": {
| |
| 100 | "x": x, | |
| 101 | "y": y}, | |
| 102 | "pop": {"level": 20}
| |
| 103 | } | |
| 104 | ).save() | |
| 105 | print pop | |
| 106 | ||
| 107 | if not Object.objects.filter(name = room): | |
| 108 | Object( | |
| 109 | name = room, | |
| 110 | model = om.find_one({"name": "Room"})["_id"],
| |
| 111 | data = {"container": {"container": True}}
| |
| 112 | ).save() | |
| 113 | print room | |
| 114 | ||
| 115 | if not Object.objects.filter(name = rack): | |
| 116 | Object( | |
| 117 | name = rack, | |
| 118 | - | model = om.find_one({"name": 'NoName | Rack | 19" 8U 800mm Shelf'})["_id"],
|
| 118 | + | model = om.find_one({"name": 'NoName | Rack | 19" 42U 1000mm Shelf'})["_id"],
|
| 119 | data = {"container": {"container": True}}
| |
| 120 | ).save() | |
| 121 | print rack | |
| 122 | ||
| 123 | # Уложить mo в Rack | |
| 124 | ||
| 125 | k += 1 | |
| 126 | print "\n\n" | |
| 127 | break | |
| 128 | # except: | |
| 129 | # break | |
| 130 | # continue | |
| 131 | ||
| 132 | print K, k |