Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Created August 1, 2013 14:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save izumogeiger/6131907 to your computer and use it in GitHub Desktop.
# coding:utf-8
require "pp"
require "twitter"
require "net/https"
def tweet(cpm,msv)
tweet = "#{cpm} cpm, #{msv} microsv"
Twitter.configure do |config|
config.consumer_key='xxxx'
config.consumer_secret='xxxx'
config.oauth_token='xxxx'
config.oauth_token_secret='xxxx'
end
Twitter.update(tweet)
end
def xively(cpm,msv)
uri = URI.parse('https://api.xively.com/v2/feeds/xxxxxxxxx.xml')
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
headers = {
'X-ApiKey' => 'xxxxxxxxxxxxxxxxxxx',
'Content-Type' => 'application/xml'
}
data =<<-EOS
<?xml version="1.0" encoding="UTF-8"?>
<eeml>
<environment>
<data id="cpm">
<current_value>#{cpm}</current_value>
</data>
<data id="microsv">
<current_value>#{msv}</current_value>
</data>
</environment>
</eeml>
EOS
res = https.send_request('PUT',uri.request_uri,data,headers)
end
now = Time.now
dir = File.join(ENV["HOME"],"gc10",now.strftime("%Y"),now.strftime("%m"))
file = File.join(dir,now.strftime("%d")+".txt")
#file = "data.txt"
lines = File.read(file).split("\n")
return if lines.size < 60
cpms = lines[-60..-1].map do |l| l.split("\t")[1] end
total = cpms.inject(0) do |sum,i| sum + i.to_i end
avg_cpm = sprintf("%.2f",total / 60.0)
avg_msv = sprintf("%.3f",(total/60.0) / 150)
begin
tweet(avg_cpm,avg_msv)
rescue Exception
end
begin
xively(avg_cpm,avg_msv)
rescue Exception
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment