Automatically record the number of push-ups using Weibo

Source: Internet
Author: User
Tags scalar

According to the smart Principle I set a goal of 2016 years. Each month has a small goal, each goal is specific (specific), measurable (measurable), attainable (achievable), relevant (relevance), Time-bound (time limit). The goal of January is to run 200 km, which includes 4 half marathon. At the end of January, I found this goal easy to achieve, the whole January I ran a total of 220 km +, which ran 4 half marathon. And the second marathon broke my personal record, with a score of 1 hours, 43 minutes, 30 seconds, and my personal best score was increased by 2 minutes.

February My goal is to do 4,000 push-ups + Write 4 technical blogs. While running I can use the running software (plump or lap) to record my running mileage, while record push-ups have some ready-made software (such as push-ups), but I feel too heavyweight and want a lightweight way to record. Then I came up with a way to just enter a simple command at the command-line terminal, for example pushups 30 , my microblog will automatically come up with a blog post, record how many push-ups I've done this time, how many push-ups have been done this month, and how many push-ups are left from the target. This looks like every set, I just hit a line of command can be easily recorded, and also the vast number of netizens to supervise.

That's a good idea, but how do you do that? In fact, the whole process is not complicated, I spent two hours on the weekend to finish. Sina Weibo provides an open platform for Weibo, which opens up a series of APIs on Weibo's open platform, one of which is the sending of micro-blogs. We just need to assemble the content we want to send, and write a program that calls its API to send the microblog.

Send the Weibo API documentation here. As you can see in the documentation, it's good to send an HTTP request with the appropriate content. Two of these fields are important, one is access token and the other is status. Access token is an authentication token that determines which app is sending content to which microblogging, and status is the text of the tweet that needs to be pushed.

The process of obtaining access_token is complex and requires you to understand the OAUTH2.0 certification process, see the authorization mechanism for details. Simply put your Weibo account on the Weibo open platform, register an app, then get an app ID, then use the app ID to invoke the appropriate API to authorize access to your personal Weibo and finally get an access token.

What if this API is called? Because I have previously written a plugin to Twitter to send 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
123456789Ten One A - - the - - - + - + A at - - - - - in - to + - the * $Panax Notoginseng - the + A the + - $ $ - - the -Wuyi the -
 require ' Faraday '  require ' Yaml '  require ' json '  class weiboposter  def Initialize     @weibo_config  =  yaml  Span class= "O".  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] 

The whole logic is to read out the current number of push-ups from a configuration file, and then match the number of current groups that are passed through the command line parameters, generate the microblog content with the Weibo template, and then call the API to send HTTP requests.

Accecs token and Weibo templates are stored in the Weibo-config.yml file.

Weibo-config.yml
1 2 3
# Sina Weibo Postaccess_token: YOUR_ACCESS_TOKENpost_template: 刚才做了%{number_done}个俯卧撑,2月份总共完成了%{total}个俯卧撑,距离4000个俯卧撑目标还差%{number_rest}个

Then I configured a task in Rakefile to call the Weiboposter class.

Rakefile
1 2 3 4 5 6 7 8 9 Ten  One
 require "RubyGems"  require "Bundler/setup"  require "Stringex"  desc "post pushups to Weibo"  Task :p ushups, :  number do | T , args | args. With_defaults (: number = +)     Number   = args.       Number system "Ruby post_weibo.rb" +       numberEnd

Finally, a shell script is encapsulated to support shell invocation.

pushups
1 2
#!/bin/shrake pushups[$1]

Ok so it's all alive. After you've finished a set of push-ups, just type in the command line pushups 35 , and you'll see a tweet in my Twitter post.

The final effect is as follows.

My Weibo address: @ Invincible North Melon

Automatically record the number of push-ups using Weibo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.