使用微博自動記錄俯臥撐個數

根據SMART原則我制定了2016年的目標。每月都有一個小目標,每一個目標都是specific(具體)、Measurable(可度量)、Attainable(可實現)、Relevant(相關性)、Time-bound(時限)的。1月份的目標是跑步200千米,其中包含4個半程馬拉松。1月底驗收的時候發現這個目標輕鬆達成,整個1月份我總共跑了220千米+,其中跑了4個半程馬拉松。並且第二次的馬拉松打破個人我的記錄,成績爲1小時43分30秒,把個人我的最好成績提升了2分鐘。shell

2月份個人目標的是作4000個俯臥撐+撰寫4篇技術博客。跑步的時候我可使用跑步軟件(咕咚或者悅跑圈)來記錄個人跑步里程,而記錄俯臥撐雖然有一些現成的軟件(好比Push-Ups),可是我感受過重量級,想要一種輕量的方式來記錄。後來我想到了一種方式,只需在命令行終端輸入一條簡單的命令,好比pushups 30,那麼個人微博會自動多出來一條博文,記錄我本次作了多少俯臥撐,本月已經完成了多少俯臥撐,距離目標還剩下多少俯臥撐。這樣子每作完一組,我只需敲一行命令就能夠輕鬆記錄下來,而且還有廣大網友進行監督。json

這個主意很好,但是怎麼實現那?其實整個過程並不複雜,我週末花了兩個小時就完成了。新浪微博提供了一個微博開放平臺,在微博開放平臺上其開放了一系列API,其中發送微博就屬於其中一個。咱們只需把咱們要發送的內容組裝好,編寫程序調用其發送微博的API便可。api

發送微博的API文檔在這裏。經過文檔能夠看出其實只要發送一個http請求,包含相應的內容就好。其中有兩個字段比較重要,一個是access token,一個是status。access token是認證令牌,肯定是哪一個應用向哪一個微博發送內容,status是須要推送的微博正文。ruby

獲取access_token的過程比較複雜,須要你瞭解OAuth2.0認證流程,詳情請看受權機制說明。簡單來講就是用你的微博帳號登錄微博開放平臺,註冊一個應用,而後獲得一個應用Id,而後用該應用Id調用相應的API來受權訪問你的我的微博,最後獲得一個access token。bash

若是調用這個API那?由於以前曾經寫過一個插件向微博推送個人博客信息,因此只需把相關代碼拿出來重用便可。相關代碼採用Ruby寫的。代碼以下:post

WeiboPoster
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
51
52
53
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] 

整個邏輯就是先從一個配置文件中讀出當前完成的俯臥撐個數,再配合經過命令行參數傳入的當前組作的個數,結合微博模板生成微博內容,再調用API發送HTTP請求。ui

accecs token和微博模板存放在weibo-config.yml文件中。url

weibo-config.yml
1
2
3
# Sina Weibo Post access_token: YOUR_ACCESS_TOKEN post_template: 剛纔作了%{number_done}個俯臥撐,2月份總共完成了%{total}個俯臥撐,距離4000個俯臥撐目標還差%{number_rest}個 

而後我在Rakefile中配置了一個任務,用於調用WeiboPoster類。spa

Rakefile
1
2
3
4
5
6
7
8
9
10
11
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 

最後再用一個shell腳本封裝一下,支持shell調用。插件

pushups
1
2
#!/bin/sh rake pushups[$1] 

OK這樣就齊活了。作完一組俯臥撐以後,只需在命令行輸入pushups 35,而後就能夠看到個人微博多了一篇推文。

最終效果以下。

個人微博地址:@無敵北瓜

相關文章
相關標籤/搜索