require 'faraday'
require 'yaml'
require 'json'
class WeiboPoster
def initialize
@weibo_config = YAML.load_file(File.expand_path(File.dirname(__FILE__) + '/weibo-config.yml'))
@pushups = YAML.load_file(File.expand_path(File.dirname(__FILE__) + '/pushups.yml'))
end
def post_weibo(number)
@number = number
conn = Faraday.new(:url => "https://api.weibo.com")
result = conn.post '/2/statuses/update.json',
:access_token => @weibo_config['access_token'],
:status => generate_post
responseJSON = JSON.parse result.body
if responseJSON['error_code']
puts 'post error:' + responseJSON['error']
else
puts "post to weibo successfully"
end
end
private
def generate_post
total = get_history
total = total + @number.to_i
number_rest = 4000 - total
save_to_history total
post_template = @weibo_config['post_template'].force_encoding("utf-8")
post_template % {:number_done => @number, :total => total,:number_rest => number_rest}
end
def get_history
@pushups['total']
end
def save_to_history(total)
@pushups['total'] = total
File.open('pushups.yml','w') do |h|
h.write @pushups.to_yaml
end
end
end
poster = WeiboPoster.new
poster.post_weibo ARGV[0]
# Sina Weibo Post access_token: YOUR_ACCESS_TOKEN
require "rubygems" require "bundler/setup" require "stringex" desc "post pushups to weibo" task :pushups, :number do |t, args| args.with_defaults(:number => 50) number = args.number system "ruby post_weibo.rb " + number end
#!/bin/sh rake pushups[$1]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有