-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_images.coffee
164 lines (142 loc) · 4.11 KB
/
find_images.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
_ = require 'underscore'
HTTP = require 'http'
URL = require 'url'
IM = require 'imagemagick'
FS = require 'fs'
Mongoose = require 'mongoose'
requestOptions = {
protocol: "http",
hostname: "ajax.googleapis.com",
port: 80,
pathname: "/ajax/services/search/images"
}
requestQuery = {
v: "1.0",
}
uploadsPath = __dirname + '/public/uploads'
Schema = Mongoose.Schema
ObjectId = Schema.ObjectId
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 },
ext: { type: String }
}],
owner: { type: ObjectId, ref: 'User' }
tags: [{ type: ObjectId, ref: 'Tag' }]
}
Place = Mongoose.model 'Place', placeSchema
querySearch = (place, callback) ->
options = _.clone requestOptions
options.query = _.clone requestQuery
options.query.q = "Челябинск " + place.name
url = URL.format options
HTTP.get url, (res) =>
res.setEncoding 'utf8'
body = ""
res.on 'data', (data) ->
body += data
res.on 'end', ->
json = JSON.parse body
if json.responseStatus != 200
console.log "Error:\n\turl: #{url}\n\tquery: #{place.name}\n\tinfo: #{json.responseDetails}"
else
callback place, json.responseData.results
saveResults = (place, results, cb) ->
return if not results
placeDir = "#{uploadsPath}/#{place._id}"
FS.mkdirSync placeDir
c = 0
e = 0
for r in results
c += 1
url = r.url
image = place.images.push({})
image = place.images[image - 1]
image.url = "/uploads/#{place._id}/#{image._id}"
match = url.match(/\.([0-9a-z]+)$/i)
if match
image.ext = match[1]
else
c -= 1
console.log "error", url
e += 1
continue
saveFile url, image, placeDir, (err) ->
if err
console.log "error"
c -= 1
if c == 0
cb place
if e == results.length
cb place
filePath = (placeDir, image, version) ->
if version
version = "-" + version
else
version = ""
"#{placeDir}/#{image._id}#{version}.#{image.ext}"
saveFile = (url, image, placeDir, cb) ->
protocol = url.match(/^[a-z]+/)
if (!protocol || protocol[0] != "http")
image.error = true
cb(true)
return
tmp = "#{__dirname}/tmp/#{image._id}"
file = FS.createWriteStream(tmp)
HTTP.get url, (res) ->
if res.statusCode != 200
file.close()
image.error = true
cb(true)
return
res.pipe file
file.on 'finish', ->
file.close()
r = filePath(placeDir, image, 'small')
IM.convert [tmp, '-gravity', 'center', '-resize', '100x100^', '-crop', '100x100+0+0', r], (err) ->
throw err if err
r = filePath(placeDir, image, 'middle')
IM.convert [tmp, '-gravity', 'center', '-resize', '320x240^', '-crop', '320x240+0+0', r], (err) ->
throw err if err
r = filePath(placeDir, image)
IM.convert [tmp, '-gravity', 'center', '-resize', '640x480^', '-crop', '640x480+0+0', r], (err) ->
throw err if err
FS.unlink tmp
cb()
Mongoose.connect 'mongodb://localhost/travel_foot', (err) ->
throw err if err
page = Number(process.argv[2]) || 1
console.log "page: #{page}"
Place.find({}).sort({_id: 1}).limit(10).skip((page - 1) * 10).exec (err, places) ->
throw err if err
console.log "found: #{places.length}"
for p in places
console.log "\t", p._id
c = 0
timeout = 0
for place in places
c += 1
((place) ->
setTimeout (->
querySearch place, (p, r) ->
saveResults p, r, (place) ->
place.images = _.filter place.images, (i) ->
!i.error
place.save (err) ->
throw err if err
c -= 1
if c == 0
Mongoose.connection.close()
console.log "done"
), timeout
)(place)
timeout += 2000