-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_objects.coffee
201 lines (170 loc) · 4.56 KB
/
find_objects.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
_ = require 'underscore'
HTTP = require 'http'
URL = require 'url'
Mongoose = require 'mongoose'
findOrCreate = require 'mongoose-findorcreate'
requestOptions = {
protocol: "http",
hostname: "psearch-maps.yandex.ru",
port: 80,
pathname: "/1.x/"
}
requestQuery = {
format: "json",
results: "100",
ll: "61.407778,55.160556",
spn: "0.5,0.5",
rspn: "1"
}
queries = {
sight: [
"памятник",
"фонтан"
],
place: [
"парк"
],
religion: [
"мечеть",
"церковь",
"синагога"
],
culture: [
"музей",
"театр"
]
}
Schema = Mongoose.Schema
ObjectId = Schema.ObjectId
userSchema = new Schema {
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
}
placeSchema = new Schema {
name: { type: String, required: true },
description: { type: String },
coord: { type: [Number], index: '2dsphere', required: true },
kind: { type: String },
address: {
city: { type: String },
street: { type: String },
house: { type: String }
},
images: [{
url: { type: String }
}],
owner: { type: ObjectId, ref: 'User' }
tags: [{ type: ObjectId, ref: 'Tag' }]
}
tagSchema = new Schema {
value: { type: String, required: true, unique: true }
places: [{ type: ObjectId, ref: 'Place', unique: true }]
}
placeSchema.plugin findOrCreate
userSchema.plugin findOrCreate
tagSchema.plugin findOrCreate
Place = Mongoose.model 'Place', placeSchema
User = Mongoose.model 'User', userSchema
Tag = Mongoose.model 'Tag', tagSchema
# functions
querySearch = (queryStr, callback) ->
options = _.clone requestOptions
options.query = _.clone requestQuery
options.query.text = queryStr
url = URL.format options
console.log url
HTTP.get url, (res) =>
res.setEncoding 'utf8'
body = ""
res.on 'data', (data) ->
body += data
res.on 'end', ->
json = JSON.parse body
if json.error
console.log "Error:", queryStr, json.error
else
callback json.response.GeoObjectCollection.featureMember
findObjectsStack = 0
findObjects = (group, word) ->
findObjectsStack += 1
querySearch word, (objects) ->
for object in objects when object["GeoObject"]
newPlace object["GeoObject"], group, word
findObjectsStack -= 1
if findObjectsStack == 0
saveBuffers()
newPlace = (ya, kind, query) ->
p = {}
tags = [query]
if ya["metaDataProperty"] and ya["metaDataProperty"]["PSearchObjectMetaData"]
if ya["metaDataProperty"]["PSearchObjectMetaData"]["Address"]
addr = ya["metaDataProperty"]["PSearchObjectMetaData"]["Address"]
p.address = {}
p.address.city = addr["locality"] if addr["locality"]
p.address.street = addr["thoroughfare"] if addr["thoroughfare"]
p.address.house = addr["premiseNumber"] if addr["premiseNumber"]
if ya["metaDataProperty"]["PSearchObjectMetaData"]["Tags"]
p.tags = []
for tag in ya["metaDataProperty"]["PSearchObjectMetaData"]["Tags"] when tag.tag?
tags.push tag.tag
p.description = ya["description"] if ya["description"]
p.name = ya["name"]
p.coord = ya["Point"]["pos"].split(" ")
p.kind = kind
place = new Place(p)
addPlace place, tags
globalOwner = undefined
placesBuffer = []
addPlace = (place, tags) ->
p = new Place place
for t in tags
tag = getTag(t)
tag.places.push p._id
p.tags.push tag._id
p.owner = globalOwner._id if globalOwner
placesBuffer.push p
tagsBuffer = {}
getTag = (tag) ->
if tagsBuffer[tag]
tagsBuffer[tag]
else
t = new Tag { value: tag, places: [] }
tagsBuffer[tag] = t
t
saveBuffers = ->
c = 0
for p in placesBuffer
c += 1
p.save (err, p) ->
c -= 1
throw err if err
console.log p
if c == 0
printInfo()
for i, t of tagsBuffer
c += 1
t.save (err, t) ->
c -= 1
throw err if err
console.log t
if c == 0
printInfo()
printInfo = ->
console.log "Count of places: #{placesBuffer.length}\n"
console.log "Tags:"
counter = 0
for i, v of tagsBuffer
counter++
console.log "\t#{counter}: #{i}"
console.log "done"
Mongoose.connection.close()
# main
Mongoose.connect 'mongodb://localhost/travel_foot', (err) ->
throw err if err
User.findOrCreate { email: "system" }, { email: "system", password: "ph06ahyoVble" }, (err, user) ->
throw err if err
console.log user
globalOwner = user
for group, words of queries
for word in words
findObjects group, word