[分享][Go] 建立一個 Server 幫你把 Twitter 透過 IFTTT 轉發到 Plurk

Plurk 一直是台灣人很愛的推文服務.不過因為只有台灣在使用,所以許多大的服務(比如說 IFTTT ) 就不太理他. 之前本來還可以很簡單的透過 Email 來發送噗,結果也不知道為何關閉 (聽說要收費?) . 於是想了很多方式來弄,都還是有點麻煩. 最後還是自己寫一個 Server 來給 IFTTT 使用

如何讓你的 Twitter 轉發到 Plurk ?

首先申請一個 Plurk APP

  1. 先到這個地方,申請你的 App
    • 類別: 手機或應用程式
    • 其他相關資料填寫可以參考這篇文章
  2. 點下測試工具
  3. 取得以下四個數值
    • App key
    • App secret
    • Token
    • Token secret

在Plurk App就算是完成,接下來要到 IFTTT設定

再來架設你自己的Plurk Maker Server

https://github.com/kkdai/plurk-makerserver 按下下面的按鈕

Deploy

記住你的 Server URL 等等要使用

在 IFTTT Maker 上的設定

  1. 接下來到 IFTTT Maker 申請一個帳號.

  2. 建立一個 IFTTT Receipt , 前端用 Twitter 接起來,後面接到剛剛建立的 Maker .

  3. Maker 設定頁面上,資料依照以下的格式來填:

  • URL : 你剛剛的 Server URL
  • Method: POST
  • Content Type: application/json
  • Body: 依照以以下的修改,貼上去
// 記得將以下資料換成剛剛在 Plurk 拿到的資料
// [[App key]]      -> App key
// [[App secret]]   -> App secret
// [[Token]]        -> Token
// [[Token secret]] -> Token secret
// [[Text]]         -> 改成 twitter 內容 

{"ConsumerToken":"[[App key]]", "ConsumerSecret":"[[App secret]]", "AccessToken": "[[Token]]","AccessSecret":"[[Token secret]]", "Msg": "[[Text]]"}

這樣就可以了…. 有問題來討論…

程式設計週記[2016/04/01] 微軟: 今年我們讓 Windows 越來越不像 Windows.. 語畢 全場歡呼

這是什麼?

程式週記主要內容如下:

Gihub project 介紹:

  • 主要會貼一些github,但是會盡量寫上一些有用的評語(或是我容易想到的關鍵詞)幫助以後查詢

網路文章心得:

  • 會寫些心得,強迫自己閱讀.

“程式週記”並且定期週期性更新.

大部分內容在我的twitter都會有,這邊只是將一些簡單的心得與感想註解一下.

本週摘要

最近幾週越來越少有時間可以好好的把每週專案來寫, 主要是因為這一週都把時間拿去重新閱讀 Raft Paper. 只好這一週寫一個小工具 slack-console 來幫助自己管理伺服器上面發生的問題.

Go

Building Minimal Docker Containers for Go Applications

這一篇主要是討論如何編譯出一個最小的docker container給Go application使用.

以下的方式可以:

  • CGO_ENABLED=0: 拔掉CGO (在Go裡面使用C的library, 如果你沒用到)
  • GOOS=linux: 預設定好環境 linux
  • -a: 連所有的使用到的package都重編譯(在這邊,一併會把cgo拿掉)
  • -installsuffix cgo: 前面明明把cgo拔掉,為何後面還要把需要的部分加回去呢? 因為這是Go 1.4 的workaround

全部參數如下:

CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

另外一個可以參考的repo go-proxy: A simple proxy implementation

剛剛測試,以上的部分在Go 1.6 沒有看到太大差異.可能因為我還沒有用到太複雜的package.

LeftPad and Go: can tooling help?

從 Go 開發者角度來看 LeftPad 事件

Writing a Text Adventure Game in Go

透過 Go 來寫一個文字冒險遊戲

Paranoid text spacing in Go (Golang)

Vinta 大大寫的工具, 可以自動幫你把中文與英文中間加上空白. (Paranoid text spacing)

Python

Android/JAVA/NODE.JS

Docker

Docker image for thumbor. Detectors, optimizers, lazy detection and separate docker for remotecv.

透過 docker 架設一個具有自動縮圖與人臉辨識的伺服器, 從碼天狗看到的.

Docker Monitoring with the ELK Stack: A Step-by-Step Guide

透過Docker來架設ELK (Elasticsearch, Logstash, Kibana)的架構.

Docker Machine driver plugin for xhyve (native OSX hypervisor)

排不到 Docker for MacOSX Beta ? 先來看看裡面用的技術, 也就是原生的 MacOSX Hypervisor (xhyve) Driver.

iOS/Swift

網站文章

