2018年以降の記事はGitHub Pagesに移行しました

SlideShareのAPIを叩いてスライドをDLするRubyスクリプトをHerokuにデプロイした

あらすじ

slideshareを社内から閲覧する事を禁じられているので、ワンクッションおいてスライドのpptを落とせるようにしたい

参考サイト

とりあえず公式サイトを抑えておけばいけそう。

流れ

API申請
  • slideshare -> Developer & APIのページからApply for API keyに移動
  • ログイン or 新規アカウント作成
  • Name、E-Mail、How do you want to use the API?を入力し送信。頑張って英文書く。
I want to get slideshare's slide from API.
  • 送信されたメールに貼られているAPI KeyShared Secretを控える
パラメータ
  • slideshare -> Documentationを見ながら必要なパラメータを調べる
api_key さっきのAPI Key
ts タイムスタンプ*1
hash さっきのShared Secretとこれから取得するtsでハッシュを作る*2
(username) 登録ID
(password) パスワード

全然関係ないけど、Documentationのページ内にあるi.e.って単語を初めて見たのでググってみた。論文中の「e.g.」「i.e.」の意味は何? - Diary@一色政彦WebSite that is ... すなわちとかそういう意味らしい。e.g.で例えば、for example的な。

ソース

ソースはこんな感じ。

require 'openssl'
require 'uri'
require 'net/http'

url = 'http://www.slideshare.net/api/2/get_slideshow'
param = Hash.new
param["slideshow_url"] = 'http://www.slideshare.net/gishi/wicket-presentation'
param["api_key"] = 'XXXXXXXX'
param["sharedsecret"] = 'XXXXXXXX'
# ts
param["ts"] = Time.now.to_i.to_s
# hash
param["hash"] = Digest::SHA1.hexdigest(param["sharedsecret"]+param["ts"])

uri = URI.parse(url)
Net::HTTP.new(uri.host).start do |http|
#Net::HTTP.new(uri.host, 80, ENV["PROXY"], 8080).start do |http|
  uri_param = param.sort.map {|i|i.join('=')}.join('&')
  
  res = http.get(uri.path + '?' + uri_param)
  puts res.body
end

成功するとこんな感じの内容が返ってくる。

<?xml version="1.0" encoding="UTF-8"?>
<Slideshow>
  <ID>579496</ID>
  <Title>Wicket&#20307;&#39443;&#35527;</Title>
  <Description>&#31532;1&#22238;Wicket&#21193;&#24375;&#20250;&#12398;&#12521;&#12452;&#12488;&#12491;&#12531;&#12464;&#12488;&#12540;&#12463;&#30330;&#34920;&#36039;&#26009;</Description>
  <Status>2</Status>
  <Username>gishi</Username>
  <URL>http://www.slideshare.net/gishi/wicket-presentation</URL>
  <ThumbnailURL>http://cdn.slidesharecdn.com/wicket-1220375587470160-8-thumbnail</ThumbnailURL>
  <ThumbnailSmallURL>http://cdn.slidesharecdn.com/wicket-1220375587470160-8-thumbnail-2</ThumbnailSmallURL>
  <Embed>&lt;div style=&quot;width:425px&quot; id=&quot;__ss_579496&quot;&gt;&lt;strong style=&quot;display:block;margin:12px 0 4px&quot;&gt;&lt;a href=&quot;http://www.slideshare.net/gishi/wicket-presentation&quot; title=&quot;Wicket&#20307;&#39443;&#35527;&quot;&gt;Wicket&#20307;&#39443;&#35527;&lt;/a&gt;&lt;/strong&gt;&lt;object id=&quot;__sse579496&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=wicket-1220375587470160-8&amp;stripped_title=wicket-presentation&amp;userName=gishi&quot; /&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;/&gt;&lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot;/&gt;&lt;param name=&quot;wmode&quot; value=&quot;transparent&quot;/&gt;&lt;embed name=&quot;__sse579496&quot; src=&quot;http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=wicket-1220375587470160-8&amp;stripped_title=wicket-presentation&amp;userName=gishi&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; wmode=&quot;transparent&quot; width=&quot;425&quot; height=&quot;355&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;div style=&quot;padding:5px 0 12px&quot;&gt;View more &lt;a href=&quot;http://www.slideshare.net/&quot;&gt;presentations&lt;/a&gt; from &lt;a href=&quot;http://www.slideshare.net/gishi&quot;&gt;Hiroto Yamakawa&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;</Embed>
  <Created>Tue Sep 02 10:14:12 -0500 2008</Created>
  <Updated>Tue Sep 02 10:16:15 -0500 2008</Updated>
  <Language>ja</Language>
  <Format>ppt</Format>
  <Download>1</Download>
  <DownloadUrl>http://s3.amazonaws.com/ppt-download/wicket-1220375587470160-8.ppt?response-content-disposition=attachment&amp;Signature=ABs151smgWZ9213%2FyFq81fnMc6A%3D&amp;Expires=1328606581&amp;AWSAccessKeyId=AKIAJLJT267DEGKZDHEQ</DownloadUrl>
  <SlideshowType>0</SlideshowType>
  <InContest>0</InContest>
</Slideshow>

おー取れた。んで、xmlの中のDownloadUrlからpptをゲットできた! これをSinatraでWebアプリケーションにしてHerokuにデプロイすれば職場から行けるかな!?

→いけた! 自分の中ではお役立ち。

*1:Set this to the current time in Unix TimeStamp format, to the nearest second(?).

*2:Set this to the SHA1 hash of the concatenation of the shared secret and the timestamp (ts). i.e. SHA1 (sharedsecret + timestamp). The order of the terms in the concatenation is important.