-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddHourToAvgColour-id-time-date-csv.rb
executable file
·50 lines (42 loc) · 1.42 KB
/
AddHourToAvgColour-id-time-date-csv.rb
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
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'pp'
require 'rmagick'
require 'mongo'
Mongo::Logger.logger.level = ::Logger::FATAL # http://stackoverflow.com/questions/30292100/how-can-i-disable-mongodb-log-messages-in-console
MONGO_HOST = ENV["MONGO_HOST"]
raise(StandardError,"Set Mongo hostname in ENV: 'MONGO_HOST'") if !MONGO_HOST
MONGO_PORT = ENV["MONGO_PORT"]
raise(StandardError,"Set Mongo port in ENV: 'MONGO_PORT'") if !MONGO_PORT
MONGO_USER = ENV["MONGO_USER"]
MONGO_PASSWORD = ENV["MONGO_PASSWORD"]
INSTAGRAM_DB = ENV["INSTAGRAM_DB"]
raise(StandardError,"Set Mongo instagram database name in ENV: 'INSTAGRAM_DB'") if !INSTAGRAM_DB
db = Mongo::Client.new([MONGO_HOST], :database => INSTAGRAM_DB, :port => MONGO_PORT)
if MONGO_USER
auth = db.authenticate(MONGO_USER, MONGO_PASSWORD)
if !auth
raise(StandardError, "Couldn't authenticate, exiting")
exit
end
end
photosColl = db[:photos]
printf("colour,id,dayofweek-month-dayofmonth,daynumber,unixtimeint,hour\n")
header = TRUE
ARGF.each do |line|
if header
header = FALSE
next
end
#pp line
id = line.chomp.split(",")[1]
$stderr.printf "id:%s\n", id
photo = photosColl.find({ "id" => id}).\
projection({ "id" => 1, "datetaken" => 1}).limit(1).first()
#pp photo
hour = photo["datetaken"].localtime.hour
unixtimeint = photo["datetaken"].localtime
printf("%s,%d,%2.2d\n",
line.chomp, unixtimeint, hour)
end