Ubuntu on Windows — The Ubuntu Userspace for Windows Developers

Microsoft Build第一天最大亮點: Windows 10 新的更新將不僅僅是bash shell 而是整個ubuntu user space,所以你還可以使用 gcc, vim, sed, find, sort… 所以應該可以跟cygwin說再見…

不是透過VM,而且你也會有 /usr 類似的目錄可以使用.. 我想.. Windows 真的越來不像Windows… 不知道大家喜不喜歡…

網站收集

Build 2016

每年又到了微軟最大的開發者聚會 Microsoft Build 今年亮點不少, 大家可以看看 :

MSFT: Bot Framework

微軟同時宣佈他們新的 Bot Framework 一個架設在網路的機器人架構, 可以透過這個在 slack, skype 甚至是 IM 上面建立機器人. 透過機器人可以幫助你查詢甚至是做更多的事情.

有聲書/影片心得

本週專案

這邊會寫一些我的Project 52的成果.

本週專案: https://github.com/kkdai/slack-console

一個可以快速幫你在 slack channel 發言的小工具,適合放在 server 上管理機器用.

本週專案比較小, 主要是寫小東西來避免每次打 curl 要落落長的 comment . 這個使用也很簡單, 比較複雜還是在取得 Slack IncomingWebHook.

[TIL][Go] 把http.response.body 在關閉前先清乾淨可以達到重複使用增加四倍速度

前言

主要是因為這一份twiiter讓我注意到,Filippo Valsorda(@FiloSottile) 提到說他只花了四行程式碼就讓go-github (Google 出的直接操控github 的package) 連線速度可以增加四倍

原因?

根據這次的PR,可以知道主要的原因是在於json decoder對於io.reader讀取資料的時候. 不會一次把所有的直抓完,而會剩下一個 \n

由於這個因素 response.Body (io.Readcloser)呼叫close的時候會把整個TLS的connect關閉.造成每次的connection 都會重新啟動,浪費了許多無謂的時間.

解法

針對Reader的行為而言,如果是讀到了最後一個位元,接下來的讀取都會讀到EOF. 這裡有一個範例可以參考

(When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.)

所以如果不是使用json decoder,而是使用其他方式來讀資料.就會連\n都讀完,使得response.Body清空後close不會直接把connect 整個關閉.

所以mattn也提供了一個小套件,其實就是把這件事情實現:

resp, err := http.Get(blah)
if err != nil {
    return err
}
json.NewDecoder(resp.Body).Decode(&data)

// 如果reader 已經被清掉,直接結束
if resp.Body == nil {
	return nil
}

// 如果body reader有存在,先把它清空 (drain)
// 透過io.Copy的方式把 resp.Body清空
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
	return err
}

// 這樣一來這樣的Close就不會將TLS connection關閉.
return resp.Body.Close()

此外,透過mattn的這篇文章,也有提到.json.Unmarlshal並不會有這個問題.

2016/03/31 更新

Bradfitz 也就是Go net/http的作者就跳出來說要把這個問題修回去Golang裡面,避免以後其他部分的影響.不過這部分的修改,已經趕不上Go 1.6之中了,大家要稍微注意一下.

[TIL] Note: about change your shell from bash to fish

Preface

When I first take a look about fisherman, I got lots of interesting to change my shell from bash to fish

play

How to Change to Fish (shell)

Install Fish shell

brew install fish

Change it to default shell

//Add fish shell into known shell list
sudo -s 
echo /usr/local/bin/fish >> /etc/shells

//back to original user
//change default shell to fish
exit
chsh -s /usr/local/bin/fish

Suggestion a good plugin for Fisherman?

## My suggest plugin for fisherman
fisher omf/theme-bobthefish

# ~   master *…  Documents/ 

Ops, Why my all configurations missing?

You might occur all the ~/.bash_profile and ~/.bashrc setting missing in fish.

Here is guideline to change it

I got help from @jbucaran, here is his guildline.

1. Convert aliases into functions to opt in fish autoloading, tl;dr alias is slow, functions are fast.

For example:

function vi
     vim $argv
end

2. For export, use set -x

For example:

//setup GOPATH
set -x GOPATH $HOME/src/go

//Extend your PATH
set -x PATH $PATH $GOBIN

Note:

//bash setting
export GOROOT=`go env GOROOT`

//change to fish
set -x GOROOT (go env GOROOT)

More detail, feel free to check this gist.

Let use “fisherman”

“fisherman” is fish plugin manager, which is powerful and useful. Check here for more detail.

curl -sL get.fisherman.sh | fish

Any problem? How to change it back?

Don’t worry, just use chsh to change it back.

chsh -s /bin/bash

[TIL] Push Go 1.6 code to heroku and add Deploy to Heroku button

