According to the smart principle I set goals for 2016 years. Each month has a small goal, each of which is specific (specific), measurable (measurable), attainable (achievable), relevant (relevance), Time-bound (time limit). The January goal was to run 200 km, which included 4 half marathon. At the end of January, it was found that this goal was easily achieved, and I ran a total of 220 km + in January, of which I ran 4 half marathon. And the second marathon broke my personal record by 1 hours, 43 minutes and 30 seconds, raising my personal best score by 2 minutes.
February My goal is to do 4,000 push-ups + Write 4 technical blog. While running I can use running software (thump or lap) to record my running mileage, while recording push-ups although there are some off-the-shelf software (such as push-ups), but I feel too heavy, want a lightweight way to record. Then I thought of a way, just in the command line terminal input A simple command, such as pushups 30, then my microblog will automatically come out with a blog, record how many push-ups I do, this month has completed how many push-ups, distance to the goal of how many push-ups. This looks like every one, I just knock a line of command can be easily recorded, and there are vast numbers of netizens to supervise.
This idea is very good, but how to achieve it? In fact, the whole process is not complicated, I spent two hours on the weekend to complete. Sina Weibo offers a micro-blogging platform that opens up a series of APIs on Weibo's open platform, one of which is sending microblogs. All we need to do is assemble the content we want to send, and write a program that calls its API to send the microblogging.
Send microblogging API documentation here. The document can be seen in fact, as long as sending an HTTP request, including the appropriate content. Two of these fields are more important, one is access token, and the other is status. Access token is the authentication token that determines which application to which to send the content, status is the micro-blog text to be pushed.
The process of obtaining access_token is more complicated, you need to understand the OAUTH2.0 certification process, please see the authorization mechanism description for details. Simply use your Weibo account to login to the microblogging open platform, register an application, then get an application ID, and then use the application ID to invoke the appropriate API to authorize access to your personal microblog, and finally get an access token.
What if you call this API? Because I have written a plugin to Twitter to tweet my blog information, so just put the relevant code to reuse it. The relevant code is written in Ruby. The code is as follows:
Weiboposter
Require ' Faraday ' require ' yaml ' require ' JSON ' class Weiboposter def initialize @weibo_config = Yaml.load_file (file.expa Nd_path (File.dirname (__file__) + '/weibo-config.yml ') @pushups = Yaml.load_file (File.expand_path (__ file__) + '/pushups.yml ') End def Post_weibo (number) @number = Number conn = faraday.new (: URL => "https://api.weibo.co M ") 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[' Erro R ' else puts ' post to Weibo successfully ' end-Private def generate_post total = get_history Total = Total + @number. t O_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_histor Y @pushups [' Total '] end def save_to_history (totAL) @pushups [' total '] = Total File.Open (' pushups.yml ', ' W ') does |h| H.write @pushups. To_yaml End poster = Weiboposter.new Poster.post_weibo argv[0]
The whole logic is first from a configuration file to read out the current completion of the number of push-ups, coupled with the command line parameters passed in the current group to do, combined with micro-blog template to generate micro-blog content, and then invoke the API to send HTTP requests.
Accecs token and Weibo templates are stored in weibo-config.yml files.
Weibo-config.yml
# Sina Weibo Post
Access_token:your_access_token
Post_template: Just did%{number_done} a push-ups, February a total of%{total} A push-ups, distance 4,000 push-ups target is still poor%{number_rest}
I then configured a task in Rakefile to invoke the Weiboposter class.
Rakefile
Require "RubyGems" require "bundler/setup" require "Stringex" desc "
post pushups to Weibo"
task: pushups,: Number do |t, args|
Args.with_defaults (: Number =>) Number
= Args.number
system "Ruby Post_weibo.rb" + Number end
Finally, a shell script is encapsulated to support shell invocation.
Pushups
#!/bin/sh
Rake pushups[$1]
OK, so we're all alive. After finishing a set of push-ups, just enter pushups 35 at the command line, and then you can see my microblog more than one tweet.
The final effect is as follows.