Last active
July 21, 2022 10:33
-
-
Save hyuki/ef3760610e6913dc25c8938aaaa0cf87 to your computer and use it in GitHub Desktop.
twitter-markdown.rb - convert tweet to markdown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require 'twitter' | |
require 'open-uri' | |
require 'date' | |
require 'uri' | |
# $ grep TWITTER_MARKDOWN ~/.bash_profile | |
# export TWITTER_MARKDOWN_consumer_key=XXXXXXXXXXXXXXXXXXXXXXXXXXX | |
# export TWITTER_MARKDOWN_consumer_secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
# export TWITTER_MARKDOWN_access_token=9999999-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
# export TWITTER_MARKDOWN_access_token_secret=xxxxxxxxxxxxxxxxxxxx | |
CLIENT = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV['TWITTER_MARKDOWN_consumer_key'] | |
config.consumer_secret = ENV['TWITTER_MARKDOWN_consumer_secret'] | |
config.access_token = ENV['TWITTER_MARKDOWN_access_token'] | |
config.access_token_secret = ENV['TWITTER_MARKDOWN_access_token_secret'] | |
end | |
def fetch_markdown(url) | |
tweet = CLIENT.status(url, { tweet_mode: "extended" }) | |
markdown = [] | |
name = tweet.attrs[:user][:name] | |
screen_name = tweet.attrs[:user][:screen_name] | |
markdown << ">**#{name}** [@#{screen_name}](https://twitter.com/#{screen_name})" | |
markdown << ">" | |
tweet.attrs[:full_text].split(/\n/).each do |line| | |
markdown << ">#{line}" | |
end | |
markdown << ">" | |
if tweet.media? | |
tweet.media.each do |media| | |
img = media.media_url_https.to_s | |
markdown << ">![#{img}](#{img})" | |
end | |
end | |
created_at = DateTime.parse(tweet.attrs[:created_at]).to_time.localtime.strftime("%Y-%m-%d %H:%M:%S") | |
markdown << ">[#{created_at}](#{url})" | |
markdown.join("\n") | |
end | |
if ARGV.length != 1 | |
puts "Usage: twitter-markdown TWEET-URL" | |
puts "Example: twitter-markdown https://twitter.com/hyuki/status/1236171158606639104" | |
abort | |
end | |
url = ARGV[0] | |
puts fetch_markdown(url) | |
# vim: set filetype=ruby: |
media.media_url
よりmedia.media_url_https
の方がいい。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://snap.textfile.org/20200308180914/