Publish to Heroku

  • Init git and commit your code.
    • git init
    • git add .
    • git commit -m "init project"
  • Login heroku
    • heroku login
  • Using Golang backpack
    • heroku create -b https://github.com/kr/heroku-buildpack-go.git
  • Vendoring: For Go 1.6 (refer here)
    • go get -u github.com/tools/godep
    • Save your dependency godep save github.com/kkdai/githubrss
    • Submit your depdendency
      • git add .
      • git commit -m "godep"
  • Push your complete code to Heroku
    • git push heroku master

Add “Deploy to Heroku”

  • copy app.json (refer more spec here)
  • Remember must include buildpack. And all buildpack list here.
  • Make sure your buildpack sync with what you use
    • In my case, I use https://github.com/kr/heroku-buildpack-go.git:
"buildpacks": [
    {
      "url": "https://github.com/kr/heroku-buildpack-go.git"
    },
    {
      "url": "heroku/go"
    }	

All app.json sample as follow (refer to rss-webserver):

程式設計週記[2016/03/25]: Docker歡慶三歲.. Docker生態圈已經日漸茁壯

這是什麼?

程式週記主要內容如下:

Gihub project 介紹:

  • 主要會貼一些github,但是會盡量寫上一些有用的評語(或是我容易想到的關鍵詞)幫助以後查詢

網路文章心得:

  • 會寫些心得,強迫自己閱讀.

“程式週記”並且定期週期性更新.

大部分內容在我的twitter都會有,這邊只是將一些簡單的心得與感想註解一下.

本週摘要

本週時間花在Survey 各種IoT Big Data的架構與各種元件的用法,因為Docker三週年聚會,也花了不少時間玩Docker.

比較少時間來弄Golang.下周應該繼續看分散式系統才對.

Go

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template

這個Go template 效能有點恐怖.. 20x faster than html/template!!

“Go Learn: Some rookie Go mistakes we made (And how we recovered from them)”

不少值得看的粗心錯誤,挑出幾個很容易犯的:

  1. Naked return (先定義好的return value) 會讓整個function 更難閱讀(因為return不需要再帶回傳值). 並且很容易少回傳幾個參數而造成回傳沒有意義的初始值.

  2. Map 的concurrency 不穩定性 (可以參考Go FAQ: Atomic_Maps),要寫concurrency program 要儘量避免使用map

同場加印: Gist上面有人提供Go地雷Landmines

  • 主要是for i scope
  • interface pointer
  • 前面提到Naked return與容易踩到scope問題.

vim-go 1.5 Release

vim-go release新版 New in 1.5 (1) fatih/motion 提供函式檢索的功能 (2) GoAlternate 可以快速切換test跟原本檔案

A microservice toolkit

號稱有著相當多microservice功能的 toolkit,並且具有pluggable(可拔插)功能的設計.感覺很酷…

jsonparser: Alternative JSON parser for Go that does not require schema

相當快速的json parser(根據作者測試),甚至可以比”encoding/json” 快上九倍.

其實有附上跟一些其他json parser的效能比較表,可以參考看看. 同場加映: https://github.com/mailru/easyjson 也說他比 “ encoding/json”快上不少… 不過他跟jsonparser沒有比過效能.. 倒底”encoding/json”是有多慢呢 XDDD

Python

Android/JAVA/NODE.JS

Docker

docker-NAT-router: Docker container that functions as a simple NAT router. Linux iptables MASQUERADE provides network address translation (NAT) and dnsmasq provides DHCP, DNS, and TFTP services.

透過docker來做NAT router.之前再找就是想透過docker來做hole punching的.總算找到比較類似的東西… 主要是透過pipework來達成,這篇有更多相關應用.

此外這一篇有提到Docker 1.9裡面的新東西Container Network Model (CNM) (更多官方文件,).不過似乎無法達成我需要的MASQUERADE的功能. 繼續多多研究.

katacoda: Learn new technologies right in your browser

透過瀏覽器來學習一些科技,裡面有docker的相關教學.可以線上透過瀏覽器來下指令操作.

原生的Docker VM for Mac/Windows

Docker for Mac 與 Docker for Windows 版本,以後在 Mac 與 Windows 上面跑 Docker 不用再安裝VirtualBox,提供的原生的網路支援與更棒的磁碟掛載功能

iOS/Swift

網站文章

網站收集

有聲書/影片心得

本週專案

這邊會寫一些我的Project 52的成果.

本週專案: https://github.com/kkdai/rss-webserver

主要是把前一次的專案githubrss架成RSS Server,順便複習一下Host在Heroku上面. 結果花最多的時間都在弄Heroku.

結果弄好之後,接上IFTTT沒兩天就爆量 (笑) 不過至少看來是個正確的RSS Provider.