Skip to content

Instantly share code, notes, and snippets.

@hyuki
Created August 28, 2018 07:11
Show Gist options
  • Save hyuki/6ba92e144a33af112118c1ffa2530e0a to your computer and use it in GitHub Desktop.
Save hyuki/6ba92e144a33af112118c1ffa2530e0a to your computer and use it in GitHub Desktop.
twitter-post-scrapbox - ツイートURLを与えるとScrapboxに添付画像付きでツイート内容を投稿するRubyスクリプト
#!/usr/bin/env ruby
# encoding: utf-8
# https://qiita.com/mataneko_boy/items/852ca5484b80d2530560
# https://gist.github.com/hyuki/163340e4f5924c9ab8fb66c84f378132
# https://snap.textfile.org/20180518132348/
# https://scrapbox.io/help-jp/%E3%83%9A%E3%83%BC%E3%82%B8%E3%82%92%E4%BD%9C%E3%82%8B#58ae7c9a97c29100005b886b
require 'twitter'
require 'open-uri'
require 'date'
require 'uri'
# DOWNLOAD_DIR に filename.jpg を置くと DOWNLOAD_URL/filename.jpg でアクセスできるようになっている環境前提
# DOWNLOAD_DIR = '/your/server/files
# DOWNLOAD_URL = 'https://example.com'
DOWNLOAD_DIR = ENV['TWITTER_POST_SCRAPBOX_DOWNLOAD_DIR']
DOWNLOAD_URL = ENV['TWITTER_POST_SCRAPBOX_DOWNLOAD_URL']
CLIENT = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_POST_SCRAPBOX_consumer_key']
config.consumer_secret = ENV['TWITTER_POST_SCRAPBOX_consumer_secret']
config.access_token = ENV['TWITTER_POST_SCRAPBOX_access_token']
config.access_token_secret = ENV['TWITTER_POST_SCRAPBOX_access_token_secret']
end
def fetch_media(url)
html = open(url)
img = html.read
mime = html.content_type
ext = if mime.include?('jpeg')
'.jpg'
elsif mime.include?('png')
'.png'
elsif mime.include?('gif')
'.gif'
else
''
end
return img, ext
end
# pageid, body = get_all_images_and_compose_page(url)
def get_all_images_and_compose_page(url)
tweet = CLIENT.status(url)
body = tweet.full_text
if not tweet.media?
puts "No media."
t = tweet.attrs[:created_at]
d = DateTime.parse(t)
article_id = d.strftime("%Y%m%d%H%M%S")
return article_id, body
end
counter = 1
urls = []
tweet.media.each do |media|
media_url = media.media_url.to_s
img,ext = fetch_media(media_url)
filename = "twitter-#{tweet.id}-#{counter}#{ext}"
fullpath = "#{DOWNLOAD_DIR}/#{filename}"
urls << "#{DOWNLOAD_URL}/#{filename}"
puts "Download: #{fullpath}"
open(fullpath, 'w') do |f|
f.puts(img)
end
system("open #{fullpath}")
counter += 1
end
t = tweet.attrs[:created_at]
d = DateTime.parse(t)
article_id = d.strftime("%Y%m%d%H%M%S")
body += "\n"
urls.each do |url|
body += "[[#{url}]]\n"
end
return article_id, body
end
if ARGV.length != 2
puts "Usage: twitter-post-scrapbox PROJECT TWEET-URL"
puts "Example: twitter-post-scrapbox hyuki https://twitter.com/hyuki/status/995912906477731840"
abort
else
project = ARGV[0]
url = ARGV[1]
pageid, body = get_all_images_and_compose_page(url)
encbody = URI.encode(body)
system(%Q(open "https://scrapbox.io/#{project}/#{pageid}?body=#{encbody}"))
end
# vim: set filetype=ruby:
@hyuki
Copy link
Author

hyuki commented Aug 28, 2018

$ twitter-post-scrapbox hyuki https://twitter.com/hyuki/status/995912906477731840

20180828160839

@hyuki
Copy link
Author

hyuki commented Aug 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment